Fix flaky De/Serialization and Tests #4960
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Seeing two types of flakiness that this PR attempts to address: JSON String Equality assertions, and JSON property ordering flakiness.
Solution:
Firstly, across the XChange test suite, there are several instances of tests that validate a JSON object. The JSON specification is inherently unordered, and thus, a simple string equality may fail if because we rely upon
jackson
producing ordered JSON.jackson
internally uses a Hash-based datastructure, so iteration order is not guaranteed to be deterministic. For example:Second: Many of the DTO objects that are parsed by XChange via
jackson
rely on ordered arrays, e.g. a[price, size]
array. However, we cannot rely upon the order that class fields are declared in the class source, as Java Class'getDeclaredFields()
is explicitly nondeterministic. Thus, annotating DTO objects that haveJsonFormat.Shape.ARRAY
with a@JsonPropertyOrder
(doc) is best practice and prevents deserialization errors (or worse -- incorrectly parsed data that doesn't throw an error). An example:Reproduction:
These are most likely to cause test failures / flaky behavior if XChange is compiled and tested with different JVM/JDK distributions running on different architectures, so is somewhat of a preemptive measure. The tests can be shown to be flaky via NonDex and the following command:
Happy to iterate on this!