Skip to content

Conversation

cperkkk
Copy link

@cperkkk cperkkk commented Oct 1, 2025

Fix Flaky Test in JsonPOJOMapperTest

Motivation

Problem

The testSerialization unit test in JsonPOJOMapperTest fails intermittently due to non-deterministic ordering of fields in the JSON output string. The test compares the exact string representation of a serialized JSON object.

The object also contains a HashMap<String, Object> field. Since HashMap does not guarantee iteration order, the serialized JSON string may have fields in different orders, causing the test to fail (even though the serialized data is semantically correct).

image

The fix ensures the test is robust to non-deterministic behaviors of java.util.Map and compatible with future updates to it or related dependencies. Let me know if further improvements or clarifications are needed!

Solution

The fix involves two changes to ensure consistent field ordering:

  1. Add @JsonPropertyOrder annotation to the MyType class to enforce a specific order for top-level fields during serialization
  2. Change HashMap to LinkedHashMap for the c field to preserve insertion order of nested map entries
@JsonPropertyOrder({"a", "b", "c", "d", "e"}) // changes here
public static class MyType {
  public int a;
  public String b;
  public HashMap<String, Object> c = new LinkedHashMap<>(); //changes here
  public List<MyType> d = new ArrayList<>();
  public List<Integer> e = new ArrayList<>();
}

Reproduction of Bug

Use the TestingResearchIllinois's NonDex tool and run:

mvn edu.illinois:nondex-maven-plugin:2.1.7:nondex -pl vertx-core -Dtest=JsonPOJOMapperTest

Error Message

Before the fix:
image

After the fix:
image

Conformance

I have signed the Contributor Agreement.

@cperkkk
Copy link
Author

cperkkk commented Oct 2, 2025

Hi @vietj seems like there's another flaky test unrelated to this that makes the check fails.

image

Please kindly advise on what to do, thank you!

@vietj
Copy link
Member

vietj commented Oct 2, 2025

@cperkkk can you rebase on latest ? this test has been rewritten recently

@cperkkk cperkkk force-pushed the fix_flaky_JsonPOJOMapperTest branch from 073058d to 5d1d240 Compare October 2, 2025 17:02
@cperkkk
Copy link
Author

cperkkk commented Oct 2, 2025

I have rebased the branch, thanks a lot @vietj

@cperkkk
Copy link
Author

cperkkk commented Oct 3, 2025

I have merged changes from main again since you fixed another flaky test as well @vietj

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