Redocly / redoc

📘 OpenAPI/Swagger-generated API Reference Documentation
https://redocly.github.io/redoc/
MIT License
22.87k stars 2.26k forks source link

OpenAPI 3 with discriminator & mapping fields #1076

Open gigaga opened 4 years ago

gigaga commented 4 years ago

Hi,

Firstly, congratulations for your tool. It is really the best viewer that I could have used!

From redoc v2.0.0-rc.18, I have a problem with discriminator & mapping fields of OpenAPI 3 Indeed, below, we can see my screenshot from the following specs file:

openapi: 3.0.0

########## INFO ##########
info:
  title: Test
  description: Specifications of test bounded context
  version: 1.0.0

paths:
  /pet:
    put:
      tags:
      - Pet
      description: Test
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Pet"
        description: Capture that has to be applied to transaction
      responses:
        "200":
          description: The transaction captured.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"
components:
  schemas:
    Pet:
      type: object
      required:
      - pet_type
      properties:
        pet_type:
          type: string
      discriminator:
        propertyName: pet_type
        mapping:
          dog: "Dog"
          cat: "Cat"
          lizard: "Lizard"
    Dog:
      allOf:
      - $ref: "#/components/schemas/Pet"
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: string
    Cat:
      allOf:
      - $ref: "#/components/schemas/Pet"
      - type: object
        # all other properties specific to a `Cat`
        properties:
          name:
            type: string
    Lizard:
      allOf:
      - $ref: "#/components/schemas/Pet"
      - type: object
        # all other properties specific to a `Lizard`
        properties:
          lovesRocks:
            type: boolean

Capture

mohdzohlof commented 4 years ago

You are using invalid references, your discriminator should look something like this:

discriminator:
  propertyName: pet_type
  mapping:
    dog: "#/components/schemas/Dog"
    cat: "#/components/schemas/Cat"
    lizard: "#/components/schemas/Lizard"
gigaga commented 4 years ago

Thanks @mohdzohlof. Indeed like that but it should work in implicit way too (https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#discriminator-object). Also, by using mapping in external reference context, it does not work too :(

mohdzohlof commented 4 years ago

It works fine when using full URI reference

Screenshot from 2019-11-22 12-52-18

openapi: 3.0.0

########## INFO ##########
info:
  title: Test
  description: Specifications of test bounded context
  version: 1.0.0

paths:
  /pet:
    put:
      tags:
        - Pet
      description: Test
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Pet"
        description: Capture that has to be applied to transaction
      responses:
        "200":
          description: The transaction captured.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"
components:
  schemas:
    Pet:
      type: object
      required:
        - pet_type
      properties:
        pet_type:
          type: string
      discriminator:
        propertyName: pet_type
        mapping:
          dog: "#/components/schemas/Dog"
          cat: "#/components/schemas/Cat"
          lizard: "#/components/schemas/Lizard"
    Dog:
      allOf:
        - $ref: "#/components/schemas/Pet"
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: string
    Cat:
      allOf:
        - $ref: "#/components/schemas/Pet"
        - type: object
          # all other properties specific to a `Cat`
          properties:
            name:
              type: string
    Lizard:
      allOf:
        - $ref: "#/components/schemas/Pet"
        - type: object
          # all other properties specific to a `Lizard`
          properties:
            lovesRocks:
              type: boolean
gigaga commented 4 years ago

Hello,

Try it by using external file like this : pet.yaml#/Pet

Best regards,

mohdzohlof commented 4 years ago

Yes that's a bug in the tool, check #862 and check https://github.com/Redocly/redoc/issues/862#issuecomment-491631228 for a workaround