Search code examples
javaswaggerswagger-codegen

Swagger generating duplicated model classes


Generating server code for this minimal example using spring or jaxrs-spec creates 2 model classes: Pet.java and InlineResposne200.java. Except for the class name, they are identical.

The controller that responds to /pets returns a List<InlineResponse200> instead of List<Pet> and the class Pet is actually never used anywhere, even though the yaml definition uses $ref: "#/definitions/Pet". Why is this happening?

---
  swagger: "2.0"
  info: 
    version: "1.0.0"
    title: "Swagger Petstore"
    description: "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification"
    termsOfService: "http://swagger.io/terms/"
    contact: 
      name: "Swagger API Team"
    license: 
      name: "MIT"
  host: "petstore.swagger.io"
  basePath: "/api"
  schemes: 
    - "http"
  consumes: 
    - "application/json"
  produces: 
    - "application/json"
  paths: 
    /pets: 
      get: 
        description: "Returns all pets from the system that the user has access to"
        produces: 
          - "application/json"
        responses: 
          "200":
            description: "A list of pets."
            schema: 
              type: "array"
              items: 
                $ref: "#/definitions/Pet"
  definitions: 
    Pet: 
      type: "object"
      required: 
        - "id"
        - "name"
      properties: 
        id: 
          type: "integer"
          format: "int64"
        name: 
          type: "string"
        tag: 
          type: "string"

Solution

  • I tried replicating this with swagger-codegen v2.2.2 using both spring and jaxrs-spec, but I was not able to get the InlineResponse200.java.

    However, having done some more digging, I have found that this issue was recently reported as a bug. It has not yet been fixed, but the InlineResponse200.java isn't meant to be there.

    This file seems to be incorrectly generated when using Swagger Editor v3. I'm guessing that's how you generated your file?

    Whilst Swagger Editor v2 does not have this problem, my suggestion for the time being for you would be to install and use the latest stable version of swagger-codegen to generate your code from your Swagger.yaml file.

    Hope this helps.