-
-
Notifications
You must be signed in to change notification settings - Fork 548
Description
Describe the bug
It's not possible to generate a polymorphic oneOf
schema using @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
and @JsonSubTypes
.
I created a test case to showcase my problem and expected behavior: Test case
Description:
I want to generate a polymorphic schema with oneOf
(See https://swagger.io/docs/specification/v3_0/data-models/inheritance-and-polymorphism/).
I tried using @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
and @JsonSubTypes
annotations for this. However this seems to generate a schema relying on model composition with allOf
.
To relieve this, I tried manually adding oneOf
info to the @Schema
annotation but this produces an invalid schema where the children are missing the type
property and there is a circular ´oneOf-
allOf` relationship which seems weird (unsure if it's within OpenAPI spec).
If I don't use these jackson annotations and instead add the type
property manually to all child classes and manually maintain the parent @Schema
annotation content, I get the expected generated schema but I DO want to use jackson. (Workaround is basically this test case test.org.springdoc.api.v30.app243
)
To Reproduce
See test case here: https://github.com/MoellJ/springdoc-openapi/blob/3103-showcase-oneOf-problem/springdoc-openapi-starter-webmvc-api/src/test/java/test/org/springdoc/api/v30/app244/HelloController.java
(Based on test.org.springdoc.api.v30.app243
)
Issue also exists for v3.1 and doesn't differ as far as I can tell, so I didn't add a test case for this (yet)
Expected behavior
See linked test case above
Additional context
As far as I can tell, this issue is caused by a schema that suffers from the same issue: OpenAPITools/openapi-generator#19261
So evidentially there are some libraries that can not work with this. Unsure if this is an issue on their side or if this is simply not within spec
Possible fixes
- In case there already is a way to achieve the desired behavior (different config options?): We should add a test case for this and improve documentation
- Change default generator behavior of
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
and@JsonSubTypes
to output polymorphic schema (oneOf
) instead of composite schema (allOf
) (Will change current behavior) - Introduce a config option to manually toggle this behavior between polymorphic and composite
- Automatically toggle the behavior if a manually added
oneOf
relationship in the@Schema
annotation of the Parent class was detected - Other - maintainers probably have better ideas than me
I'd be open to work on this, if we agree on the a solution