Swagger-codegen: [Java] Support inheritance (OAS3: discriminator/mapping element)

Created on 12 Dec 2018  路  1Comment  路  Source: swagger-api/swagger-codegen

Description

Mapping element doesn't appear to be supported in io.swagger.codegen.v3:swagger-codegen-maven-plugin:3.0.3 plugin.

I have the following swagger (extract):

    Vehicle:
      type: object
      discriminator:
        propertyName: type
        mapping:
          car: '#/components/schemas/Car'
          truck: '#/components/schemas/Truck'
      properties:
        model:
          type: string
...

And the following class is generated:

@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2018-12-12T10:48:45.405+01:00[Europe/Paris]")@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "Discriminator{propertyName='type', mapping={car=#/components/schemas/Car, truck=#/components/schemas/Truck}}", visible = true )
@JsonSubTypes({
  @JsonSubTypes.Type(value = Car.class, name = "Car"),
  @JsonSubTypes.Type(value = Truck.class, name = "Truck"),
})
public class Vehicle {

Whereas, I would expect to find my swagger mappings in the name attribute, something like:

@JsonSubTypes.Type(value = Car.class, name = "car"),
@JsonSubTypes.Type(value = Truck.class, name = "truck"),

Specification link:
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#discriminatorObject

Sample project showing the issue: https://github.com/gonzalad/swagger-samples/tree/swagger-openapi3-inheritance/client

Swagger-codegen version

3.0.3

Swagger declaration file content or url

https://gist.github.com/gonzalad/1953b3c504240112bfc11241eb5afa38

Command line used for generation
            <plugin>
                <groupId>io.swagger.codegen.v3</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>3.0.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/swagger.yml</inputSpec>
                            <language>java</language>
                            <configOptions>
                                <library>resttemplate</library>
                                <!--
                                <sourceFolder>src/gen/java/main</sourceFolder>
                                    -->
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce
  • run swaggercodegen on the linked swagger.yml file

Thanks,
Adrian

Most helpful comment

Note that you can get it working with using the vendor extension x-discriminator-value on the sub type.

>All comments

Note that you can get it working with using the vendor extension x-discriminator-value on the sub type.

Was this page helpful?
0 / 5 - 0 ratings