Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use $ref to reference a path from another OpenAPI file?

Tags:

openapi

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?

For example, I have Anotherfile.yaml that contains the /a/b path:

paths:
  /a/b:
    post:

In another file, I use $ref to reference the /a/b path as follows:

paths:
  /c/d:
    $ref: 'Anotherfile.yaml#/paths/a/b'

but this gives an error:

Could not find paths/a/b in contents of ./Anotherfile.yaml

like image 304
Tiina Avatar asked Nov 18 '19 06:11

Tiina


People also ask

How do I merge OpenAPI files?

How to Merge OpenAPI 3 Files. `resolve` is the command to merge definition files back into one. It's followed by the path to the main definition file that includes references to other files you wish to merge. Next, we add the option flag -o to output the resulting definition to a new file named spec-output.

How does swagger relate to OpenAPI?

What Is Swagger? Swagger is a set of open-source tools built around the OpenAPI Specification that can help you design, build, document and consume REST APIs. The major Swagger tools include: Swagger Editor – browser-based editor where you can write OpenAPI definitions.

What is OpenAPI URL?

In OpenAPI 3.0, you use the servers array to specify one or more base URLs for your API. servers replaces the host , basePath and schemes keywords used in OpenAPI 2.0. Each server has an url and an optional Markdown-formatted description .


1 Answers

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'
like image 97
Helen Avatar answered Sep 24 '22 21:09

Helen