Skip to content

@JsonSubTypes annotation conflicts with @Schema and anyOf #2872

@erbrecht

Description

@erbrecht

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions