Skip to content

WW-5652 Add support for Java records and Optional serialization in the JSON plugin.#1792

Open
srilakshmikanthanp wants to merge 2 commits into
apache:mainfrom
srilakshmikanthanp:main
Open

WW-5652 Add support for Java records and Optional serialization in the JSON plugin.#1792
srilakshmikanthanp wants to merge 2 commits into
apache:mainfrom
srilakshmikanthanp:main

Conversation

@srilakshmikanthanp

Copy link
Copy Markdown

Summary

  • Java records are now serialized using RecordComponent metadata, as Introspector.getBeanInfo() does not expose record components through the traditional JavaBeans property model.
  • java.util.Optional now serializes as its contained value or null when empty. Optional.empty() is treated as null when excludeNullProperties is enabled, including for bean/record properties and map values.
  • @JSON and @JSONFieldBridge annotations 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.

@lukaszlenart

Copy link
Copy Markdown
Member

Please create a JIRA ticket first https://issues.apache.org/jira/projects/WW/

@srilakshmikanthanp

Copy link
Copy Markdown
Author

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.

@srilakshmikanthanp srilakshmikanthanp changed the title Add support for Java records and Optional serialization in the JSON plugin. WW-5652 Add support for Java records and Optional serialization in the JSON plugin. Jul 17, 2026
@srilakshmikanthanp

Copy link
Copy Markdown
Author

@lukaszlenart I created the required JIRA issue: WW-5652. Please let me know if any further changes are needed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Optional unwrapping during serialization, and treat Optional.empty() as “absent” when excludeNullProperties is enabled (including map values).
  • Expand unit test coverage for record serialization scenarios and Optional behaviors.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants