question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[OAS3] Schemas with external references fail to resolve properly

See original GitHub issue
  • swagger-parser version: io.swagger.parser.v3:swagger-parser-v3:2.0.3-SNAPSHOT latest from Maven Central.

  • Code snippet used to load OpenAPI definition: OpenAPI oai = new OpenAPIV3Parser().read("/path/to/index.yaml");

The attached tarball is a minimal sample which exhibits the issue:

  • index.yaml uses external $refs to refer to a response, (components/response-with-reference.yaml)
  • this in turn refers to a schema (components/schema-with-reference.yaml)
  • this in turn refers to another schema to define the type of items in an array (components/referent.yaml)

On running the code and examining the toString() output of the OpenAPI object obtained (see attached), the $ref for items becomes ./components/components/referent.yaml, which is incorrect. This appears to be due to the invocation of constructRef in ExternalRefProcessor#processRefSchema.

Attempting to run openapi-generator-cli with this definition results in the error

java.lang.RuntimeException: Could not find ./components/components/referent.yaml on the classpath
...
[main] WARN  io.swagger.v3.parser.OpenAPIV3Parser - Exception while reading:
java.lang.RuntimeException: Unable to load RELATIVE ref: ./components/components/referent.yaml

Finally, we would like to understand which path is used to process relative references, is it the root location (the location containing the index.yaml), or is it the location containing the current document?

  • The swagger.io documentation states that “Remote Reference…Uses the whole document located on the same server and in the same location”. The phrase “same location” is a little unclear here but my interpretation is that it refers to the “current” file, eg the one containing the $ref.
  • The OpenAPI 3 Spec states that “Relative references used in $ref are processed…using the URL of the current document as the base URI”.

If the spec means that the current document should be used as the base URI, then why does the $ref in components/response-with-reference.yaml have to be ./components/schema-with-reference.yaml? Surely it should just be schema-with-reference.yaml since components is already on the “current path”?

You can surely see that there is an inconsistency between the way the reference is made from response-with-reference.yaml and schema-with-reference.yaml, in that to currently get generation to work, one has to contain components and the other not.

oapi-reference-test.tar.zip parser-output.txt

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
tomblenchcommented, Aug 24, 2018

@webron that makes perfect sense to me but that doesn’t seem to be happening in practice.

If you look at the attached tar you will see that response-with-reference.yaml has the contents:

---
description: Response with schema containing reference
content:
  application/json:
    schema:
      $ref: "./components/schema-with-reference.yaml"

This correctly resolves ./components/schema-with-reference.yaml despite the fact that response-with-reference.yaml is also in the components directory. If your assertion were correct then the $ref line should be

      $ref: "./schema-with-reference.yaml"

but this fails.

0reactions
sivagithub2019commented, Jun 14, 2019

HiI have create this new issue need to write a java code for validating a json response against a OpenApi json schema · Issue #1126 · swagger-api/swagger-parser

| | | | | |

|

| | | | need to write a java code for validating a json response against a OpenA…

need to write a java code for validating a json response against a OpenApi json schema. For example the petstore… |

|

|

your help is much appreciated in this. thanks.  On Friday, June 14, 2019, 06:30:48 PM GMT+1, Grace Karina Gonzalez Diaz notifications@github.com wrote:

@sivagithub2019 can you please add a new issue, and please add an example so we can discuss, this is a closed issue, so It will be hard to follow in here. thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenAPI Specification - Version 3.0.3 - Swagger
The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to ... to reference those parts as follows from the JSON Schema definitions.
Read more >
json - Resolver error when referencing external schema files
The problem disappears when I remove one (any) of the PATHs. The problem disappears when I use the inline (i.e. the ones defined...
Read more >
OpenAPI Specification v3.1.0 | Introduction, Definitions, & More
The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for HTTP APIs.
Read more >
Splitting specification file - API Handyman
Then we can remove the Person definition in definitions and replace all ... You can also reference a file outside the current folder....
Read more >
express-openapi - npm
Supports error middleware scoped to your API's basePath . See args.errorMiddleware ... Supports external schema references. See args.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found