networknt / json-schema-validator

A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Apache License 2.0
855 stars 325 forks source link

com.networknt-json-schema-validator-1.2.0 - com.networknt.schema.JsonSchemaException - reference cannot be resolved #927

Closed vincent-tallier-jemmic closed 9 months ago

vincent-tallier-jemmic commented 9 months ago

I've an issue with the upgrade of com.networknt-json-schema-validator from version 1.1.0 to 1.2.0.

This is my schema: { "$schema": "http://json-schema.org/draft-07/schema#", "$id": "id", "type": "object", "title": "title", "anyOf": [ { "required": [ "id", "type", "genericSubmission" ] } ], "properties": { "id": { "type": "string", "title": "title" }, "type": { "type": "string", "title": "title" }, "genericSubmission": { "$ref": "#/definitions/genericSubmission" } }, "definitions": { "genericSubmission": { "$id": "#/definitions/genericSubmission", "type": "object", "title": "title", "required": [ "transactionReference", "title" ], "properties": { "transactionReference": { "type": "string", "title": "title", "description": "description" }, "title": { "type": "array", "minItems": 1, "items": { "type": "object", "required": [ "value", "locale" ], "properties": { "value": { "$ref": "#/definitions/value" }, "locale": { "$ref": "#/definitions/locale" } } } } } }, "value": { "$id": "#/definitions/value", "type": "string" }, "locale": { "$id": "#/definitions/locale", "type": "string", "default": "fr" } } }

and the associated file:

[ { "$schema": "./mySchema.json", "_comment": "comment", "id": "b34024c4-6103-478c-bad6-83b26d98a892", "type": "genericSubmission", "genericSubmission": { "transactionReference": "123456", "title": [ { "value": "[DE]...", "locale": "de" }, { "value": "[EN]...", "locale": "en" } ] } } ]

with `com.networknt-json-schema-validator-1.1.0, validation was OK.

with `com.networknt-json-schema-validator-1.2.0, validation is not OK.

I've such error. Any ideas ?

com.networknt.schema.JsonSchemaException: $.genericSubmission.title[0].value: Reference #/definitions/value cannot be resolved at com.networknt.schema.RefValidator.validate(RefValidator.java:244) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:441) at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:77) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:441) at com.networknt.schema.ItemsValidator.doValidate(ItemsValidator.java:104) at com.networknt.schema.ItemsValidator.validate(ItemsValidator.java:87) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:441) at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:77) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:441) at com.networknt.schema.RefValidator.validate(RefValidator.java:246) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:441) at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:77) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:441) at com.networknt.schema.BaseJsonValidator.validate(BaseJsonValidator.java:277) at com.networknt.schema.BaseJsonValidator.validate(BaseJsonValidator.java:309) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:531) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:517) at com.networknt.schema.JsonSchema.validate(JsonSchema.java:504)

justin-tay commented 9 months ago

The use of the $id keyword in a sub schema is typically used to denote a schema resource. The $ref within that schema resource is then resolved relative to that resource.

Can you remove all the $id keywords in your sub schemas? In any case the $id should not be containing a json pointer fragment so I'm not sure what is the intent.

vincent-tallier-jemmic commented 9 months ago

Good morning @justin-tay and thanks a lot for your comment ! It makes it working with the latest version ! Have a nice day !

vincent-tallier-jemmic commented 9 months ago

Issue closed as fixed when removing the $id as suggested !