json-schema-validator
json-schema-validator copied to clipboard
urn:jsonschema:java:time:Instant is not supported as reference
There is error when validating using schema generated by jackson 2.9.6:
CODE:
public void generateSchema() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
JsonSchema schema = schemaGen.generateSchema(SampleEvent.class);
String stringSchema = mapper.writeValueAsString(schema);
JsonParser parser = mapper.getFactory().createParser(stringSchema);
JsonNode schemaNode = parser.readValueAsTree();
JsonSchemaFactory factory = JsonSchemaFactory.getInstance();
com.networknt.schema.JsonSchema schemaOut = factory.getSchema(schemaNode);
}
POJO:
@lombok.Data
public static class SampleEvent {
private Instant dateFrom;
private Instant dateTo;
}
SCHEMA:
{
"type": "object",
"id": "urn:jsonschema:net:sample:SimpleEvent",
"properties": {
"dateFrom": {
"type": "object",
"id": "urn:jsonschema:java:time:Instant",
"properties": {
"nano": {
"type": "integer"
},
"epochSecond": {
"type": "integer"
}
}
},
"dateTo": {
"type": "object",
"$ref": "urn:jsonschema:java:time:Instant"
}
}
}
ERROR:
at java.lang.String.substring(String.java:1967)
at com.networknt.schema.RefValidator.obtainAbsolutePath(RefValidator.java:94)
at com.networknt.schema.RefValidator.getRefSchema(RefValidator.java:60)
at com.networknt.schema.RefValidator.<init>(RefValidator.java:45)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.networknt.schema.ValidatorTypeCode.newValidator(ValidatorTypeCode.java:121)
at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:240)
... 41 more
The current Jackson version we are using is 2.9.5. Have you tried with it? Just trying to narrow the issue to ensure that it is the Jackson version issue. Thanks.
It is the same with 2.9.5. It generates the same schema. It seems that RefValidator is not able to resolve the "$ref": "urn:jsonschema:java:time:Instant" using id used in the previous attribute.
I don't think we support urn:jsonschema:java:time:Instant
in the validator. I have marked the issue with help wanted
. Let's see if someone from the community can get it fixed.
@matejsp The library has been enhanced by @jawaff and @flozano to support URI and URN. Could you please try it out again? If possible, could you add you use case to the unit test? Whether it works for not, it will help us to get the issue resolved. Thanks.
@stevehu This example actually wouldn't work with what I put together. Only standard URL schemes and the 'classpath'/'resource' schemes are supported right now (that is all configured in the constructor of JsonSchemaFactory.Builder). Custom URI schemes need to have a URIFactory and URIFetcher injected into the JsonSchemaFactory.Builder.
Now that I'm looking into it more, I'm realizing that there's actually a spec for a 'urn' URI scheme (https://tools.ietf.org/html/rfc8141#section-2). I did not add support for that scheme, but it can easily be done by creating a URIFactory for it. However, users will need to supply their own URIFetcher for the 'urn' scheme because there's no standard way to fetch a URN from what I can tell.
There is still similar problem with urn resolving (invalid segment for URI). URL factory: public URI create(final String uri)
This does not work:
return new URL(uri).toURI();
This works:
return new URI(uri);
The same goes regarding: public URI create(final URI baseURI, final String segment)
Hello, I'm away from my PC, but the URLFactory isn't for URNs. A URL is a valid URI, but a URN is not a valid URL. We should have a URNFactory, but we don't yet have built in support for URNs. It's possible to create your own URIFactory and URIFetcher for URNs, but they need to be injected into the JsonSchemaFactory.
On Sun, Sep 15, 2019, 11:16 PM Matej Spiller Muys [email protected] wrote:
There is still similar problem with urn resolving (invalid segment for URI). URL factory: public URI create(final String uri)
This does not work:
return new URL(uri).toURI();
This works:
return new URI(uri);
The same goes regarding: public URI create(final URI baseURI, final String segment)
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/networknt/json-schema-validator/issues/100?email_source=notifications&email_token=AAWC3XCGFK44HJXVNO3WVTDQJ4QCHA5CNFSM4FUIN45KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6YGEIA#issuecomment-531653152, or mute the thread https://github.com/notifications/unsubscribe-auth/AAWC3XEXCRA3RC3XWG7X663QJ4QCHANCNFSM4FUIN45A .
@stevehu @jawaff about URNFactory you can add the possiblity for customize the URISchemeFactory in JsonSchemaFactory? or you are going intention to add URNFactory? Or you have another solution for manage relative path?
@francesc79 Thanks for making this issue float again. Frankly, I haven't looked at this issue for a while as I am busy with a lot of other issues. I think @jawaff is in the same situation. Would you be able to try it out and submit a PR? Personally, I don't have a preference for either solution.
I need to use a URN in the $id to identify the schema. Since version 1.0.30 this is not working anymore. We don't want the URN to be able to be fetched (retrievable) at all (otherwise we would use a URL). In the combineCurrentUriWithIds method a JsonSchemaException is thrown.
@Arno538 Thanks a lot for raising the issue. Given that it was working before 1.0.30, chances are some updates in 1.0.30 broke the feature. Would you be able to compare the code base and find out the root cause and get it fixed? Thanks.
I think this issue is resolved in the latest version. If not, let's reopen this issue to address it. Thanks.