Can a swagger object passed as a parameter have default values in swagger-ui?

后端 未结 1 1303
滥情空心
滥情空心 2021-02-15 00:01

I define a path that takes MyObject as a parameter. MyObject has properties for cats and dogs. These have default values. In swagger-editor, the example doesn\'t show the defaul

1条回答
  •  囚心锁ツ
    2021-02-15 00:36

    It seems like there are 2 kinds of defaults:

    • server-side: variable is not required and server will assume a value for it if it is not given definition from OpenApi v3.0 spec
    • client side: variable is required and must be only one value (for example headers)

    For a client-side default, we can define it by setting required=True and enum to the only allowed value. See this example below:

    swagger: "2.0"
    info:
      title: "some api"
      description: "a description"
      version: "1.0.0"
    host: "example.com"
    basePath: "/api"
    schemes:
    - "http"
    paths:
      /myobject:
         post:
          summary: |
            post an object.
          parameters:
            - name: myObject
              in: body
              required: true
              schema:
                type: array
                items:
                  $ref: '#/definitions/MyObject'
          responses:
            200:
              description: OK
    definitions:
      MyObject:
          type: object
          description: Contains default properties
          required:
            - cats
            - dogs
          properties:
            cats:
              type: number
              enum:
                - 9
            dogs:
              type: string
              enum:
                - fido
    
    And you can see it work in the swagger-editor here: https://editor.swagger.io/

    The default parameter is a bit confusing because swagger 2.0 initially described the default parameter without specifying a server or client reference frame.

    Swagger 2.0 spec Defines schema default as

    default (Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object)
    

    OpenAPI v3.0 spec

    default - The default value represents what would be assumed by the consumer of the input as the value of the schema if one is not provided. Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object defined at the same level. For example, if type is string, then default can be "foo" but cannot be 1.
    

    0 讨论(0)
提交回复
热议问题