Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@JsonSubTypes annotation conflicts with @Schema and anyOf #2872

Open
erbrecht opened this issue Jan 21, 2025 · 3 comments
Open

@JsonSubTypes annotation conflicts with @Schema and anyOf #2872

erbrecht opened this issue Jan 21, 2025 · 3 comments

Comments

@erbrecht
Copy link

erbrecht commented Jan 21, 2025

Describe the bug
When I use @JsonSubTypes and @Schema(oneOf = {...}) on an interface that has at least one implementation, I get an error when loading the SpringDoc UI and expanding the schema definition for the interface. The schema definition for the implementation also shows an allOf property even though I haven't configured the schema for any of the implementations.

If I remove the @JsonSubTypes annotation, everything works.

This is similar to #2655.

To Reproduce
I created a simple application:

https://github.com/erbrecht/spring-doc-json-subtypes/tree/master

To reproduce, uncomment the @JsonSubTypes annotation in

https://github.com/erbrecht/spring-doc-json-subtypes/blob/master/src/main/java/org/example/springdocjsonsubtypes/dto/TermDto.java#L8

  • Spring Boot 3.4.1
  • SpringDoc 2.8.3

Expected behavior
I would expect no errors to show up in the spring doc ui.

I would also expect my implementation class NOT to have the allOf property defined in the swagger definition.

Screenshots
Errors:

Image

"Bad" schema:

Image

"Good" schema:

Image

Additional context
I'm also not seeing a specific class for the implementations in the schema definition. I just see an array index. I do see these defined in previous versions of spring doc, eg 2.6.0. I'm not sure if this is related, but it seemed worth mentioning.

spring doc 2.6.0, no @JsonSubType annotation:

Image

spring doc 2.6.0, WITH @JsonSubType annotation:

Image

spring doc 2.8.3, no @JsonSubType annotations:

Image

spring doc 2.8.3, WITH @JsonSubType annotations:

Image

@Mattias-Sehlstedt
Copy link

My understanding is that the allOf annotation comes from swagger-core resolving the Json-annotations https://github.com/swagger-api/swagger-core/blob/master/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java#L1637. I.e., they have implemented it so that the Json-annotation is enough to yield a proper polymorphic OpenAPI schema (but adding a custom Schema annotation is not treated as an override, but they are both rather considered at the same time, which produces an invalid recursive specification).

But I definitely see the usecase for letting only the Schema annotation decide the OpenAPI format, and that the Json-annotation is only used to guide the deserialization.

This issue looks to ask the same question swagger-api/swagger-core#3677

@erbrecht
Copy link
Author

@Mattias-Sehlstedt This is very interesting, thank you for that info. If I leave the JsonSubType annotations, but remove the Schema annotation, things seem to work mostly as I expect. I'm seeing the possible types listed in the schema in the endpoint definition, just not in the schema listed at the bottom of the UI.

The schema still doesn't specify the possible classes though. I updated my sample code to remove the schema annotation and just rely on the json sub type annotations. For the following screenshots, I only changed the spring doc version from 2.6.0 to 2.8.3. Is there something else I'm missing, or is this the new behavior of version 2.8.3?

Actual implementation classes listed in schema with version 2.6.0:

Image

Array index instead of class name with version 2.8.3:

Image

@Mattias-Sehlstedt
Copy link

The reason for the visualization I have covered in this comment #2867 (comment), and in a later comment in that discussion I given the solution for how you can retain the old way of visualizing while still keeping the springdoc-openapi version update (do note that "the old way of visualizing" in this context implies that you expose a 3.0 OpenAPI specification instead of a 3.1 specification).

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

No branches or pull requests

2 participants