Description
The current version of swagger-codegen
generates inconsistent discriminator mappings for Jackson's JsonSubTypes
annotations for different Java modules. I'm currently using swagger-codegen
to generate model classes, REST controller interfaces using OpenAPI 3.0 definitions. We're using the maven plugin generator facility in order to generate both Spring controllers, as well as java-clients using resttemplate
library. However, we've noticed inconsistencies between SpringCodegen
classes and JavaClientCodegen
classes for the same API definitions.
Versions
This is currently happening on 3.0.23
, both on Maven plugin & CLI.
Reproduction Details
The following API definition was used to reproduce this issue
openapi: '3.0.3'
info:
title: 'Discriminator Problem API'
version: '1.0.0'
components:
schemas:
SomeTypeDTO:
type: string
enum:
- SubTypeA
- WeirdlyNamedSubTypeB
ResultForSubTypeDTO:
type: object
properties:
type:
type: string
oneOf:
- $ref: '#/components/schemas/SubTypeAResultDTO'
- $ref: '#/components/schemas/SubTypeBResultDTO'
required:
- type
discriminator:
propertyName: type
mapping:
SubTypeA: '#/components/schemas/SubTypeAResultDTO'
WeirdlyNamedSubTypeB: '#/components/schemas/SubTypeBResultDTO'
SubTypeAResultDTO:
allOf:
- $ref: '#/components/schemas/ResultForSubTypeDTO'
type: object
properties:
some_attribute:
type: string
SubTypeBResultDTO:
allOf:
- $ref: '#/components/schemas/ResultForSubTypeDTO'
type: object
properties:
another_attribute:
type: string
paths:
/repro:
get:
operationId: 'getRepro'
responses:
204:
description: OK
Execution
The testing was grounded on using the CLI, triggered for Java language (-l java
+ resttemplate
library) and Spring (-l spring
).
Output
Spring
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2020-11-16T16:49:29.361700Z[Europe/Lisbon]")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true )
@JsonSubTypes({
@JsonSubTypes.Type(value = SubTypeAResultDTO.class, name = "SubTypeA"),
@JsonSubTypes.Type(value = SubTypeBResultDTO.class, name = "WeirdlyNamedSubTypeB"),
})
public class ResultForSubTypeDTO implements Serializable , OneOfResultForSubTypeDTO {
// Redacted
}
Works as expected, having the SubTypeAResultDTO
and SubTypeBResultDTO
classes properly mapped to its discriminator definition.
Java Client
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2020-11-16T16:39:57.004152Z[Europe/Lisbon]")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true )
@JsonSubTypes({
@JsonSubTypes.Type(value = SubTypeAResultDTO.class, name = "SubTypeAResultDTO"),
@JsonSubTypes.Type(value = SubTypeBResultDTO.class, name = "SubTypeBResultDTO"),
})
public class ResultForSubTypeDTO implements Serializable, OneOfResultForSubTypeDTO {
// Redacted
}
Generates erroneous code, discarding the existing discriminator mapping for SubTypeAResultDTOand
SubTypeBResultDTO` classes.
Expected
The expected behavior is that the generation for the Java Client code should contain the correct discriminator mapping, as following
@JsonSubTypes({
@JsonSubTypes.Type(value = SubTypeAResultDTO.class, name = "SubTypeA"),
@JsonSubTypes.Type(value = SubTypeBResultDTO.class, name = "WeirdlyNamedSubTypeB"),
})
Note: This is a similar issue introduced at #602, but the PR (#603) related to that issue only fixed the problem for Spring, leaving other Java modules outdated, such as Micronaut, JavaClient or JAXRS.