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

Discriminator omitted when used in root level of composed schema #1269

Open
spacether opened this issue Dec 8, 2019 · 2 comments
Open

Discriminator omitted when used in root level of composed schema #1269

spacether opened this issue Dec 8, 2019 · 2 comments

Comments

@spacether
Copy link
Contributor

When using a discriminator in a composed schema model, the discriminator is omitted when the schema is parsed.

Given the following spec:

swagger: "2.0"
info:
  description: "This is a sample server Petstore server.  You can find out more about     Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).      For this sample, you can use the api key `special-key` to test the authorization     filters."
  version: "1.0.0"
  title: "Swagger Petstore"
  termsOfService: "http://swagger.io/terms/"
  contact:
    email: "[email protected]"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "petstore.swagger.io"
basePath: "/v2"
tags:
- name: "pet"
  description: "Everything about your Pets"
  externalDocs:
    description: "Find out more"
    url: "http://swagger.io"
- name: "store"
  description: "Access to Petstore orders"
- name: "user"
  description: "Operations about user"
  externalDocs:
    description: "Find out more about our store"
    url: "http://swagger.io"
schemes:
- "https"
paths:
  /pet:
    post:
      tags:
      - "pet"
      summary: "Add a new pet to the store"
      description: ""
      operationId: "addPet"
      consumes:
      - "application/json"
      - "application/xml"
      produces:
      - "application/xml"
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        description: "Pet object that needs to be added to the store"
        required: true
        schema:
          $ref: "#/definitions/ParentPet"
      responses:
        405:
          description: "Invalid input"
definitions:
  GrandparentAnimal:
    type: object
    required:
    - pet_type
    properties:
      pet_type:
        type: string
  ParentPet:
    type: object
    allOf:
    - $ref: '#/definitions/GrandparentAnimal'
    discriminator: pet_type
  ChildCat:
    allOf:
    - $ref: '#/definitions/ParentPet'
    - type: object
      properties:
        name:
          type: string
  ChildDog:
    allOf:
    - $ref: '#/definitions/ParentPet'
    - type: object
      properties:
        bark:
          type: string
  ChildLizard:
    allOf:
    - $ref: '#/definitions/ParentPet'
    - type: object
      properties:
        lovesRocks:
          type: boolean
  Dog:
    allOf:
      - $ref: '#/definitions/Animal'
      - type: object
        properties:
          breed:
            type: string
  Cat:
    allOf:
      - $ref: '#/definitions/Animal'
      - type: object
        properties:
          declawed:
            type: boolean
  Animal:
    type: object
    discriminator: className
    required:
      - className
    properties:
      className:
        type: string
      color:
        type: string
        default: 'red'

For Animal we see the discriminator in the schema:

class ObjectSchema {
    class Schema {
        type: object
        format: null
        $ref: null
        description: null
        title: null
        multipleOf: null
        maximum: null
        exclusiveMaximum: null
        minimum: null
        exclusiveMinimum: null
        maxLength: null
        minLength: null
        pattern: null
        maxItems: null
        minItems: null
        uniqueItems: null
        maxProperties: null
        minProperties: null
        required: [className]
        not: null
        properties: {className=class StringSchema {
            class Schema {
                type: string
                format: null
                $ref: null
                description: null
                title: null
                multipleOf: null
                maximum: null
                exclusiveMaximum: null
                minimum: null
                exclusiveMinimum: null
                maxLength: null
                minLength: null
                pattern: null
                maxItems: null
                minItems: null
                uniqueItems: null
                maxProperties: null
                minProperties: null
                required: null
                not: null
                properties: null
                additionalProperties: null
                nullable: null
                readOnly: null
                writeOnly: null
                example: null
                externalDocs: null
                deprecated: null
                discriminator: null
                xml: null
            }
        }, color=class StringSchema {
            class Schema {
                type: string
                format: null
                $ref: null
                description: null
                title: null
                multipleOf: null
                maximum: null
                exclusiveMaximum: null
                minimum: null
                exclusiveMinimum: null
                maxLength: null
                minLength: null
                pattern: null
                maxItems: null
                minItems: null
                uniqueItems: null
                maxProperties: null
                minProperties: null
                required: null
                not: null
                properties: null
                additionalProperties: null
                nullable: null
                readOnly: null
                writeOnly: null
                example: null
                externalDocs: null
                deprecated: null
                discriminator: null
                xml: null
            }
        }}
        additionalProperties: null
        nullable: null
        readOnly: null
        writeOnly: null
        example: null
        externalDocs: null
        deprecated: null
        discriminator: Discriminator{propertyName='className', mapping=null}
        xml: null
    }
}

But for ParentPet we do not see the discriminator set, even though it is included in our schema:

class ComposedSchema {
    class Schema {
        type: null
        format: null
        $ref: null
        description: null
        title: null
        multipleOf: null
        maximum: null
        exclusiveMaximum: null
        minimum: null
        exclusiveMinimum: null
        maxLength: null
        minLength: null
        pattern: null
        maxItems: null
        minItems: null
        uniqueItems: null
        maxProperties: null
        minProperties: null
        required: null
        not: null
        properties: null
        additionalProperties: null
        nullable: null
        readOnly: null
        writeOnly: null
        example: null
        externalDocs: null
        deprecated: null
        discriminator: null
        xml: null
    }
    allOf: [class Schema {
        type: null
        format: null
        $ref: #/components/schemas/GrandparentAnimal
        description: null
        title: null
        multipleOf: null
        maximum: null
        exclusiveMaximum: null
        minimum: null
        exclusiveMinimum: null
        maxLength: null
        minLength: null
        pattern: null
        maxItems: null
        minItems: null
        uniqueItems: null
        maxProperties: null
        minProperties: null
        required: null
        not: null
        properties: null
        additionalProperties: null
        nullable: null
        readOnly: null
        writeOnly: null
        example: null
        externalDocs: null
        deprecated: null
        discriminator: null
        xml: null
    }]
    anyOf: null
    oneOf: null
}

For composed schemas please include a discriminator if it is included in the schema.

@spacether spacether changed the title Discriminator omitted when used in composed schema Discriminator omitted when used in root level of composed schema Dec 8, 2019
@r-sreesaran
Copy link
Contributor

r-sreesaran commented Jan 18, 2020

The issue is with swagger 2.0 parser, where the parser fails to add the discriminator in ComposedModel.

@r-sreesaran
Copy link
Contributor

r-sreesaran commented Jan 18, 2020

The Composed Model class is missing discriminator field. Not sure as to why it was left out in the first place.

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

No branches or pull requests

2 participants