Search code examples
jsonswaggeropenapijson-ref

How can I create OpenAPI spec files having $ref to other sub-files? Is this possible?


I am unable to get OpenAPI spec Swagger "external refs" to work because Swagger rendering gives me an exception.

I must have this to make editing giant swagger documents more manageable since I don't have an auto-generator (due to architecture of azure sdk)

I am using 3.0.1 version, trying to do something like this:

paths:
  /v1:
    $ref: 'first-api.yaml#/paths/~1v1'
  /v2:
    $ref: 'second-api.yaml#/paths/~1v2'

The answer in this question doesn't work: How to use $ref to reference a path from another OpenAPI file?

The error I get looks like this (in IntelliJ-IDEA IDE using the OpenAI (Swagger) Editor plugin):

enter image description here

Would appreciate a link to an example project proving this works.

I tried it here, for example, and it fails:


Solution

  • Try changing the $ref to specify the path starting with an explicit reference to the current directory. That is, prepend the path with ./

    paths:
      /v1:
        $ref: './first-api.yaml#/paths/~1v1'