Skip to content

Improved Ref Validator #141

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

Merged
merged 11 commits into from
Jun 7, 2019
Merged

Improved Ref Validator #141

merged 11 commits into from
Jun 7, 2019

Conversation

jawaff
Copy link
Contributor

@jawaff jawaff commented May 3, 2019

This pull request's aim is to eliminate some of the limitations with the RefValidator class.

Here are list of some of the limitations:

In order to solve these issues, I made it so that JsonSchemas are able to optionally store a 'currentUrl'. This currentUrl is effected by 'id' keywords, urls that are provided to the JsonSchemaFactory. The JsonSchemaFactory is operated by the user of this library as well as RefValidators that point to a remote schema.

All of this then allows the RefValidator to easily deal with a relative $ref. We deal with the relative $ref by simply using the constructor of the URL class. The URL constructor is able to automatically handle domain and subdirectory relative references. The URL constructor just requires a parent URL (which is provided by the JsonSchema's currentUrl) and some relative/absolute string path. If the parent URL is null, then the constructor requires the string path to be an absolute url. If the parent URL is nonnull, then the string path can be either an absolute url or a relative path (absolute urls in the string path are able to override the parent URL).

Looking at my changes though, the URL constructor isn't actually being used directly. I chose to use the existing URLFactory class in this project and added to it so that it can combine a parent URL with some string path. Doing this allows the RefValidator to support the 'classpath:' and 'resource:' protocols that were already working with this library.

Another thing to note is that I updated the interface for the JsonSchemaFactory. I essentially just duplicated the existing factory methods so that users can optionally provide the URL of the schema that they're dealing with. The JsonSchemaFactory is still backwards compatible and doesn't require the URL of the schema, but it can be supplied so that '$ref' and 'id' values are able to be relative to the schema's URL.

So in short, I basically updated the library so that it can rely on the URL constructor for the complicated logic of combining 'id' and '$ref' values with the url of a schema. The URL constructor basically handles all of what this validator needs to do (we even are able to add support for custom protocols) and that bit of code is builtin to Java, so I think this creates a robust and flexible solution.

Note1: I saw that unit tests (in the JsonSchemaTest file) with invalid schemas were succeeding, so I ended up making it so that those tests fail. There was one existing unit test that was silently failing and it has probably been silently failing in this project for a while. It had a reference to the src/test/resources/tests/folder/folderInteger.json file (which didn't exist). I figure that if a test schema isn't able to be loaded, then the unit test or the project probably needs to be fixed.

Note2: I fixed a unit test relating to issue #12 (i.e. SelfRefTest.java). It was loading a schema as a JsonNode (which was succeeding), but the test seemed like it should have loaded it as a JsonSchema. Since I fixed this unit test and it now correctly reproduces the problem in issue #12, the unit test now fails (as expected with a StackOverflow). I just added an @ignore annotation until someone provides a fix.

Jake Waffle added 6 commits May 3, 2019 13:25

Unverified

This user has not yet uploaded their public signing key.
…ive references.

Unverified

This user has not yet uploaded their public signing key.
…s have to be valid for the unit tests.

Verified

This commit was signed with the committer’s verified signature.
80avin Avinash Thakur

Verified

This commit was signed with the committer’s verified signature.
80avin Avinash Thakur
…tly reproduces the problem described in issue networknt#12.

Verified

This commit was signed with the committer’s verified signature.
80avin Avinash Thakur
…special license in order to use in production, so it's probably safe to eliminate full support for it. The issues that exist in Java 1.8 are only relevant to relative values that have upper case characters.

Verified

This commit was signed with the committer’s verified signature.
80avin Avinash Thakur
… version back to 1.8. I just was skipping the problematic unit test due to some problems in my dev environment.
@jawaff
Copy link
Contributor Author

jawaff commented May 4, 2019

My development environment has a proxy, so some of the unit tests cause connection timeouts. I had some problems with a slight bug that I introduced into a schema, but I eventually figured out what the problem was. I initially thought that it had something to do with my Java version (I'm using OpenJDK 11 and Travis is using Oracle JDK 1.8), but it was just a simple typo in the end.

@jawaff
Copy link
Contributor Author

jawaff commented May 4, 2019

On a positive note, if I didn't add in that exception for the invalid schemas, then my previous commits would have passed the unit tests. Silently failing tests can be a bad thing potentially.

@jawaff
Copy link
Contributor Author

jawaff commented May 4, 2019

Anyway, I'm ready for this pull request to be reviewed. I'm done adding in the improvements that I was interested in. There's still some features that this library doesn't support regarding $refs, but I think it supports the main use cases and that seems fine to me.

Jake Waffle added 2 commits May 6, 2019 08:10

Verified

This commit was signed with the committer’s verified signature.
80avin Avinash Thakur
…t the same behavior by supplying a schemaUrl and utilizing a custom URLFetcher.

Verified

This commit was signed with the committer’s verified signature.
80avin Avinash Thakur
…g a URL along with a JsonNode. The schema could be embedded into a json file and may still need to have a URL location (we use this for testing purposes and I broke our tests with the last commit).
@jawaff
Copy link
Contributor Author

jawaff commented May 6, 2019

I updated the JsonSchemaFactory a bit. I previously thought that I should add method overloads for everything so that users can associate a URL with some given schema String/InputStream. However, the user can just pass in a URL and then implement a custom URLFetcher to do whatever they need.

I did decide to keep the overload that our JsonSchemaTest currently is using. This particular use case isn't easily fixed by a URLFetcher because the schema can potentially exist within a larger json file, but may still need a URL to be associated with.

@jawaff
Copy link
Contributor Author

jawaff commented May 17, 2019

This project is once again up to date and ready to merge. I ran into an issue with the URL mapping while I was fixing a merge conflict in the RefValidator, but that is all worked out now.

@stevehu
Copy link
Contributor

stevehu commented May 17, 2019

@jawaff I have reviewed the PR and want another developer to review it before merging. The idea is to skip this weekend's release as well due to some emergency bug fixes in this library.

@stevehu
Copy link
Contributor

stevehu commented May 25, 2019

@jawaff We have got the emergency fix 1.0.10 out and going back to this PR. Could you please resolve the conflicts. Thanks a lot for your help.

@stevehu
Copy link
Contributor

stevehu commented Jun 7, 2019

@jawaff I have merged the PR and released version 1.0.14. Thanks a lot for your help. It is a huge PR and it takes some time to review.

https://github.com/networknt/json-schema-validator/releases/tag/1.0.14

@jawaff
Copy link
Contributor Author

jawaff commented Jun 7, 2019 via email

@stevehu
Copy link
Contributor

stevehu commented Jun 7, 2019

No worry. I made a little adjustment after the merging and it works perfectly. Again, thanks for the help as this PR resolves a lot of pending issues.

@flozano
Copy link
Contributor

flozano commented Jun 18, 2019

One Q, why using URL everywhere instead of URI? URL doesn't allow using URN values.

We'd like to extend the validator to register some well-known URNs for some internal schemas, without publishing them in a valid URL, but we don't think it's possible in a clean way with current code-base, so if we want to use this library we need to replace those URNs to URLs in a pre-processing which shouldn't be required.

@jawaff
Copy link
Contributor Author

jawaff commented Jun 19, 2019 via email

@flozano
Copy link
Contributor

flozano commented Jun 19, 2019

Why do you want to dictate that schemas should be resolv'able thru URLs? Some people can have schemas ( I do :) ) that may have different URIs resolvable thru different means. An URN is a more generic form that allows me to plug whatever I want to retrieve those. With some pluggability, it'd be very convenient to say that URIs in the form of "http://..." are resolved with your URL resolver, whereas URIs in the form of "urn:mycompany:...." are resolved in the way I want

@flozano
Copy link
Contributor

flozano commented Jun 19, 2019

(discussion ongoing in #166)

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

Successfully merging this pull request may close these issues.

None yet

3 participants