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

时间:2016-07-03 13:47:49

标签: swagger swagger-ui swagger-2.0 swagger-editor

您好我正在尝试通过身体参数发送默认值,但是在提交时它没有采取。任何人都可以帮我解决这个问题。这是我的代码,我试图通过正文

发送默认的名称参数
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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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