Skip to content

Add tests for json-customize example - #535

Open
fp024 wants to merge 2 commits into
apache:mainfrom
fp024:DEV-2026-07-06
Open

Add tests for json-customize example#535
fp024 wants to merge 2 commits into
apache:mainfrom
fp024:DEV-2026-07-06

Conversation

@fp024

@fp024 fp024 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds JUnit tests for the json-customize example.

Motivation

This PR adds a reproducible JUnit test case to the struts-examples repository to make it easier to verify the behavior of the custom JSON writer configuration.

With Struts 7.1.1, the custom JSON writer configured by the json-customize example is used as expected. However, after changing only the Struts version to 7.2.1, the configured custom JSON writer no longer appears to be selected, and org.apache.struts2.json.StrutsJSONWriter is used instead.

The behavior can be reproduced or verified by changing only the Struts version to 7.2.1 and running the added tests.

Since the Struts main repository does not have the Issues tab enabled, I am submitting this to struts-examples as a reproducible test case.

Notes

The tests use "json-unit-assertj" to validate the serialized JSON output. The lastLogin field is validated by date format instead of comparing against a fixed value.

Thanks 👍

fp024 added a commit to fp024/struts2-study that referenced this pull request Jul 6, 2026
Jackson, Flexjson으로 사용자 지정 JSON Writer를 사용한 부분은 7.2.1로 버전 업그레이드를 할 때,
기본으로 제공되는 StrutsJSONWriter 만 사용할 수 있어서, 아직은 7.1.1로 버전을 고정해두었다.

이 문제 현상에 대해서는 struts-example의 json-customize 예제에 테스트 코드를 추가해서 보고했다.
* apache/struts-examples#535
Comment thread json-customize/pom.xml Outdated
@lukaszlenart

Copy link
Copy Markdown
Member

@fp024 you must rebase onto main branch

@fp024
fp024 force-pushed the DEV-2026-07-06 branch from 74d3dac to b7b7b9f Compare July 6, 2026 13:23
@fp024

fp024 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

@lukaszlenart
Rebased onto the latest main branch. Thank you.

@fp024

fp024 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

When flexjson excludes fields with null values, it leaves trailing keys behind, which results in an invalid JSON string. I have pushed an additional commit to resolve this issue.

@lukaszlenart

Copy link
Copy Markdown
Member

Test ProduceActionTest fails

@fp024

fp024 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

I added and shared this test case specifically because it fails on Struts 7.2.1 while passing completely on Struts 7.1.1.

On Struts 7.2.1, the following configuration does not take effect, and the system is seemingly forced to use only the default StrutsJSONWriter instead of our custom bean:

  <bean type="org.apache.struts2.json.JSONWriter" name="flexJSONWriter" class="org.demo.FlexJSONWriter"
        scope="prototype"/>
  <constant name="struts.json.writer" value="flexJSONWriter"/>

@lukaszlenart

Copy link
Copy Markdown
Member

Thanks for that, I will double check what's wrong

@lukaszlenart

Copy link
Copy Markdown
Member

Nice, this is regression in Struts 7.2.1, I will prepare a fix.

@lukaszlenart

Copy link
Copy Markdown
Member

Ticket ready, on it
https://issues.apache.org/jira/browse/WW-5641

@fp024

fp024 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Thank you very much for checking. Have a nice day! 👍

@lukaszlenart

Copy link
Copy Markdown
Member

Thanks for this — the ProduceActionTest coverage and the ProduceAction cleanup are welcome, and this module had no tests at all before.

Heads up that #544 just landed and overlaps part of this PR, which is why it now shows conflicts. On current main, json-customize already has:

  • web.xml migrated to the Jakarta EE 6.0 schema (same target as your change)
  • struts.xml on https://struts.apache.org/dtds/struts-6.5.dtd
  • the POM schema URL moved to https://

So when you rebase, the XML parts of this PR can simply be dropped. Two of them would otherwise move things backwards:

  • your struts.xml points at http://struts.apache.org/dtds/struts-6.0.dtd, where main is now on https:// and 6.5
  • your web.xml keeps id="struts_blank"/id="WebApp_ID", which the migration dropped repo-wide

The valuable parts — ProduceActionTest.java, and the changes to ProduceAction, ExcludeTransformer, and FlexJSONWriter — rebase cleanly as far as I can tell.

One thing to check while you're rebasing: struts.parameters.requireAnnotations has defaulted to true since before 7.2.1, so any action property that receives a request parameter needs @StrutsParameter (depth 0 on the setter, depth ≥ 1 on the getter). #544 fixed seven modules that were silently dropping parameters. If ProduceAction takes any request input, it likely needs the annotation too — and a test that posts a parameter and asserts it arrives would catch exactly that class of bug.

CI is also currently red here (build (21) failed, build (17) cancelled) from before the conflicts, so worth a look after the rebase.

@fp024

fp024 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

@lukaszlenart

Hi, thanks for the review!

I've rebased the branch on main and dropped the redundant XML/DTD changes as suggested.
I verified locally that mvn clean test passes cleanly.

Regarding @StrutsParameter: ProduceAction only outputs internally generated data as JSON and doesn't accept any incoming request parameters, so no additional annotations were needed.

Thanks!

@fp024

fp024 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

Update: Added a follow-up commit as the assertion update to json-unit-assert was missing after the rebase.

@fp024

fp024 commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

Hello.

The struts-6.5.dtd file has not been uploaded to https://struts.apache.org/dtds/.

In the case of IntelliJ, there were no issues as it automatically reads the struts-6.5.dtd embedded in struts2-core-7.3.0.jar...

However, in the VS Code + Red Hat XML Extension environment, a 404 error occurs because it attempts to download the DTD directly from the web at that address and cache it.

https://struts.apache.org/dtds/struts-6.0.dtd is currently available for download.

It seems that the struts-6.5.dtd file also needs to be uploaded.

Please check into this.

@lukaszlenart

Copy link
Copy Markdown
Member

Good catch, I will fix that later, thanks!

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.

2 participants