swagger-api / swagger-api/swagger-core
Consider the @JsonSubTypes for generating the discriminator mapping
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 7.5k
- Forks
- 2.3k
- Avg merge
- 18h 1m
- Merged PRs (30d)
- 10
Description
To generate the appropriate mapping for a discriminator of a parent class, we are required to add the discriminatorMapping property to @Schema, which is almost always redundant if the class already has a @JsonSubTypes annotation.
As the discriminatorProperty is read from the @JsonTypeInfo.property can the discriminatorMappings also be read from the JsonSubTypes if available?
As a workaround, I have registered a CustomModelResolver and extended the method resolveDiscriminator as follows,
@Override
protected Discriminator resolveDiscriminator(JavaType type, ModelConverterContext context) {
Discriminator discriminator = super.resolveDiscriminator(type, context);
if (discriminator != null && discriminator.getPropertyName() != null &&
(discriminator.getMapping() == null || discriminator.getMapping().isEmpty())) {
JsonSubTypes jsonSubTypes = type.getRawClass().getDeclaredAnnotation(JsonSubTypes.class);
if (jsonSubTypes != null) {
Arrays.stream(jsonSubTypes.value()).forEach(subtype -> {
discriminator.mapping(subtype.name(), RefUtils.constructRef(
context.resolve(new AnnotatedType().type(subtype.value())).getName()));
});
}
}
return discriminator;
}
Additional Info: Using the swagger-maven-plugin v2.1.1 for generating the OAS files during the compile phase.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the resolveDiscriminator entry point and compare its current behavior with the CustomModelResolver workaround shown in the issue. Check how JsonTypeInfo and JsonSubTypes are read, then use the swagger-maven-plugin compile phase to generate an OAS file and verify that discriminator mappings are produced without redundant @Schema configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100