cheb1k4
cheb1k4

Reputation: 2464

Generate java classes using swagger and yaml

I would like to generate my Java classes using the maven plugin swagger-codegen-maven-plugin version 2.2.3. Here my pom.xml file with the configuration:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.2.3</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${basedir}/src/main/resources/swagger/project.yaml</inputSpec>
                <language>java</language>
                <configOptions>
                    <sourceFolder>src/gen/java/main</sourceFolder>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

My project.yaml file contains this:

definitions:
    Parent:
        type: "object"
        discriminator: "type"
        required:
            - type
        properties:
            id:
                type: "integer"
                format: "int64"
            code:
                type: "string"
   ChildA:
       allOf:
           - $ref: "#/definitions/Parent"
           - properties:
                 attributeA:
                     type: "string"
   ChildB:
       allOf:
           - $ref: "#/definitions/Parent"
           - properties:
                 attributeB:
                     type: "string"

All the 3 classes are generated and then I want to create ChildA or ChildB using a web service. So my method is:

@POST
public Response createChild(@WebParam Parent parent) {
    ...
}

Using Postman, I sent the following json in order to create an ChildA instance:

{
    "code": "child-a",
    "attributeA": "value"
}

The following exception happens:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "attributeA" (class io.swagger.client.model.Parent), not marked as ignorable (2 known properties: "code", "id"])
    at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@1df2f416; line: 3, column: 17] (through reference chain: io.swagger.client.model.Parent["attributeA"])

I read at several places that I need some annotation in my Parent class like:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @Type(value = ChildA.class, name = "ChildA"),
    @Type(value = ChildB.class, name = "ChildB" ) })

But I don't know how to modify my yaml file to add those annotations. Can someone helps me?

Upvotes: 1

Views: 37524

Answers (1)

cheb1k4
cheb1k4

Reputation: 2464

I found the solution (not thanks to swagger documentation unfortunately). In the configuration of the plugin in my pom.xml, <library>resteasy</library> was missing. The full configuration is now:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.2.3</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${basedir}/src/main/resources/swagger/project.yaml</inputSpec>
                <language>java</language>
                <configOptions>
                    <sourceFolder>src/gen/java/main</sourceFolder>
                    <library>resteasy</library>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

Upvotes: 5

Related Questions