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

Bundled schema and $id #97

Closed
paulwib opened this issue Aug 24, 2018 · 4 comments
Closed

Bundled schema and $id #97

paulwib opened this issue Aug 24, 2018 · 4 comments

Comments

@paulwib
Copy link

paulwib commented Aug 24, 2018

It seems that when bundling a schema $id attributes aren't considered which makes the output incompatible with other JSON Schema tools, for example:

a.json:

{
  "$id": "https://schema.example.com/a.json",
  "b": {
    "$ref": "b.json"
  }
}

b.json:

{
  "$id": "https://schema.example.com/b.json",
  "properties": {
    "bar": {
      "$ref": "#/definitions/thing"
    }
  },
  "definitions": {
    "thing": {
      "type": "string"
    }
  }
}

Some code:

const RefParse = require('json-schema-ref-parser');

RefParse.bundle('a.json')
  .then(s => {
    console.log(JSON.stringify(s, null, 2));
  });

Outputs:

{
  "$id": "https://schema.example.com/a.json",
  "b": {
    "$id": "https://schema.example.com/b.json",
    "properties": {
      "bar": {
        "$ref": "#/b/definitions/thing"
      }
    },
    "definitions": {
      "thing": {
        "type": "string"
      }
    }
  }
}

The issue is that in JSON Schema b.properties.bar.$ref is resolved against the nested b.$id giving https://schema.example.com/b.json#/b/definitions/bar which doesn't actually exist.

This breaks other tools, like trying to use the bundled schema in Ajv.

I saw the comment on #22 that the aims of this library do not include supporting JSON Schema and the $id keyword, but it's confusing as the first line of the README says: "Parse, Resolve, and Dereference JSON Schema $ref pointers" 😄 Latest JSON Schema has it's own definition of $ref and no longer uses the expired JSON Reference spec 😮

Any chance of an option to consider $id when bundling?

@JamesMessinger
Copy link
Member

Hi @paulwib - Thanks for opening this issue. As you pointed out, I've said before that this library is an implementation of the JSON Reference specification and the JSON Pointer specification, not the JSON Schema specification. Thus, the id keyword isn't supported, because that's not part of JSON Reference or JSON Pointer.

That said... the name of this library is (perhaps inappropriately) json-schema-ref-parser, so it's easy to see why many people have been confused by its lack of support for the JSON Schema spec. So, yeah, I will probably end up adding support for id and $id at some point, to eliminate the confusion and because multiple people have now requested it.

No promises on timeframe though. I have lots of other obligations competing for my time and attention, and this change is likely to be pretty complicated and require a lot of thought and thorough testing.

@JamesMessinger
Copy link
Member

Linking #22 and #95 here, for future reference

@philsturgeon
Copy link
Member

Also related to #145.

@philsturgeon
Copy link
Member

Let's leave #145 to handle this topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants