大摇大摆 POST 与 non-json body

Swagger POST with non-json body

我正在定义您 POST 使用 non-JSON body 的资源。它只是一个类似于表单参数的字符串。类似于 OAuth 的情况: grant_type=&代码=&redirect_uri=

我如何在 Swagger 上记录它?我必须使用 formParam 格式而不是 body 吗?我放入 body 的所有内容都会转换为 JSON 个示例。

  TokenRequest:
    properties:
      grant_type:
        type: string
        description: OAuth Grant Type
        enum:
        - authorization_code
        - refresh
      code:
        type: string
        description: Authorization Code obtained from /authorize required if grant_type = au
      redirect_uri:
        type: string
        description: Defined Redirect URI Example - https://example.com/callback
      refresh_token:
        type: string
        description: Required if grant_type = refresh

下面是一个关于如何记录表单数据的例子:

post:
  tags:
    - pet
  summary: Updates a pet in the store with form data
  description: ''
  operationId: updatePetWithForm
  consumes:
    - application/x-www-form-urlencoded
  produces:
    - application/xml
    - application/json
  parameters:
    - name: petId
      in: path
      description: ID of pet that needs to be updated
      required: true
      type: integer
      format: int64
    - name: name
      in: formData
      description: Updated name of the pet
      required: false
      type: string
    - name: status
      in: formData
      description: Updated status of the pet
      required: false
      type: string
  responses:
    '405':
      description: Invalid input
  security:
    - petstore_auth:
        - 'write:pets'
        - 'read:pets'

但在您的情况下,您似乎想要定义 OAuth 设置,因此请参阅 Swagger Spec 2.0 了解更多信息。以下是 PetStore 的示例:

securityDefinitions:
  petstore_auth:
    type: oauth2
    authorizationUrl: 'http://petstore.swagger.io/api/oauth/dialog'
    flow: implicit
    scopes:
      'write:pets': modify pets in your account
      'read:pets': read your pets
  api_key:
    type: apiKey
    name: api_key
    in: header