WW-5652 Add support for Java records and Optional serialization in the JSON plugin.#1792
WW-5652 Add support for Java records and Optional serialization in the JSON plugin.#1792srilakshmikanthanp wants to merge 2 commits into
Conversation
|
Please create a JIRA ticket first https://issues.apache.org/jira/projects/WW/ |
|
Thanks! I've requested an Apache JIRA account. Once it's approved, I'll create the JIRA issue and update this PR with the issue number. |
|
@lukaszlenart I created the required JIRA issue: WW-5652. Please let me know if any further changes are needed. |
There was a problem hiding this comment.
Pull request overview
This PR updates the Struts JSON plugin’s serializer to natively support Java records (via RecordComponent metadata) and to serialize java.util.Optional as its contained value (or null when empty), aligning record/Optional handling with existing excludeNullProperties behavior.
Changes:
- Add record-aware serialization path in
StrutsJSONWriter, iterating record components and honoring@JSON/@JSONFieldBridge. - Add
Optionalunwrapping during serialization, and treatOptional.empty()as “absent” whenexcludeNullPropertiesis enabled (including map values). - Expand unit test coverage for record serialization scenarios and
Optionalbehaviors.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| plugins/json/src/main/java/org/apache/struts2/json/StrutsJSONWriter.java | Adds Optional handling, record component iteration serialization, and “absent” detection to support excludeNullProperties behavior. |
| plugins/json/src/test/java/org/apache/struts2/json/StrutsJSONWriterTest.java | Adds tests covering record serialization, annotation behavior on record components, include/exclude filters, and Optional serialization/exclusion semantics. |
| plugins/json/src/main/java/org/apache/struts2/json/annotations/JSONFieldBridge.java | Allows @JSONFieldBridge to be applied to record components. |
| plugins/json/src/main/java/org/apache/struts2/json/annotations/JSON.java | Allows @JSON to be applied to record components. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
RecordComponentmetadata, asIntrospector.getBeanInfo()does not expose record components through the traditional JavaBeans property model.java.util.Optionalnow serializes as its contained value ornullwhen empty.Optional.empty()is treated asnullwhenexcludeNullPropertiesis enabled, including for bean/record properties and map values.@JSONand@JSONFieldBridgeannotations can now be applied directly to record components.I implemented these changes based on my understanding of the existing codebase and tried to keep the implementation minimal and consistent with the current design.
I'd appreciate a review of the implementation and approach. If you'd like any changes, please let me know and I'll update the PR.