fix: auto-convert array values to comma-separated strings for checkbox fields#34816
fix: auto-convert array values to comma-separated strings for checkbox fields#34816
Conversation
…x fields - Modified FieldHandlerStrategyFactory.textStrategy() to detect when CheckboxField receives Collection values and automatically convert them to comma-separated strings - Added type safety check in ESContentletAPIImpl.groupContentletsByLanguage() to prevent ClassCastException with non-Contentlet objects - Resolves issue where workflow fire endpoint returned misleading ClassCastException instead of proper validation - Maintains backward compatibility with existing string inputs for checkbox fields - Adds debug logging for transparency when array conversion occurs Fixes #34815 Co-authored-by: Freddy Montes <fmontes@users.noreply.github.com>
|
@claude does this fix means that now support array and comma separated strings? |
|
Claude finished @fmontes's task —— View job Analyzing the fix implementationLet me examine the changes to understand exactly what input formats are now supported. Todo:
Answer: Yes, this fix now supports both array and comma-separated string formats!Here's exactly what the fix provides: 🔄 Auto-Conversion for Checkbox FieldsThe key change is in // Special handling for checkbox fields that receive array values
if (field instanceof CheckboxField && value instanceof Collection) {
final Collection<?> arrayValue = (Collection<?>) value;
final String commaSeparatedValue = arrayValue.stream()
.filter(Objects::nonNull)
.map(Object::toString)
.collect(Collectors.joining(","));
contentlet.setStringProperty(field.variable(), commaSeparatedValue);
return;
}📋 Supported Input Formats✅ JSON Arrays (NEW): {
"searchEngineIndex": ["index", "follow", "snippet"],
"sitemap": ["true"]
}→ Auto-converts to: ✅ Comma-Separated Strings (EXISTING): {
"searchEngineIndex": "index,follow,snippet",
"sitemap": "true"
}→ Remains as: 🛡️ Additional Safety ImprovementsThe fix also added a type safety check in .filter(content -> content instanceof Contentlet) // Type safety check to prevent ClassCastExceptionThis prevents the ClassCastException that was occurring when the workflow fire endpoint received JSON arrays. 🔧 Backward Compatibility
So yes, checkbox fields now accept both formats seamlessly, with automatic conversion happening behind the scenes for JSON arrays! |
There was a problem hiding this comment.
Pull request overview
This PR addresses workflow fire requests that send checkbox field values as JSON arrays, preventing downstream failures by normalizing those values and attempting to harden relationship grouping against unexpected record types.
Changes:
- Auto-convert
CheckboxFieldvalues provided asCollectioninto a comma-separated string inFieldHandlerStrategyFactory.textStrategy(). - Add an element-type filter in
ESContentletAPIImpl.groupContentletsByLanguage()intended to avoidClassCastExceptionduring relationship validation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| dotCMS/src/main/java/com/dotcms/content/elasticsearch/business/field/FieldHandlerStrategyFactory.java | Normalizes checkbox array inputs into the string format expected by selectable field types. |
| dotCMS/src/main/java/com/dotcms/content/elasticsearch/business/ESContentletAPIImpl.java | Attempts to guard relationship language grouping against non-Contentlet elements reaching this code path. |
...c/main/java/com/dotcms/content/elasticsearch/business/field/FieldHandlerStrategyFactory.java
Outdated
Show resolved
Hide resolved
dotCMS/src/main/java/com/dotcms/content/elasticsearch/business/ESContentletAPIImpl.java
Outdated
Show resolved
Hide resolved
…fields - Handle MultiSelectField, TagField, CategoryField in addition to CheckboxField - Addresses Copilot review feedback about incomplete multi-value field support - Improves logging to show specific field type being converted - Ensures all selectable fields with array inputs get properly converted to comma-separated strings Co-authored-by: Freddy Montes <fmontes@users.noreply.github.com>
Use untyped stream to safely filter non-Contentlet elements and prevent ClassCastException during relationship validation. Co-authored-by: Freddy Montes <fmontes@users.noreply.github.com>
|
Claude finished @fmontes's task —— View job PR Code ReviewTodo:
Issues Found:1. Input Validation Gap (FieldHandlerStrategyFactory.java:275-278)final String commaSeparatedValue = arrayValue.stream()
.filter(Objects::nonNull)
.map(Object::toString)
.collect(Collectors.joining(","));Risk: No validation of array element content. Malicious inputs could inject commas, special characters, or extremely long strings that break downstream processing or storage constraints. Recommendation: Add input sanitization: .map(obj -> sanitizeFieldValue(obj.toString()))
.filter(str -> !str.contains(",")) // Or escape commas2. Collection Type Safety (FieldHandlerStrategyFactory.java:274)final Collection<?> arrayValue = (Collection<?>) value;Risk: No size limits or type validation on the collection. Could accept infinite-sized collections or collections containing unexpected object types. Edge Cases:
3. Type Safety Fix Incomplete (ESContentletAPIImpl.java:8836-8838)The fix addresses the immediate ClassCastException but:
Recommendation: Add logging for dropped elements to aid debugging. 4. Missing Edge Case TestingNo apparent handling for:
AssessmentNot clean - Has several input validation gaps and edge cases that could cause issues in production. |
Fixes misleading ClassCastException when checkbox fields are sent as JSON arrays to workflow fire endpoint.
Changes
Testing
Handles the exact scenario:
{"searchEngineIndex": ["index", "follow", "snippet"], "sitemap": ["true"]}"index,follow,snippet"and"true"Fixes #34815
Generated with Claude Code