APIDevTools / json-schema-ref-parser

Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers
https://apitools.dev/json-schema-ref-parser
MIT License
953 stars 227 forks source link

fix: internal root level reference do not resolve #246

Closed jonaslagoni closed 2 years ago

jonaslagoni commented 2 years ago

This PR addresses the issue where root level references to internal schema definitions are not resolved accurately.

Fixes https://github.com/APIDevTools/json-schema-ref-parser/issues/201

jonaslagoni commented 2 years ago

So, I am not quite sure of the best cause of action to solve this issue, so might need some pointers from someone who knows the code more in-depth 😄

Trying to dereference the input in the tests the following happens:

  1. First pass, the reference is being dereferenced - https://github.com/APIDevTools/json-schema-ref-parser/blob/f6886ab9ee576754791f2a194b140a637dd98a87/lib/dereference.js#L50
  2. Then the pointer is resolved, it says it is a circular reference (and I cant figure out whether that is incorrect behavior) and the resolved value is the root schema when I believe it should be the resolved definition schema name. - https://github.com/APIDevTools/json-schema-ref-parser/blob/f6886ab9ee576754791f2a194b140a637dd98a87/lib/dereference.js#L133
    pointer = {
    ...,
    path: "/Users/lagoni/Documents/AsyncAPI/json-schema-ref-parser/test/specs/root-internal/root-internal.yaml#/definitions/name",
    originalPath: "/Users/lagoni/Documents/AsyncAPI/json-schema-ref-parser/test/specs/root-internal/root-internal.yaml#/definitions/name",
    value: {
    definitions: {
      name: {
        title: "name",
        type: "object",
        required: [
          "first",
          "last",
        ],
        properties: {
          first: {
            type: "string",
          },
          last: {
            $ref: "#/definitions/name/properties/first",
          },
        },
      },
    },
    $ref: "#/definitions/name",
    },
    circular: true,
    indirections: 0,
    }
  3. Since the pointer is marked as circular, this means that the rest of the references are not crawled, which then means the $ref within the definition is not iterated, which to me indicate that there are multiple places that need to change, but I might be wrong 😅 - https://github.com/APIDevTools/json-schema-ref-parser/blob/f6886ab9ee576754791f2a194b140a637dd98a87/lib/dereference.js#L151

But from this point on, I do not know where or what to try and poke at...

Any suggestions?

cc @philsturgeon (as you are the most recently pushed changed, feel free to re-tag someone else 😄)

jonaslagoni commented 2 years ago

With no interest, closing.