Skip to content
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

networknt / json-schema-validator - Unknown Metaschema: http://json-schema.org/draft-07/schema# #273

Closed
harpreetk1986 opened this issue Mar 18, 2020 · 20 comments · Fixed by #400

Comments

@harpreetk1986
Copy link

Hi,

While using networknt / json-schema-validator, I am getting error while validating schema -06 and above. Getting following error:
com.networknt.schema.JsonSchemaException: Unknown Metaschema: http://json-schema.org/draft-07/schema#

Only schema-04 is working fine.

Here is sample schema I am using:
{
"$schema" : "http://json-schema.org/draft-07/schema#",
"$id" : "http://json-schema.org/draft-07/schema#"
}

@stevehu
Copy link
Contributor

stevehu commented Mar 18, 2020

@harpreetk1986 Thanks a lot for raising this issue. I am wondering if this is due to the recent update on the json-schema.org to redirect all HTTP requests to HTTPS. I have updated the draft-2019-09 at this location. https://github.com/networknt/json-schema-validator/blob/master/src/main/java/com/networknt/schema/JsonMetaSchema.java#L151

Have you debugged into it to see where the error message is coming from?

@harpreetk1986
Copy link
Author

@stevehu. Thanks for the reply. I just did a debug and found the following:

Issue is coming in findMetaSchemaForSchema() function of JsonSchemaFactory Class at below line of code:
final JsonMetaSchema jsonMetaSchema = jsonMetaSchemas.get(uri);

Here uri is "http://json-schema.org/draft-07/schema#"
jsonMetaSchema is coming as null.

@harpreetk1986
Copy link
Author

@stevehu : I debug more and found out that, I am calling JsonSchemaFactory.getInstance() and this function is always returning Version flag as V4. Do I need to call getInstance() by passing the version flag. If yes, from where should I fetch that from?

@stevehu
Copy link
Contributor

stevehu commented Mar 18, 2020

Yes. You need to specify which draft version to use unless you are using v4 which is the default.

https://github.com/networknt/json-schema-validator/blob/master/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java#L39

@stevehu
Copy link
Contributor

stevehu commented Mar 18, 2020

To prevent users to use the old method. I am thinking to deprecate the getInstance(). What do you think?

+    /**
+     * @deprecated
+     * This is a method that is kept to ensure backward compatible. You shouldn't use it anymore.
+     * Please specify the draft version when get an instance.
+     *
+     * @return JsonSchemaFactory
+     */
+    @Deprecated
     public static JsonSchemaFactory getInstance() {
         return getInstance(SpecVersion.VersionFlag.V4);
     }

@harpreetk1986
Copy link
Author

@stevehu . It is a good idea, it makes complete sense to me.

I am thinking , how it can be more dynamic to pass the version flag which supports multiple versions. Since schema can contain any draft version. May be we have to fetch the Draft versions from Schema and create mapping to map to the corresponding version to pass as parameters of getInstance has format like V4, V6 etc.

And once if new draft version is released, that needs to be added to the mapping to map to the corresponding version to pass.

@stevehu
Copy link
Contributor

stevehu commented Mar 18, 2020

I thought about it when I implement the versioning. We can just decide the version based on the $schema in the schema file; however, we cannot make a decision for the reference schemas if there are any. For example, if you have a schema v7 that refers to another remote schema v4. Which version should the library use? To let the user specify the version at least make them aware of this issue. The other reason is that not all schema files have the $schema.

@stevehu stevehu reopened this Mar 18, 2020
@stevehu
Copy link
Contributor

stevehu commented Mar 18, 2020

I just checked in the @deprecated code. It is not the final solution but just to reduce the risk. Let's keep this conversation going to have a better solution.

@harpreetk1986
Copy link
Author

I just checked in the @deprecated code. It is not the final solution but just to reduce the risk. Let's keep this conversation going to have a better solution.

Sure. Thank you.

@harpreetk1986
Copy link
Author

I thought about it when I implement the versioning. We can just decide the version based on the $schema in the schema file; however, we cannot make a decision for the reference schemas if there are any. For example, if you have a schema v7 that refers to another remote schema v4. Which version should the library use? To let the user specify the version at least make them aware of this issue. The other reason is that not all schema files have the $schema.

Sure, this way also it will work for me. Thanks.

@andrhamm
Copy link

I'm getting this error and I believe I'm specifying the correct version

val config = SchemaValidatorsConfig()
config.isFailFast = true
val mapper = ObjectMapper()
val validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build();
validatorFactory.getSchema(schemaString, config)

@stevehu
Copy link
Contributor

