在使用swagger-ui-express和swagger-jsdoc时如何正确地在swagger文件中使用$ ref [英] How to use $ref in swagger file properly while working with swagger-ui-express and swagger-jsdoc
问题描述
我开始使用带有 swagger-ui-express 和 swagger-jsdoc 的swagger自动记录我现有的API,该API是用 nodejs 并表达(如此处所述-示例).
I started to use swagger with swagger-ui-express and swagger-jsdoc to auto document my existing API, which is written with nodejs and express (like described here - example).
当我尝试将$ref
添加到注释中现有的 JSON Schema 文件(与所有js文件位于同一目录中的项目内)时,遇到一个问题.
I came across a problem when I tried to add a $ref
to an existing JSON Schema file (that sits inside my project on the same directory as all my js files) on my annotation.
我尝试编写本地路径(./schema.json
)和绝对路径,并尝试使用#
,并使用了许多语法,但没有任何效果.
I tried to write the local path (./schema.json
) and the absolute path, tried to use #
, using many syntaxes and nothing worked.
我的注释如下:
/**
* @swagger
* /testing:
* get:
* description: This should show the json schema
* responses:
* 200:
* description: "successful operation"
* schema:
* $ref: "./schema.json"
*/
我希望swagger ui可以在我的请求部分中向我显示JSON模式.我收到以下错误-
I expected the swagger ui to show me the JSON schema in my request section by. I get the following error -
Resolver error at paths./testing.get.responses.200.schema.$ref
Could not resolve reference: Tried to resolve a relative URL, without having a basePath. path: './schema.json' basePath: 'undefined'.
我在网上查找了问题,找不到明确的答案.我看到了一个解决方案,建议我将架构放在服务器上并使用URL地址访问它,但是我不愿意在这一点上这样做.
I looked the problem up online and couldn’t find any clear answer. I saw a solution that suggested I should put my schema on a server and access it with an URL address but I prefer not to do it on this point.
此外,在某些时候,我将该方案保存在一个变量中,然后将其放在$ref
中,并且工作正常.唯一的问题是,该方案在同一文件中包含对元素的一些内部引用,而 Swagger 无法解析它们.
Also, at some point, I saved the scheme in a variable and then put it in the $ref
and it worked fine. The only problem was that the scheme included some inner refs to an element in the same file and Swagger couldn't resolve them.
在 swagger-ui-express 中是否可以使用$ref
正常工作?
Is there a way to work properly with $ref
in swagger-ui-express?
推荐答案
在这里遇到了同样的问题并找到您的问题.
got the same problem here and find your question.
我刚刚解决了我的问题,所以我想可以帮忙.
I've just solved mine so I guess I might help.
首先,您是否尝试过:
schema:
$ref: "schema.json"
没有.
这是他们在文档中的教学方式.
without the .
This is how they teach at the documentation.
这篇关于在使用swagger-ui-express和swagger-jsdoc时如何正确地在swagger文件中使用$ ref的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!