Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local reference fails after importing sub-schema using << #138

Closed
henhal opened this issue Sep 27, 2019 · 1 comment
Closed

Local reference fails after importing sub-schema using << #138

henhal opened this issue Sep 27, 2019 · 1 comment

Comments

@henhal
Copy link

henhal commented Sep 27, 2019

I have multiple OpenAPI 3.0 API:s using some common types, such as some common schema types and common error responses. The responses also reference common schemas.

A simple example setup:

common.yml:

components:
 schemas:
   SomeError:
     type: object
     properties:
       message: {type: string}
 responses:
   SomeErrorResponse:
     description: Some error
     content:
       application/json:
         schema: {$ref: '#/components/schemas/SomeError'}

api.yml:

openapi: 3.0.2
components:
 schemas:
   # Import common schemas (including the ones used by common responses)
   << : {$ref: 'common.yml#/components/schemas'}
   # Define local schemas
   Foo:
     type: string
 responses:
   # Import common responses (using common schemas)
   << : {$ref: 'common.yml#/components/responses'}
   # Define local responses
   FooResponse:
     description: OK
     content:
       application/json:
         schema: {$ref: '#/components/schemas/Foo'} # This fails unless schemas/Foo is defined in common.yml
paths:
 /foo:
   get:
     summary: Foo
     responses:
       200: {$ref: '#/components/responses/FooResponse'}
       400: {$ref: '#/components/responses/SomeErrorResponse'}

Most of this works:

  • Schemas are imported fine and merged with the local schemas. Paths can use both common and local schemas
  • Responses are also imported and merged.

However, even though #/components/schemas/Foo is now resolved fine from e.g. paths when calling dereference() the local response FooResponse fails to resolve the local schema #/components/schemas/Foo, because it attempts to look it up in common.yml#/components/schemas/Foo:

SyntaxError: Error resolving $ref pointer "/<path>/common.yml#/components/schemas/Foo

It seems that after an import using << :, all references to #/ within the same section (e.g. responses) will refer to that imported document, rather than the current document into which the imported document has been merged.

The effect of this is that I cannot define any local responses using local schemas. Obviously I don't want to pollute my common files with local schemas used for one particular API. I also want to avoid manually copy&paste of all common schemas and responses into every single API.

Is my document structure and the way I use imports incorrect per the specification or is there a bug in json-schema-ref-parser when it comes to resolving # references from a section which has used a << : merge directive?

@jonluca
Copy link
Collaborator

jonluca commented Mar 6, 2024

This is working on latest - let me know if not

@jonluca jonluca closed this as completed Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants