问题描述
我尝试使用以下路径创建一个 swagger 文件:路径:/v1/customers/{id}/summary :
I am try to create a swagger file with the following path: paths: /v1/customers/{id}/summary :
但是我立即收到以下错误:
However I get the following error right off bat:
API 需要路径参数但未定义:id在路径 ▹/v1/customers/{id}/summary
API requires path parameter but it is not defined: idat paths ▹ /v1/customers/{id}/summary
它似乎不喜欢 'id' 参数.谁能告诉我如何纠正这个问题?
It does not seem to like the 'id' parameter. Could anybody tell me how I could rectify this?
如果我深入研究,我会看到以下内容:
If I drill down on this I see the following:
Details
Object
swaggerError: Object
errors: Array [1]
0: Object
code: "MISSING_API_PATH_PARAMETER"
message: "API requires path parameter but it is not defined: id"
data: "/v1/customers/{id}/summary"
path: Array [2]
warnings: Array [0]
推荐答案
基本上,您是通过使用路径模板来声明一个包含路径参数的路径.在这种情况下,{id}
声明了一个名为 id
的路径参数.
Basically, you're declaring a path that has a path parameter in it, by using path templates. In this case {id}
declares a path parameter called id
.
当您声明这样的路径时,意味着您必须将该路径参数声明为操作的一部分.
When you declare such a path, it means that you have to declare that path parameter as part of the operation.
看看这个 YAML 示例:
Take a look at this YAML example:
/pets/{id}:
get:
description: Returns a user based on a single ID, if the user does not have access to the pet
operationId: findPetById
produces:
- application/json
- application/xml
- text/xml
- text/html
parameters:
- name: id
in: path
description: ID of pet to fetch
required: true
type: integer
format: int64
responses:
'200':
description: pet response
schema:
$ref: '#/definitions/pet'
default:
description: unexpected error
schema:
$ref: '#/definitions/errorModel'
可以看到路径中有一个{id}
,还有一个对应的id
参数定义.没有它,规范将无效.
You can see there's an {id}
in the path, and a corresponding id
parameter definition. Without it, the spec won't be valid.
这篇关于Swagger:路径参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!