Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inheritance not recognized by Swagger Codegen #1136

Closed
kajaj opened this issue Jan 9, 2018 · 4 comments
Closed

Inheritance not recognized by Swagger Codegen #1136

kajaj opened this issue Jan 9, 2018 · 4 comments
Milestone

Comments

@kajaj
Copy link

kajaj commented Jan 9, 2018

I want to have automated builds of clients for my Swagger API in several languages. Swagger Codegen allows me to do that, but if I use the swagger.json that is generated by Nswag, I only get properties of the base class in the generated model. The generated model was as follows:

    "Animal": {
      "type": "object",
      "discriminator": "discriminator",
      "additionalProperties": false,
      "required": [
        "discriminator"
      ],
      "properties": {
        "Foo": {
          "type": "string"
        },
        "discriminator": {
          "type": "string"
        }
      }
    },
    "Dog": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "Bar": {
          "type": "string"
        }
      },
      "allOf": [
        {
          "$ref": "#/definitions/Animal"
        }
      ]
    }
  }

Alongside using my code, I have tested the NSwag.Demo.Web swagger.json and the result was the same. I have then decided to figure out what is wrong and discovered, that in case of inheritance both base class and the properties must be listed under allOf (https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/). It lists this is valid for OpenAPI 3.0, but this has been so in OpenAPI 2.0 as well (https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md):

  "definitions": {
    "ErrorModel": {
      "type": "object",
      "required": [
        "message",
        "code"
      ],
      "properties": {
        "message": {
          "type": "string"
        },
        "code": {
          "type": "integer",
          "minimum": 100,
          "maximum": 600
        }
      }
    },
    "ExtendedErrorModel": {
      "allOf": [
        {
          "$ref": "#/definitions/ErrorModel"
        },
        {
          "type": "object",
          "required": [
            "rootCause"
          ],
          "properties": {
            "rootCause": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
}

To check if this is true I have then written a simple script that moves the properties to allOf field if allOf field is defined and the generated clients had all the specified fields listed correctly.

I have tried using both Swagger-codegen.jar as well as the webpage that does the same (http://editor.swagger.io/).

@RicoSuter
Copy link
Owner

The question here is which tool has a bug. NSwag can handle both types of property locations when generating client code, but it seems that the others only generate properties when they are in allOf. The question is, whether NSwag is generating invalid specs or the other tools do not support it correctly?

@kajaj
Copy link
Author

kajaj commented Jan 10, 2018

If you look at the OpenAPI specification and the inheritance examples there, I would say the bug is in NSwag. I have also looked at a couple of other people implementing inheritance (top hits on google for swagger inheritance) and they were all using it as it is in OpenAPI example.

@RicoSuter RicoSuter added this to the vNext milestone Feb 1, 2018
@RicoSuter
Copy link
Owner

@RicoSuter
Copy link
Owner

Fixed in latest version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants