Skip to content

Support for $ref external schema references? #55

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

Closed
harold-owen opened this issue Dec 19, 2017 · 9 comments
Closed

Support for $ref external schema references? #55

harold-owen opened this issue Dec 19, 2017 · 9 comments

Comments

@harold-owen
Copy link

We have a schema that refers to three other schemas using $ref. The others are at the same relative path (folder). Trying to resolve by URL. The first schema is:
https://scap.nist.gov/schema/nvd/feed/0.1/nvd_cve_feed_json_0.1_beta.schema

The others are
https://scap.nist.gov/schema/nvd/feed/0.1/cvss-v3.0.json
https://scap.nist.gov/schema/nvd/feed/0.1/cvss-v2.0.json
https://scap.nist.gov/schema/nvd/feed/0.1/CVE_JSON_4.0_min.schema

Getting warning in log:
16:27:08,402 WARN [com.networknt.schema.JsonMetaSchema] (quartzScheduler_Worker-1) Could not load validator $ref

Any tips for troubleshooting? Is $ref supported by json-schema-validator? Nice library BTW, it has saved us a lot of time.

@stevehu
Copy link
Contributor

stevehu commented Dec 20, 2017

{"$ref": "CVE_JSON_4.0_min.schema"} cannot be resolved normally as json schema reference should be a uri. If you are using the uri (https://scap.nist.gov/schema/nvd/feed/0.1/CVE_JSON_4.0_min.schema) it should work. If you want to refer to local filesystem, you need to take a look at #45

@harold-owen
Copy link
Author

Thank you. I will try this and report back.

@2beers
Copy link

2beers commented Jan 7, 2018

Hello. I've used version 0.1.3 with $ref external schemas without any problem. Today I decided to upgrade to 0.1.13 and I received the following error:

Caused by: java.lang.NullPointerException
	at com.networknt.schema.RefValidator.obtainAbsolutePath(RefValidator.java:92)
	at com.networknt.schema.RefValidator.getRefSchema(RefValidator.java:60)
	at com.networknt.schema.RefValidator.<init>(RefValidator.java:45)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
	at com.networknt.schema.ValidatorTypeCode.newValidator(ValidatorTypeCode.java:102)
	at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:241)

in my json schema I have something like this
"$ref":"file:path/file.json"

is this not working anymore?

@stevehu
Copy link
Contributor

stevehu commented Jan 8, 2018

@2beers There were some changes since then and the local $ref was changed and documented in #45.

@2beers
Copy link

2beers commented Jan 8, 2018

I managed to make it work.
With new version it is required to have an id property on parent.
Is this the standard as well? If yes, you should detect that the input schema is not valid and throw an error.

Also I think there is a bug on https://github.com/networknt/json-schema-validator/blob/master/src/main/java/com/networknt/schema/RefValidator.java line 94

int index = baseSchemaUrl.lastIndexOf("/");
baseSchemaUrl = baseSchemaUrl.substring(0, index);

This requires for the id to contain at least one "/" charater, which in my opinion is wrong

I could simply have an id like this classpath:schema.json or file:schema.json

I easy fix this because I put all my .json files in a folder and defined the id like this:
id:"folder/schema.json"

@stevehu
Copy link
Contributor

stevehu commented Jan 8, 2018

@2beers The previous version is implemented as schema v4 and now it is more close to v6 in $ref implementation. I am planning to rewrite this library so that users can choose which validator to use v4 or v6 but never got enough time to commit. The idea is to use light-4j service module to inject different implementations in a service.yml file. It would be very easy for customization as well. Take a look at openapi-parser validator implementation for example.

@2beers
Copy link

2beers commented Jan 9, 2018

I've never tried light-4j so I can't comment, however I think you should add a condition where the id doesn't contain any "/" character to avoid throwing errors.

@stevehu
Copy link
Contributor

stevehu commented Jan 9, 2018

@2beers could you please create a pull request? Thanks.

@stevehu
Copy link
Contributor

stevehu commented Jun 7, 2019

This issue should be resolved by PR #141

@stevehu stevehu closed this as completed Jun 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants