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

JSON Schema: Using $ref to an internal $id is flagged as an error #1104

Closed
GrahamLea opened this issue Apr 20, 2020 · 6 comments
Closed

JSON Schema: Using $ref to an internal $id is flagged as an error #1104

GrahamLea opened this issue Apr 20, 2020 · 6 comments
Assignees
Labels
t/bug Something isn't working

Comments

@GrahamLea
Copy link

Describe the bug
When linting a self-contained JSON schema, using a $ref to refer to an internal sub-schema by $id is reported as an error.

To Reproduce

  1. Grab the "Using $id with $ref" example from Understanding JSON Schema
  2. Run spectral lint -F hint -v schema.json

Expected behavior
Should be a valid schema

Actual behavior

Found 52 rules (36 enabled)
Linting <file>
JSON Schema detected
JSON Schema (loose) detected
JSON Schema Draft 7 detected

<file>
 28:15  error  invalid-ref  ENOENT: no such file or directory, open ''

✖ 1 problem (1 error, 0 warnings, 0 infos, 0 hints)

Environment:

@GrahamLea GrahamLea added the t/bug Something isn't working label Apr 20, 2020
@nulltoken
Copy link
Contributor

@GrahamLea Interestlingly the latest develop tip yields a different error

$ yarn cli lint -v ./repro/schema.json
yarn run v1.19.2
$ node -r ts-node/register -r tsconfig-paths/register src/cli/index.ts lint -v .
/repro/schema.json
Missing baseUrl in compilerOptions. tsconfig-paths will be skipped
Found 81 rules (65 enabled)
Linting c:/_work/spectral/repro/schema.json
JSON Schema detected
JSON Schema (loose) detected
JSON Schema Draft 7 detected

c:/spectral/repro/schema.json
 1:1  error  invalid-ref  '#address' JSON pointer is invalid

✖ 1 problem (1 error, 0 warnings, 0 infos, 0 hints)

So, that's progress (the error looks a little more to the point), but indeed, it seems we're not there yet...

@nulltoken
Copy link
Contributor

@P0lip The invalid-ref error seems to point at json-ref-resolver.

Is it possible that it tries to reference the internal Json Schema reference as a Json Pointer?

@P0lip
Copy link
Contributor

P0lip commented Apr 30, 2020

Is it possible that it tries to reference the internal Json Schema reference as a Json Pointer?

I believe what it yells at is

"$ref": "#address" 

This is not a valid JSON Pointer.
In general, we don't support referencing $ids, so the example won't work.

@GrahamLea
Copy link
Author

Okay. I'm pretty new to JSON schema. Would you mind explaining briefly why it's not a valid pointer? Any ideas why it's documented in this popular online guide and it works in 2 schema parsers I'm using when it's not valid? Just trying to understand the landscape.

@P0lip
Copy link
Contributor

P0lip commented May 3, 2020

Would you mind explaining briefly why it's not a valid pointer?

Spectral does not support resolving based on $id - it's the property that's simply unrecognized by us.
If you want to reference a place in the document, you need to use a valid JSON Pointer, such as #/definitions/address, as explained in the extending section of the article you linked.

Any ideas why it's documented in this popular online guide and it works in 2 schema parsers I'm using when it's not valid? Just trying to understand the landscape.

Spectral is technically not a JSON Schema validator, therefore we don't support all of the resolving features that JSON Schema provides.
I'm not sure whether I was helpful or not, but please do feel free to ask further questions if something is uncertain.

@philsturgeon
Copy link
Contributor

philsturgeon commented May 4, 2020

Context: Seeing as Spectral is a tool which aims to work with OpenAPI and JSON Schema, but those two formats are only mostly compatible, there are a few quirks like this which pop up.

OpenAPI does not allow $id, so resolving via $id is commonly not supported in tooling which works with OpenAPI, even though a lot of that tooling says it supports JSON Schema too. I know. This is a mess.

Seeing as OpenAPI v3.1 will be proper JSON Schema, $id will be allowed, and all JSON Ref tooling is now scrambling to add proper support. We're ditching our own json-ref-resolver to help json-schema-ref-parser get better, and they're adding $id support, so soon this problem will have gone away.

tldr: for now please try and avoid $ref'ing to $id and point to its location in the file using a more standard JSON Pointer instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants