swagger - 无法引用在 Swagger 的单独文件中定义的组件架构
问题描述
我有以下 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 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
和 Swagger 配置:
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,但没有 basePath。路径:'components.yaml' 基本路径:'未定义'
我在这里想念什么?
解决方案
所以我设法使用这个伟大的资源解决了这个问题:
我所要做的就是在 API 文档文件的末尾添加对组件的引用,并相应地更改架构引用:
403:
description: User not authorised to call this API
schema:
$ref: '#components/schemas/AuthError'
components:
$ref: './components.yaml'
推荐阅读
- sql-server - 如何从 SSMS 检查数据同步组?
- excel - 在 Vba 中打开文本文件,出现错误 1004
- python - 从另一个数据框中替换列的值
- mysql - 在字符串中查找完全匹配的正则表达式适用于其他地方,但不适用于 MySql 查询
- networking - tcp/ip 和网络的新手。我有一些问题
- yii2 - 异常 'ReflectionException' 与消息 'Class ingelby\toolbox\helpers\PublicIdBehavior 不存在' 在 yii2 Advance
- python - 值错误:维度不匹配 - Python 分类
- react-native-android - 无法加载脚本。请确保您正在运行 Metro 服务器
- mysql - 更新到 MySQL Workbech 8.0.18 后,没有显示数据库
- android - OnClickListeners:防止内存泄漏