swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.02k stars 6.03k forks source link

[Java-Playframework][Core] Referenced models not in model list #6184

Open edrevo opened 7 years ago

edrevo commented 7 years ago
Description

We have bumped into a situation where a model ends up as an import to another model, but it does not show up in the list of CodegenModels. This happens because one of our models is an allOf which references a model called ModelError, but ModelError is not directly used anywhere in our api definition.

In the example below, ModelError ends as an import for InvalidArgument, but ModelError is not in the list of models.

I am tagging this as Java-Playframework because that's where I've seen the error surface, but it looks to me like a problem in DefaultCodegen

Swagger-codegen version

2.3.0-SNAPSHOT

Swagger declaration file content or url

File 1:

[...]
"/path": {
      "get": {
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "$ref": "#/definitions/ConsumptionsData"
            },
          "400": {
            "description": "Problem with the client request, in this case both OUT_OF_RANGE and INVALID_ARGUMENT are possible",
            "schema": {
              "$ref": "../common/errors.json#/definitions/InvalidArgument"
            },
            "examples": {
              "application/json": {
                "code": "OUT_OF_RANGE",
                "message": "Client specified an invalid range"
              }
            }
          }
        }
      }
    },
[...]

File 2:

{
  "definitions": {
    "ModelError": {
      "type": "object",
      "required": [
        "message"
      ],
      "properties": {
        "message": {
          "type": "string",
          "description": "A human readable description of what the event represent"
        }
      }
    },
    "InvalidArgument": {
      "allOf": [
        {
          "type": "object",
          "required": [
            "code"
          ],
          "properties": {
            "code": {
              "type": "string",
              "enum": [
                "INVALID_ARGUMENT"
              ],
              "default": "INVALID_ARGUMENT",
              "description": "Client specified an invalid argument, request body or query param."
            }
          }
        },
        {
          "$ref": "#/definitions/ModelError"
        }
      ]
    }
}
tzimisce012 commented 7 years ago

If a referenced model is not used as response in the swagger json it will not be loaded in the definitions object of swagger.

So, in line 1359 from DefaultCodegen, the interfaceModel will be set as null, so the properties will not be set in our child model. (lines 1370 to 1375).

Still, the model will be imported on line 1369.

tzimisce012 commented 7 years ago

Eventually, this bug comes from the Swagger Parser. Open issue on https://github.com/swagger-api/swagger-parser/issues/494

tzimisce012 commented 7 years ago

PR done on swagger parser https://github.com/swagger-api/swagger-parser/pull/495#partial-pull-merging

When the merge is done, the new version should be added to the swagger codegen and it will fix this bug

tzimisce012 commented 7 years ago

The PR have been merged on the swagger-parser project so it means that when you add the last jar as dependency the bug will be fixed. As soon as the last version will be available I will test it.