0

I'm having an issue with Swagger YAML and codegeneration.

I am trying to generate code from this very simple snippet:

swagger: '2.0'
info:
  version: 1.0.0
  title: Number API
host: localhost
basePath: /v2
schemes:
- http
paths:
  '/numbers':
    get:
      summary: Get number
      description: Returns a number
      operationId: getNumber
      produces:
      - application/json
      responses:
        '200':
          description: successful operation
          schema:
            $ref: '#/definitions/Numbers'
definitions:
  Numbers:
    $ref: '#/definitions/Number'
  Number:
    type: number

I execute the following command, using swagger-codegen-cli-2.2.3.jar:

java -jar swagger-codegen-cli-2.2.3.jar generate -i swagger.yaml -l jaxrs-spec -o ./

Now, the expected output is a very simple API implementation, where the response for the /numbers endpoint is a BigDecimal. I AM getting this, if I use Swagger Editor, which utilizes the Swagger generator.

However, using the command above, I get an API implementation, where the response tries to return a model called "Numbers". The model implementation looks like this:

package io.swagger.model;

import javax.validation.constraints.*;


import io.swagger.annotations.*;
import java.util.Objects;


public class Numbers   {



  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Numbers numbers = (Numbers) o;
    return true;
  }

  @Override
  public int hashCode() {
    return Objects.hash();
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Numbers {\n");

    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(java.lang.Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}

This is not correct, as the Numbers object should simply just translate to a BigDecimal, just like the Swagger Generator is doing..

Can anyone explain to me why this is happening, and what I can do to make the generator-cli behave as expected?

EDIT: I have looked at the JSON generated by both editor.swagger.io and codegen-cli. The JSON from the editor.swagger.io comes in the ZIP archive along with the generated code. It looks like this:

JSON output from Swagger editor JAXRS-SPEC:

{
  "swagger" : "2.0",
  "info" : {
    "version" : "1.0.0",
    "title" : "Number API"
  },
  "host" : "localhost",
  "basePath" : "/v2",
  "schemes" : [ "http" ],
  "paths" : {
    "/numbers" : {
      "get" : {
        "summary" : "Get number",
        "description" : "Returns a number",
        "operationId" : "getNumber",
        "produces" : [ "application/json" ],
        "parameters" : [ ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "type" : "number"
            }
          }
        }
      }
    }
  },
  "definitions" : {
    "Numbers" : {
      "type" : "number"
    },
    "Number" : {
      "type" : "number"
    }
  }
}

Whereas the JSON generated by codegen-cli looks like this:

JSON from codegen-cli v2.2.3, v2.3.0, github master and all other version of codegen-cli

{
  "swagger" : "2.0",
  "info" : {
    "version" : "1.0.0",
    "title" : "Number API"
  },
  "host" : "localhost",
  "basePath" : "/v2",
  "schemes" : [ "http" ],
  "paths" : {
    "/numbers" : {
      "get" : {
        "summary" : "Get number",
        "description" : "Returns a number",
        "operationId" : "getNumber",
        "produces" : [ "application/json" ],
        "parameters" : [ ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "$ref" : "#/definitions/Numbers"
            }
          }
        }
      }
    }
  },
  "definitions" : {
    "Numbers" : {
      "$ref" : "#/definitions/Number"
    },
    "Number" : {
      "type" : "number"
    }
  }
}

So in the Swagger editor version, the refs are translated into simple types, whereas the refs stays with the codegen-cli version.

Dentych
  • 106
  • 9
  • If you use the old version of the editor http://editor2.swagger.io/#!/ it generates the `Numbers` model, so my guess is that this has been changed on the latest version of swagger-codegen. Try and see if it works with the jar for 2.3.0 SNAPSHOT https://oss.sonatype.org/content/repositories/snapshots/io/swagger/swagger-codegen-cli/2.3.0-SNAPSHOT/ – moondaisy Jul 18 '17 at 18:25
  • @moondaisy latest version of 2.3.0-SNAPSHOT also generates the `Numbers` model... As far as I can see, the Swagger editor generates JSON where the $refs are being translated to type: number.. I will update my answer with the JSON I get from the codegen-cli and editor.swagger.io – Dentych Jul 19 '17 at 10:16
  • You might want to open an issue then – moondaisy Jul 19 '17 at 16:45

0 Answers0