test: Add test showing how @JsonSubTypes can be used with @Schema-oneOf annotations #5030
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.
Description
TL;DR: I have introduced a test showing how one can circumvent the fact that a
@JsonSubtypeswill always produce anallOfpolymorphic structure (that also breaks if aoneOf@Schemais specified at the same time). This is a reoccurring questions I get, so I would like to collect the solution in a location where it can have two purposes (showing the behavior of theModelResolver, but also at the same time being a reference that I can share for how to adjust for the scenario).I have many times had discussions regarding how polymorphic models can be constructed/expressed when exposing an OpenApi specification by using springdoc (which relies on swagger-core). Some examples being springdoc/springdoc-openapi#3054 and springdoc/springdoc-openapi#3103.
The scenario is that
@JsonSubtypeshas to be used to guide Jackson in the deserialization process, but the specification provider prefers to expose the polymorphism with aoneOfstructure rather than aallOfstructure. My current understanding is that this can be realized by having the parent class own the@JsonSubTypes, while it also implements an interface carrying a@Schemaannotation containing aoneOfdefinition.My preferred solution would be that it should be configurable whether the
@JsonSubTypesis processed (I.e., an explicit@Schemaannotation should take precedence over it). I know that this can be realized by providing a custom ModelResolver, but given its current size it is cumbersome to override it. I would have preferred if theModelResolverhad an isolated method responsible for it, so I could just define aModelResolverthat overrides only that method.I understand that this is a difficult thing to achieve, so thus I elect to add illustrative tests to show the current behavior before I attempt anything. This since it is helpful to add tests for common use-cases even if I do not even introduce any change in behavior with a later PR.
Type of Change
Checklist
Screenshots / Additional Context