swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Discriminator property in component disappears from component when mapping is used under discriminator

Open Numerlor opened this issue 3 years ago • 3 comments

When I have the discriminator property explicitly listed on the components that are under a oneOf, it disappears when mapping is used under discriminator.

For example with the following document, the example is

{
  "prop2": "string"
}

but with mapping removed it changes into

{
  "prop2": "string",
  "type": "string"
}
OpenAPI document
openapi: 3.0.3
info:
  title: ''
  version: 0.0.0
paths:
  /test/:
    post:
      operationId: test_create
      tags:
        - events
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TestPolymorphic'
      responses:
        '200':
          description: ''
components:
  schemas:
    Test1:
      type: object
      properties:
        prop1:
          type: string
        type:
          type: string
      required:
        - prop1
        - type
        
    Test2:
      type: object
      properties:
        prop2:
          type: string
        type:
          type: string
      required:
        - prop2
        - type
        

    TestPolymorphic:
      oneOf:
        - $ref: '#/components/schemas/Test2'
        - $ref: '#/components/schemas/Test1'
      discriminator:
        propertyName: type
        mapping:
          Test2: '#/components/schemas/Test2'
          Test1: '#/components/schemas/Test1'

Numerlor avatar Jan 06 '23 10:01 Numerlor

Hi! Did you find a solution to this?

Laikos38 avatar Jul 27 '23 18:07 Laikos38

Hi! Did you find a solution to this?

Just went with Redoc

Numerlor avatar Jul 28 '23 11:07 Numerlor

Just adding a bit more details:

  • The issue started since v4.15.0 and is still present till now (v5.30.3)
  • One workaround is to inline the TestPolymorphic schema so that the discriminator appears in the example. So, the following generates an example properly
Discriminator prop appears in the example

openapi: 3.0.3
info:
  title: ''
  version: 0.0.0
paths:
  /test/:
    post:
      operationId: test_create
      tags:
        - events
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Test2'
                - $ref: '#/components/schemas/Test1'
              discriminator:
                propertyName: type
                mapping:
                  Test2: '#/components/schemas/Test2'
                  Test1: '#/components/schemas/Test1'
      responses:
        '200':
          description: ''
components:
  schemas:
    Test1:
      type: object
      properties:
        prop1:
          type: string
        type:
          type: string
      required:
        - prop1
        - type
        
    Test2:
      type: object
      properties:
        prop2:
          type: string
        type:
          type: string
      required:
        - prop2
        - type

ghazi-git avatar Dec 09 '25 07:12 ghazi-git