如何在Swagger 2.0中通过body发送默认值

如何在Swagger 2.0中通过body发送默认值,swagger,swagger-ui,swagger-2.0,swagger-editor,Swagger,Swagger Ui,Swagger 2.0,Swagger Editor,您好,我正试图通过正文参数发送默认值,但提交时不需要。谁能在这个问题上帮助我。这是我的代码,我正试图通过body发送默认的name参数 swagger: '2.0' info: version: 1.0.0 title: PetStore on Heroku description: | **This example has a working backend hosted in Heroku** You can try all HTTP operation des

您好,我正试图通过正文参数发送默认值,但提交时不需要。谁能在这个问题上帮助我。这是我的代码,我正试图通过body发送默认的name参数

swagger: '2.0'
info:
  version: 1.0.0
  title: PetStore on Heroku
  description: |
    **This example has a working backend hosted in Heroku**

    You can try all HTTP operation described in this Swagger spec.

    Find source code of this API [here](https://github.com/mohsen1/petstore-api)
host: petstore-api.herokuapp.com
basePath: /pet
schemes:
  - http
  - https
consumes:
  - application/json
  - text/xml
produces:
  - application/json
  - text/html
paths:
  /:
    get:
      parameters:
        - name: limit
          in: query
          description: number of pets to return
          type: integer
          default: 0
      responses:
        200:
          description:  List all pets
          schema:
            title: Pets
            type: array
            items:
              $ref: '#/definitions/Pet'
    post:
      parameters:
        - name: pet
          in: body
          description: The pet JSON you want to post
          schema:
            $ref: '#/definitions/Pet'
          required: true
      responses:
        200:
          description: Make a new pet
    put:
      parameters:
        - name: pet
          in: body
          description: The pet JSON you want to post
          schema:
            $ref: '#/definitions/Pet'
          required: true
      responses:
        200:
          description: Updates the pet
  /{petId}:
    get:
      parameters:
        - name: petId
          in: path
          type: string
          description: ID of the pet
          required: true
      responses:
        200:
          description: Sends the pet with pet Id

definitions:
  Pet:
    type: object
    properties:
      name:
        type: string
        default : "xxxxxxx"
      birthday:
        type: integer
        format: int32

默认值应在服务器端处理,因为服务器不应总是假定客户端发送的HTTP请求100%符合规范。

如果您尝试使用架构发送默认数据,我认为这可以帮助您:

definitions:
 Pet:
  type: object
  default:
    name: xxxx
    birthday: xxxx 
  properties:
    name:
      type: string
    birthday:
      type: integer
      format: int32

好的,我可以在服务器端处理,但是,Swagger中的默认值有什么用呢?您也可以使用Swagger规范生成服务器存根,它应该正确处理规范中规定的默认值。