【问题标题】:Swagger requestbody not allowed不允许招摇请求正文
【发布时间】:2020-01-04 18:35:56
【问题描述】:

我正在尝试将请求正文添加到一个 swagger 文件中,但是当我使用 requestBody 时,它一直说 Additional properties not allowed: requestBody 我尝试了多个 -in 这样的参数

      parameters
        - in: body
          name: email
          description: The user to create.
          schema:
            $ref: "#/definitions/User"
        - in: body
          name: password
          description: The user to create.
          schema:
            $ref: "#/definitions/User"

但是它说Operation cannot have multiple body parameters 所以我不确定如何引用所有 req.body 值。另外,如果我有多个 body 参数和 /:id 路径怎么办? 我对 swagger 还是很陌生,所以我很感激这方面的任何帮助。

swagger: "2.0"
info:
  version: "1.0.0"
  title: Hello World App during dev, should point to your local machine
basePath: /v1
schemes:
  # tip: remove http to make production-grade
  - http
  - https
paths:
  /user/signup:
    x-swagger-router-controller: user
    post:
      description: signup POST
      operationId: signup
      parameters:
        - in: body
          name: email
          description: The user to create.
          schema:
            $ref: "#/definitions/User"
        - in: body
          name: password
          description: The user to create.
          schema:
            $ref: "#/definitions/User"
      responses:
        "200":
          description: Success got all the listings
          schema:
            $ref: "/definitions/User"
        "500":
          description: Unexpected Error
          schema:
            type: object
            properties:
              message:
                type: string

  /user/login:
    x-swagger-router-controller: user
    post:
      description: Login request
      operationId: login
      parameters:
        - in: body
          name: login
          description: The user to create.
          schema:
            $ref: "#/definitions/Login"
      responses:
        "200":
          description: Success got all the listings
          schema:
            $ref: "/definitions/Login"
        "500":
          description: Unexpected Error
          schema:
            type: object
            properties:
              message:
                type: string

definitions:
  User:
    properties:
      id:
        type: integer
      email:
        type: string
      password:
        type: string
      instagramName:
        type: string
      over21:
        type: boolean
      role:
        type: string
      fullName:
        type: string
      address1:
        type: string
      address2:
        type: string
      city:
        type: string
      state:
        type: string
      zip:
        type: string
      passwordCreated:
        type: string

  Login:
    properties:
      id:
        type: string
      email:
        type: string
      password:
        type: string

【问题讨论】:

    标签: swagger swagger-ui


    【解决方案1】:

    您不需要多个in: body 参数,您已经在用户模式中定义了它们(每个请求都只有一个主体)。这正是它应该做的。只需删除第二个“主体”,然后重命名另一个:

    parameters:
      - in: body
        name: user
        description: The user to create.
        schema:
          $ref: "#/definitions/User"
    

    如果您需要路径参数,您可以将其定义为in: path。您还需要将其添加到路径本身:

    paths:
      /user/signup/{id}:
        x-swagger-router-controller: user
        post:
          description: signup POST
          operationId: signup
          parameters:
            - in: path
              name: id
              description: User id
              type: string
              required: true
            - in: body
              name: user
              description: The user to create.
              schema:
                $ref: "#/definitions/User"
    

    in: body 不同,您可以有多个in: path 参数。路径参数必须包含required: true

    【讨论】:

      猜你喜欢
      • 2021-07-08
      • 1970-01-01
      • 2020-12-03
      • 2021-11-13
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      相关资源
      最近更新 更多