stevehu commented Mar 20, 2020

@andrhamm Thanks for raising it. It looks like the scope of the problem is getting bigger. I will take a look at it once I have free cycles. It would be great if someone in the community can take a look at your case.

@mcoder2014
Copy link

I miss the same problem, I can't generate a JsonSchema from my JSON schema file of draft-07.
It throws the exceptions like blew . How can I make the program run successfully?

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jsonSchemaService' defined in file [/projects/java-vadilation/target/classes/com/example/javavadilation/service/JsonSchemaService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getJsonSchemaFromClasspath' defined in class path resource [com/example/javavadilation/config/JsonSchemaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.networknt.schema.JsonSchema]: Factory method 'getJsonSchemaFromClasspath' threw exception; nested exception is com.networknt.schema.JsonSchemaException: Unknown Metaschema: https://json-schema.org/draft-07/schema#
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1358)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
	at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
	... 61 more

@stevehu
Copy link
Contributor

stevehu commented May 24, 2020

@mcoder2014 It looks like the stack trace is part of the spring framework and misses the important info. I am wondering if you could create a small test case to reproduce the issue. Even better if you could debug into it to find the root cause. Thanks.

@mcoder2014
Copy link

If I use "$schema": "http://json-schema.org/draft-07/schema" as $schema, there are the trace log.

Exception in thread "main" com.networknt.schema.JsonSchemaException: Unknown Metaschema: http://json-schema.org/draft-07/schema
	at com.networknt.schema.JsonSchemaFactory.findMetaSchemaForSchema(JsonSchemaFactory.java:281)
	at com.networknt.schema.JsonSchemaFactory.createValidationContext(JsonSchemaFactory.java:272)
	at com.networknt.schema.JsonSchemaFactory.newJsonSchema(JsonSchemaFactory.java:264)
	at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:296)
	at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:304)
	at com.example.schema.JsonValidationMain.main(JsonValidationMain.java:29)

If I change the "$schema" into http://json-schema.org/draft-07/schema#, there will trace log like this. I don't know why there will be NullPointerException....

Exception in thread "main" com.networknt.schema.JsonSchemaException: java.lang.NullPointerException
	at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:349)
	at com.networknt.schema.ValidationContext.newValidator(ValidationContext.java:53)
	at com.networknt.schema.JsonSchema.read(JsonSchema.java:198)
	at com.networknt.schema.JsonSchema.initialize(JsonSchema.java:76)
	at com.networknt.schema.JsonSchemaFactory.newJsonSchema(JsonSchemaFactory.java:267)
	at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:296)
	at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:304)
	at com.example.schema.JsonValidationMain.main(JsonValidationMain.java:29)
Caused by: java.lang.NullPointerException
	at com.networknt.schema.JsonSchema.combineCurrentUriWithIds(JsonSchema.java:90)
	at com.networknt.schema.JsonSchema.<init>(JsonSchema.java:72)
	at com.networknt.schema.JsonSchema.<init>(JsonSchema.java:58)
	at com.networknt.schema.PropertiesValidator.<init>(PropertiesValidator.java:35)
	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:130)
	at com.networknt.schema.JsonMetaSchema.newValidator(JsonMetaSchema.java:342)
	... 7 more

@stevehu
Copy link
Contributor

stevehu commented May 25, 2020

I think the URL is changed to https from http a couple of months ago. Can you try https?

@mcoder2014
Copy link

Now it is still unknown Metaschema:

Exception in thread "main" com.networknt.schema.JsonSchemaException: Unknown Metaschema: https://json-schema.org/draft-07/schema#
	at com.networknt.schema.JsonSchemaFactory.findMetaSchemaForSchema(JsonSchemaFactory.java:281)
	at com.networknt.schema.JsonSchemaFactory.createValidationContext(JsonSchemaFactory.java:272)
	at com.networknt.schema.JsonSchemaFactory.newJsonSchema(JsonSchemaFactory.java:264)
	at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:296)
	at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:304)
	at com.example.schema.JsonValidationMain.main(JsonValidationMain.java:29)

@stevehu
Copy link
Contributor

stevehu commented May 26, 2020

I tried to reproduce the issue but couldn't. I am wondering if you could submit a PR with a test case to reproduce the issue. Thanks a lot for raising it.

@mcoder2014
Copy link

Thanks, I have commit a pull request with a small test case. It is small but can't run on my computer.

Hope you can find the cause about the problem.

@stevehu
Copy link
Contributor

stevehu commented May 27, 2020

Thanks a lot for raising this issue and joining the discussion. I have added a normalize method to switch the protocol to https and remove the query parameters and fragment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants