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

后端 未结 1 1296
滥情空心
滥情空心 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:52

    Your usage of default is wrong. You probably want example instead.

    default is only used with optional fields and is handled on the server side. That is, if the client does not supply a value in the payload, the server will use the default value.

    Consider this User model:

    definitions:
      User:
        type: object
        required:
          - username
        properties:
          username:
            type: string
          role:
            type: string
            enum:
              - user
              - poweruser
              - admin
            default: user
    

    The role property is optional and defaults to user. So, if the client sends the payload without role:

    {
      "username": "bob"
    }
    

    the server will assume role=user.


    In your case, it looks like you want to provide example values for the fields. This is what the example keyword is for:

    definitions:
      MyObject:
        type: object
        description: Contains default properties
        required:
          - cats
          - dogs
        properties:
          cats:
            type: number
            example: 9      # <---
          dogs:
            type: string
            example: fido   # <---
    

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