refs #3613 - Configurable and deterministic order of JSON and YAML output #3740
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.
Includes and replaces #3613, provides a solution for related issues, including:
The solution covers two areas:
deterministic input (reflection): methods and fields are sorted consistently, thanks @hiddewie!
sorted serialization.
This is achieved in two possible ways:
A. specifying the newly introduced configuration option (and maven/gradle parameter)
sortOutput
, which applies an opinionated sorting , using JacksonORDER_MAP_ENTRIES_BY_KEYS
andSORT_PROPERTIES_ALPHABETICALLY
along with defined order for serialization ofOpenAPI
root andSchema
instancesB. fully customizing the
ObjectMapper
used in serialization of output, using the existing mechanism of theObjectMapperProcessor
which is now defining 2 more methodsprocessOutputJsonObjectMapper
andprocessOutputYamlObjectMapper
to customize the "output mapper" (which is different from the one used while resolving, customizable with the existing methodprocessJsonObjectMapper
).Please see tests for an example of usage.
As quick background, many alternatives have been proposed to obtain a deterministic output, including but not limited to usage of sorted Maps and Sets at the POJO level, hard-coding Jackson sort features, providing specific serializers etc. but all options would have affected the deserialization->serialization roundtrip not allowing to maintain the original order. Additionally the order of output fields is and should be opinionated, as the semantic meaning stays the same.
Related tickets will be closed referencing this PR, any further issues in this area should be reported in a new or not closed ticket.