我使用来自openapi-generator的自己的openapi-3.0.0模板创建了一个服务器存根。 在实现用于创建资源的API逻辑时,我引用了在requestBodies下声明的组件,如下所示:
components:
requestBodies:
SamyojyaUser:
content:
application/json:
schema:
$ref: '#/components/schemas/SamyojyaUser'
application/xml:
schema:
$ref: '#/components/schemas/SamyojyaUser'
description: Samyojya User object that needs to be added to the system
required: true
API声明如下
paths:
/samyojya-user:
post:
operationId: addSamyojyaUser
requestBody:
content:
application/json:
schema:
$ref: '#/components/requestBodies/SamyojyaUser'
x-content-type: application/json
但是,在处理请求Ajv时,抱怨
can't resolve reference #/components/requestBodies/SamyojyaUser from id #
看起来好像注册requestBodies组件存在一些问题。调试时,我会在Ajv中看到其他组件。我很想直接在path对象上使用用户组件,但是我想进一步自定义请求主体。关于进一步调整有什么想法吗?
答案 0 :(得分:0)
对#/components/requestBodies/...
的引用只能在requestBody
下直接使用,而不能在schema
下使用:
paths:
/samyojya-user:
post:
operationId: addSamyojyaUser
requestBody:
$ref: '#/components/requestBodies/SamyojyaUser'
此外,请求正文组件中的缩进错误-description
和required: true
必须与content
处于同一级别。
components:
requestBodies:
SamyojyaUser:
content:
application/json:
schema:
$ref: '#/components/schemas/SamyojyaUser'
application/xml:
schema:
$ref: '#/components/schemas/SamyojyaUser'
# vvvv Note the indentation level
description: Samyojya User object that needs to be added to the system
required: true
在生成代码之前,使用https://editor.swagger.io(或其他验证器)验证您的OpenAPI定义。