如何使用 $ref 从另一个 OpenAPI 文件引用路径? [英] How to use $ref to reference a path from another OpenAPI file?

查看:68
本文介绍了如何使用 $ref 从另一个 OpenAPI 文件引用路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里 它说我可以参考另一个文件中的定义单个路径,但该示例似乎引用了整个文件,而不是 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