无法引用在Swagger中的单独文件中定义的组件架构
我有以下api文档:无法引用在Swagger中的单独文件中定义的组件架构,swagger,openapi,swagger-2.0,swagger-3.0,Swagger,Openapi,Swagger 2.0,Swagger 3.0,我有以下api文档: swagger: "3.0" info: version: 0.0.1 title: Test API paths: /users: get: summary: Get all registered users produces: - application/json responses: 200: description: Users successfully retu
swagger: "3.0"
info:
version: 0.0.1
title: Test API
paths:
/users:
get:
summary: Get all registered users
produces:
- application/json
responses:
200:
description: Users successfully returned
403:
description: User not authorised to call this API
schema:
$ref: 'components.yaml#/components/schemas/AuthError'
其中AuthError模式在名为components.yaml的单独yaml文件中定义:
components:
schemas:
AuthError:
type: object
properties:
error:
type: sting
description: Error message
以及昂首阔步的配置:
const swaggerDefinition = {
info: {
title: 'FlexiWAN REST API documentation',
version: '1.0.0',
description: 'This is the REST API for FlexiWAN management',
},
components: {},
host: 'local.flexiwan.com:3443',
basePath: '/api',
securityDefinitions: {
JWT: {
type: 'apiKey',
in: 'header',
name: 'Authorization',
description: "",
}
}
};
const options = {
swaggerDefinition,
apis: ['./swagger/**/*.yaml'],
};
const swaggerSpec = swaggerJSDoc(options);
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
但是,当我尝试访问Swagger UI时,会出现以下错误:
路径处的解析程序错误。/users.get.responses.403.schema.$ref
无法解析引用:尝试解析相对URL,但没有基本路径。路径:“components.yaml”基本路径:“未定义”
我在这里遗漏了什么?所以我设法用以下方法解决了这个问题: 我所要做的就是在API文档文件的末尾添加对组件的引用,并相应地更改模式引用:
403:
description: User not authorised to call this API
schema:
$ref: '#components/schemas/AuthError'
components:
$ref: './components.yaml'
如果使用
$ref:'/components.yaml###/components/schemas/authror'
,它是否有效?你用的是大摇大摆的用户界面还是大摇大摆的编辑器?@Helen我根据你的建议修改了它,但它仍然给我同样的错误。我通过同名的节点包使用Swagger UI 1)什么版本的Swagger UI?你能发布你的大摇大摆的UI配置代码吗?2) 将swagger:“3.0”
替换为swagger:“2.0”
@Helen抱歉,我正在使用swagger UI express软件包。我编辑了上面的帖子,我怀疑这是否是真正问题的解决方案。我有一个与你类似的场景,也尝试了你的建议,但没有成功。老实说,我仍然不确定我所做的是否正确。