【问题标题】:How to send the Default value through body in Swagger 2.0如何在 Swagger 2.0 中通过正文发送默认值
【发布时间】:2016-07-03 13:47:48
【问题描述】:

您好,我正在尝试通过正文参数发送默认值,但在提交时未采用。任何人都可以在这个问题上帮助我。这是我的代码,我正在尝试通过正文发送默认的 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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 好的我可以在服务器端处理但是,Swagger中的默认值有什么用。
    • 您还可以使用 swagger 规范生成服务器存根,该存根应正确处理规范中规定的默认值。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 2021-03-28
      • 2015-05-12
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多