0

I am unable to get swagger-codegen to generate classes for json schemas defined in files separate from the main API definition file. The command I am using is:

$ java -jar swagger-codegen-cli-2.2.2.jar generate -i api.json -l java -o gen -v

This is what api.json looks like:

{
"swagger": "2.0",
"info": {
    "version": "1.0.0",
    "title": "Simple API",
    "description": "A simple API to learn how to write OpenAPI Specification"
},
"schemes": [
    "https"
],
"host": "simple.api",
"basePath": "/openapi101",
"paths": {
    "/persons": {
        "get": {
            "summary": "Gets some persons",
            "description": "Returns a list containing all persons.",
            "responses": {
                "200": {
                    "description": "A list of Person",
                    "schema": {
                  "$ref" : "person.json#/definitions/person"                    

    }
                    }
                }
            }
        }
    }

}

The person.json file referenced here lives alongside api.json (i.e. at the same level) and contains the following:

{"definitions": {
“person”: {
  "type": "object",
  "description": "",
  "properties": {
    "requestId": {
      "type": "string",
      "example": "1234"
    }
  }
}}}

I would expect the code generation to generate a class called Person.java - but it does not - in fact it does not generate any model classes. Also the verbose logging logs the following right at the start which makes me think it is interpreting the reference incorrectly and for some reason prepending a #definitions to the $ref.

[main] INFO io.swagger.parser.Swagger20Parser - reading from api.json

{
  "swagger" : "2.0",
  "info" : {
    "description" : "A simple API to learn how to write OpenAPI Specification",
    "version" : "1.0.0",
    "title" : "Simple API"
  },
  "host" : "simple.api",
  "basePath" : "/openapi101",
  "schemes" : [ "https" ],
  "paths" : {
    "/persons" : {
      "get" : {
        "summary" : "Gets some persons",
        "description" : "Returns a list containing all persons.",
        "parameters" : [ ],
        "responses" : {
          "200" : {
            "description" : "A list of Person",
            "schema" : {
              "$ref" : "#/definitions/person.json#/definitions/person"
            }
          }
        }
      }
    }
  }
}

Anybody know what is going on here and what is the correct way to reference a schema definition that lives in a local file?

subodh
  • 337
  • 2
  • 6
  • 18
  • You might want to [open an issue](https://github.com/swagger-api/swagger-codegen/issues/new) in Swagger Codegen's GitHub repository. – Helen May 23 '17 at 19:15
  • Done: https://github.com/swagger-api/swagger-codegen/issues/5693 – subodh May 23 '17 at 19:30

1 Answers1

0

Adding a ./ to the $ref makes it work.

./person.json#/definitions/person
subodh
  • 337
  • 2
  • 6
  • 18