【问题标题】:Swagger Editor multiple parameters in bodySwagger Editor 正文中的多个参数
【发布时间】:2015-06-24 17:41:39
【问题描述】:

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

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"

【问题讨论】:

    标签: swagger swagger-editor


    【解决方案1】:

    我不确定你的问题...

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

    Body [...] 只能有一个 body 参数

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

    您的示例节点也有误,详情请参阅here

    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
    

    【讨论】:

    • 感谢您的回答。但是你知道这个错误是什么意思吗? { "message": "无法更新任务。ValidationError: Validator \"required\" failed for path name, Validator \"required\" failed for path description" }
    • 你是怎么得到这个错误的?你能描述一下你的背景吗?
    • 当我尝试测试我的函数时会发生这种情况,使用您使用的示例但对于 put 函数,一切似乎都正常,然后当我尝试测试时收到该错误。
    • 我认为被调用的服务器需要一个名为“名称”的必填字段和另一个名为“描述”的字段。您能否提供有关服务器的更多信息:techno?请求模型?如果 swagger 文件不符合预期的请求,服务器将拒绝所有请求。
    • 我觉得这很矛盾,看orange-opensource.github.io/angular-swagger-ui /pet/{petID}/ 有很多!定义将它们定义为"in": "formData"。试过了!
    【解决方案2】:

    您还可以使用properties 作为其schema 的一部分来定义请求正文参数的属性。这在对象有效负载下有一个很好的示例: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
    

    当然缺点是不能重用对象定义,但有时对象定义并不合适。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-09
      • 2023-03-26
      • 2019-04-27
      • 2017-01-19
      • 1970-01-01
      相关资源
      最近更新 更多