Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

"oneOf" from superclass schema is propagated to subclasses unexpectedly #3054

Discussion options

Hello ~ I have a question about oneOf propagation in a superclass–subclass relationship.

Version

  • Java 17
  • org.springframework.boot' version '3.5.4' // 'org.springframework.boot:spring-boot-starter-web'
  • org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.9

My Code

//--- Cat.java
@JsonTypeName("cat")
@Schema(description = "it's a cat")
public class Cat extends Pet {
 private double cuteness;
}
//--- Dog.java
@JsonTypeName("dog")
@Schema(description = "it's a dog")
public class Dog extends Pet {
 private String barkSound;
}
//--- Pet.java 
@Schema(
 description = "it's super pet",
 discriminatorProperty = "petType",
 oneOf = { Cat.class, Dog.class },
 requiredProperties = {"petType"}
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "petType")
@JsonSubTypes({
 @JsonSubTypes.Type(value = Cat.class, name = "cat"),
 @JsonSubTypes.Type(value = Dog.class, name = "dog")
})
public abstract class Pet {
 private String name;
 private int age;
 private String petType;
}
//--- PetController.java
@GetMapping("/pets", produces = MediaType.APPLICATION_JSON_VALUE))
public List<Pet> getPets() { ... }

Result

When I open /swagger-ui.html and check the UI:

  • PetoneOf(Cat, Dog) ✅ (correct for polymorphism)
  • But in the schema UI, Cat and Dog also appear as oneOf siblings (misleading, because they are subclasses of Pet, not interchangeable with each other directly).
CatDog Pet

Problem

This may confuse API consumers, because it looks like Cat and Dog have a direct polymorphic relation between each other, when in fact they only share a parent class.

Question

I tried to find any similar issues or discussions about this behavior but couldn’t find one.
Is this:

  1. A known/expected behavior?
  2. A bug in springdoc-openapi?
  3. Or something that is documented somewhere but I missed?
You must be logged in to vote

Hi @YoungHoney,

This is an issue I am familiar with, it has been discussed here. It has to do with how swagger-core introspects. It both parses the @JsonSubTypes annotation but also the @Schema annotation, meaning that the generated OpenApi object tries to express two different ways of representing polymorphism.

There are several different solutions to this, with the main thing being that you need to not have both annotations on the same object.

Replies: 1 comment 1 reply

Comment options

Hi @YoungHoney,

This is an issue I am familiar with, it has been discussed here. It has to do with how swagger-core introspects. It both parses the @JsonSubTypes annotation but also the @Schema annotation, meaning that the generated OpenApi object tries to express two different ways of representing polymorphism.

There are several different solutions to this, with the main thing being that you need to not have both annotations on the same object.

You must be logged in to vote
1 reply
Comment options

Thanks for the clarification!
I’ll make sure to check out the issue you mentioned.

Answer selected by YoungHoney
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /