Swagger: variant schema shape dependant on field value

前端 未结 1 1998
青春惊慌失措
青春惊慌失措 2020-12-12 07:52

I have a model defined as:

Event:
  type: object
  properties:
    id:
      type: string
    timestamp:
      type:         


        
相关标签:
1条回答
  • 2020-12-12 08:22

    This is possible using model inheritance/composition and discriminator.

    First, define the base model Event that contains just the common properties, and mark the type property as discriminator:

    definitions:
      Event:
        type: object
        discriminator: type
        required:
          - type  # discriminator property is required
        properties:
          id:
            type: string
            example: ad1b12f0-63a8-47b5-9820-3e447143ce7e
          timestamp:
            type: string
            format: date-time
            example: 2017-09-29T16:45:20.000+00:00
          type:
            type: string
            enum:
              - click
              - open
              - sent
    

    Then inherit the click/open/sent models from the base Event model via allOf. The names of the child models must be the same as the values of the discriminator property. In your example, the models must be named click, open and sent. If you use Swagger UI, you can add title to override the displayed model names.

      click:
        title: ClickEvent
        allOf:
          - $ref: '#/definitions/Event'
          - type: object
            properties:
              click:
                type: object
                properties:
                  url:
                    type: string
                    example: www.some-website-somewhere.org/12345
                  title:
                    type: string
                    example: Click here!
    

    In operations, use the base model (Event) as the request or response schema:

    responses:
      200:
        description: OK
        schema:
          $ref: '#/definitions/Event'
    

    The clients should examine the value of the discriminator property (in this example - type) to decide which inherited model to use.

    Note for Swagger UI users: Swagger UI and Swagger Editor currently do not support switching models based on discriminator. Follow this issue for updates.


    In OpenAPI 3.0 it's easier - you can simply use oneOf to define alternate schemas for a request or response.

    responses:
      '200':
        description: OK
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/ClickEvent'
                - $ref: '#/components/schemas/OpenEvent'
                - $ref: '#/components/schemas/SentEvent'
              # Optional mapping of `type` values to models
              discriminator:
                propertyName: type
                mapping:
                  click: '#/components/schemas/ClickEvent'
                  open:  '#/components/schemas/OpenEvent'
                  sent:  '#/components/schemas/SentEvent'
    
    0 讨论(0)
提交回复
热议问题