如何使用 $ref 从另一个 OpenAPI 文件引用路径? [英] How to use $ref to reference a path from another OpenAPI file?
问题描述
这里 它说我可以参考另一个文件中的定义单个路径,但该示例似乎引用了整个文件,而不是 paths
对象下的单个路径定义.如何在另一个文件的 paths
对象中分配单个路径?
Here it says I could refer to the definition in another file for an individual path, but the example seems to refer to a whole file, instead of a single path definition under the paths
object. How to assign an individual path in another file's paths
object?
例如,我有包含 /a/b
路径的 Anotherfile.yaml
:
For example, I have Anotherfile.yaml
that contains the /a/b
path:
paths:
/a/b:
post:
在另一个文件中,我使用 $ref
来引用 /a/b
路径如下:
In another file, I use $ref
to reference the /a/b
path as follows:
paths:
/c/d:
$ref: 'Anotherfile.yaml#/paths/a/b'
但这会产生错误:
在 ./Anotherfile.yaml 的内容中找不到路径/a/b
Could not find paths/a/b in contents of ./Anotherfile.yaml
推荐答案
引用路径时,需要转义路径名 将 /
替换为 ~1
,使 /a/b
变成 ~1a~1b
.请注意,您只对路径名进行转义,而不是对 #/paths/
前缀进行转义.
When referencing paths, you need to escape the path name by replacing /
with ~1
, so that /a/b
becomes ~1a~1b
. Note that you escape just the path name and not the #/paths/
prefix.
$ref: 'Anotherfile.yaml#/paths/~1a~1b'
这篇关于如何使用 $ref 从另一个 OpenAPI 文件引用路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!