Skip to content

urn:jsonschema:java:time:Instant is not supported as reference #100

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
matejsp opened this issue Sep 10, 2018 · 12 comments
Closed

urn:jsonschema:java:time:Instant is not supported as reference #100

matejsp opened this issue Sep 10, 2018 · 12 comments

Comments

@matejsp
Copy link

matejsp commented Sep 10, 2018

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
@stevehu
Copy link
Contributor

stevehu commented Sep 10, 2018

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.

@matejsp
Copy link
Author

matejsp commented Sep 30, 2018

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.

@stevehu
Copy link
Contributor

stevehu commented Jan 22, 2019

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.

@stevehu
Copy link
Contributor

stevehu commented Jun 25, 2019

@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.

@jawaff
Copy link
Contributor

jawaff commented Jun 25, 2019

@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.

@stevehu stevehu changed the title Jackson schema generator incompatibility urn:jsonschema:java:time:Instant is not supported as reference Aug 7, 2019
@matejsp
Copy link
Author

matejsp commented Sep 16, 2019

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)

@jawaff
Copy link
Contributor

jawaff commented Sep 16, 2019 via email

@francesc79
Copy link

francesc79 commented Mar 19, 2020

@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?

@stevehu
Copy link
Contributor

stevehu commented Mar 19, 2020

@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.

@Arno538
Copy link

Arno538 commented Apr 16, 2020

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.

@stevehu
Copy link
Contributor

stevehu commented Apr 17, 2020

@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.

@stevehu
Copy link
Contributor

stevehu commented Feb 1, 2024

I think this issue is resolved in the latest version. If not, let's reopen this issue to address it. Thanks.

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

No branches or pull requests

5 participants