json-schema-ref-parser icon indicating copy to clipboard operation
json-schema-ref-parser copied to clipboard

Relative references using `$def` are being URL encoded

Open drpump opened this issue 1 year ago • 5 comments

We are using a $defs section in our schemas. Schemas within the $defs section are referenced via local file references, and these schemas can themselves have cross references to other files. The ref parser is correctly injecting a$defs cross-reference but replacing the $defs with %24defs (i.e. it is applying URL encoding). I don’t think this behaviour is correct: while $ is a reserved character in the URIs RFC, it is designated as a sub-delimiter

For example in the parent file:

{
  “properties”: {
     “foo”: “string”
  },
  ”$defs”: {
    “myschema”: {
       “$ref”: “./myschema.json”
    },
    ”otherschema”: {
       “$ref”: “./otherschema.json”
    }
  }
}

And the myschema.json file:

{
      “allOf”: [
        { “$ref”: “./otherschema.json” },
        { 
           “properties”: {
              “someProp”: “string”
           }
        }
      ]
}

Flattening results in:

{
  “properties”: {
     “foo”: “string”
  },
  ”$defs”: {
    “myschema”: {
      “allOf”: [
        { “$ref”: “#/%24defs/otherschema.” },
        { 
           “properties”: {
              “someProp”: “string”
            }
        }
      ]  
    },
    ”otherschema”: {
         “properties”: {
            “otherProp”: “string”
         }
     }
  }
}

drpump avatar Aug 14 '24 01:08 drpump

There was the same issue 262 which was closed in March. However, This issue still exists in the latest version

lindenquan avatar Aug 14 '24 14:08 lindenquan

~Note that this is occurring in v11.6.4. I haven’t updated to 11.7 yet.~

I encountered this issue with v11.6.4. Just did a quick test with 11.7 and it makes no difference.

drpump avatar Aug 14 '24 21:08 drpump

It also seems like the $defs keyword of a schema is not handled like the previous keyword definitions, but since this is not enforced and the convention changed over time makes it really tricky. See also: https://json-schema.org/draft/2019-09/release-notes#semi-incompatible-changes

Code dealing with "definitions" https://github.com/search?q=repo%3AAPIDevTools%2Fjson-schema-ref-parser+definitions&type=code

Especially: https://github.com/APIDevTools/json-schema-ref-parser/blob/27abe40702c18c02cae2ad9a0fdd5a33199d9154/lib/bundle.ts#L77

No matching code for "defs" https://github.com/search?q=repo%3AAPIDevTools%2Fjson-schema-ref-parser+defs&type=code

However, not really related to the encoding of $ in json-pointers.

simontaurus avatar Sep 08 '24 10:09 simontaurus

Is there a plan to fix this? Is there a workaround?

Edit: I guess it works if I replace all $defs with definitions, and wrap all $ref in allOf because of the issue #370

KurtGokhan avatar Apr 25 '25 13:04 KurtGokhan

Not currently, although we are open to PRs

jonluca avatar Apr 25 '25 18:04 jonluca

This was obviously not be fixed by #383, we still face the described problem. However, this fork seems to address this issue with a fix. Would be great if it finds its way into the next version.

ThomasJejkal avatar Nov 11 '25 09:11 ThomasJejkal