不允许大摇大摆的请求体
Swagger requestbody not allowed
我正在尝试将请求正文添加到 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 值。另外,如果我有多个正文参数和一个 /: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
您不需要多个 in: body
参数,您已经在用户模式中定义了它们(无论如何每个请求只有一个正文)。这正是应该做的。只需删除第二个 '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
.
我正在尝试将请求正文添加到 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 值。另外,如果我有多个正文参数和一个 /: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
您不需要多个 in: body
参数,您已经在用户模式中定义了它们(无论如何每个请求只有一个正文)。这正是应该做的。只需删除第二个 '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
.