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.

Referencing other local JSON Schemas

See original GitHub issue

I have strong doubts whether this is a bug or not, but it is definitely a nice-to-have feature which is foreseen by the JSON Schema specification.

What the specification states

The Schema References With “$ref” section states the following:

Schemas can be identified by any URI that has been given to them, including a JSON Pointer or their URI given directly by “$id”. In all cases, dereferencing a “$ref” reference involves first resolving its value as a URI reference against the current base URI per RFC 3986.

As an example it provides the following simple case:

When an implementation encounters the reference to "other.json", it resolves this to <http://example.net/other.json>, which is not defined in this document. If a schema with that identifier has otherwise been supplied to the implementation, it can also be used automatically.

And this is how it works in the latest version of the Manifold, no offense here. 😄 We are able to reference the schema with the same base URI, i.e. on the same level in the filesystem’s directory structure. It compiles and it runs smoothly.

At the same time, it is impossible to introduce any hierarchical structure on top of one’s schemas, i.e. to use subdirectories and reference other schemas relatively in both directions: forth with "sub/other.json" and back with "../other.json".

But there are these “can be identified by any URI” and “In all cases <…> resolving its value as a URI reference against the current base URI” parts that make me believe that such a scenario is also considered possible, yet omitted in the current Manifold’s implementation.

What the specification doesn’t state yet

What causes my doubts is an associated cite reference:

What should implementations do when the referenced schema is not known? Are there circumstances in which automatic network dereferencing is allowed? A same origin policy? A user-configurable option? In the case of an evolving API described by Hyper-Schema, it is expected that new schemas will be added to the system dynamically, so placing an absolute requirement of pre-loading schema documents is not feasible.

While it is not feasible in the wild (a.k.a. World Wild Web), we are dealing with a special case of “static resources as Java Types” and it would seem that it doesn’t matter much. However, there might be matters to discuss.

What everyone (including myself) definitely wants to be able to do is to work with such references without fuss in the Manifold’s “dynamic mode”.

Scenarios in scope

Test 1

Checks an ability to reference the "sub/other.json" schema from the current one.

Manifold is unable to compile such a schema in the current implementation:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project manifold-json-subschema-test: Compilation failure: Compilation failure:
[ERROR] /abc/res/Test1.java:[39,11] cannot find symbol
[ERROR] symbol:   class SubTest1
[ERROR] location: interface abc.res.Test1
[ERROR] /abc/res/Test1.java:[44,27] cannot find symbol
[ERROR] symbol:   class SubTest1
[ERROR] location: interface abc.res.Test1
[ERROR] /abc/res/Test1.java:[70,32] cannot find symbol
[ERROR] symbol:   class SubTest1
[ERROR] location: class abc.res.Test1.Builder
[ERROR] /abc/res/Test1.java:[82,27] cannot find symbol
[ERROR] symbol:   class SubTest1
[ERROR] location: class abc.res.Test1.Copier 

It also happens that Manifold’s IntelliJ plugin is dealing fine (w/o "No such file or directory" error) with such a "$ref".

test-1

Test 2

Checks an ability to back-reference the "../other.json" schema from the current one.

⚠️ In case Test 1 succeeds, this one is also expected to pass according to the URI Relative Resolution rules.

Now it gives almost the same compilation error:

[ERROR] /abc/res/sub/SubTest2.java:[39,11] cannot find symbol
[ERROR] symbol:   class Test2
[ERROR] location: interface abc.res.sub.SubTest2
[ERROR] /abc/res/sub/SubTest2.java:[44,25] cannot find symbol
[ERROR] symbol:   class Test2
[ERROR] location: interface abc.res.sub.SubTest2
[ERROR] /abc/res/sub/SubTest2.java:[70,30] cannot find symbol
[ERROR] symbol:   class Test2
[ERROR] location: class abc.res.sub.SubTest2.Builder
[ERROR] /abc/res/sub/SubTest2.java:[82,25] cannot find symbol
[ERROR] symbol:   class Test2
[ERROR] location: class abc.res.sub.SubTest2.Copier

And is equally located by the IntelliJ plugin.

test-2


As always, test project is available here: https://github.com/marksto/manifold-json-subschema-test

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
rsmckinneycommented, May 8, 2019

Fix available in release 0.65-alpha

0reactions
markstocommented, May 17, 2019

Superb, will check this out soon!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Referencing a local relative file in a JSON Schema?
What you need there is being able to reference your files using an absolute URI. Supposing you are using the stable version (ie...
Read more >
How to reference a local JSON schema - iancarpenter.dev
One sentence summary This post will show you how to use a locally stored JSON schema to validate a JSON document.
Read more >
Structuring a complex schema — Understanding JSON ...
A schema can reference another schema using the $ref keyword. The value of $ref is a URI-reference that is resolved against the schema's...
Read more >
[json] Referencing **local** JSON schema from another local ...
Steps to Reproduce: Reference one JSON schema from another. Please note that I'm not talking about referencing a JSON schema for a given...
Read more >
JSON References - NIEM GitHub
JSON References. In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a...
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