提问者:小点点

Swagger编辑器主体中的多个参数


所以我明白,如果我们想要主体参数,我们必须有一个模式,我就是这样做的。问题是,无论我如何定义我的模式,它都不允许我有多个主体参数。这是我尝试过的一种方法的一个例子。任何帮助都会很棒!

swagger: '2.0'

# This is your document metadata
info:
  version: "0.0.1"
  title: Todo App
schema: {
        }
host: localhost:3000
schemes:
  - http
  - https
consumes:
  - application/json
produces:
  - application/x-www-form-urlencoded
basePath: /

paths:
  # This is a path endpoint. Change it.
  /tasks:
    post:
      description: |
        Add 'Task' object.

      parameters:
        # An example parameter that is in query and is required
        -
          name: name 
          in: query
          description: unique object task name
          required: true
          schema:
            type: string
        - name: description
          in: query
          description: task description
          required: true
          schema:
            type: string

      responses:
        # Response code
        200: 
          description: Successful response
          # A schema describing your response object.
          # Use JSON Schema format
          schema:
              title: Return String
              type: string
              example: "Task added succesfully"
        500:
          description: Error
          schema: 
            type: string
            example: "Could not add Task"

共2个答案

匿名用户

我不确定是否理解你的问题…

  • 如果您尝试为一个操作定义多个主体参数,则不能。如swagger规范中所述:

body[…]只能有一个body参数

  • 如果你试图发送一个带有多个参数的body,在定义部分添加一个对象模型,并在body参数中引用它,见下文(适用于editor.swagger.io):

您的示例节点也错误,请参阅此处了解更多详细信息。

swagger: '2.0'
info:
  version: "0.0.1"
  title: Todo App
host: localhost:3000
schemes:
  - http
  - https
consumes:
  - application/json
produces:
  - application/x-www-form-urlencoded
basePath: /
paths:
  # This is a path endpoint. Change it.
  /tasks:
    post:
      description: |
        Add 'Task' object.
      parameters:
        - name: task 
          in: body
          description: task object
          required: true
          schema:
            $ref: '#/definitions/Task'
      responses:
        200:
          description: Successful response
          schema:
              title: Return String
              type: string
              example: "Task added succesfully"
        500:
          description: Error
          schema: 
            type: string
            example: "Could not add Task"
definitions:
  Task:
    description: Task object
    properties:
      name:
        type: string
        description: task object name
      description:
        type: string
        description: task description
    required:
      - name
      - description

匿名用户

您还可以使用properties作为其schema的一部分来定义请求体参数的属性。这在Object Payload下有一个很好的例子:https://swagger.io/docs/specification/2-0/describing-request-body/.

paths:
  /users:
    post:
      summary: Creates a new user.
      consumes:
        - application/json
      parameters:
        - in: body
          name: user
          description: The user to create.
          schema:
            type: object
            required:
              - userName
            properties:
              userName:
                type: string
              firstName:
                type: string
              lastName:
                type: string
      responses:
        201:
          description: Created

当然,缺点是您无法重用对象定义,但有时对象定义不合适。