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
822 stars 323 forks source link

Since 1.3.0: Warnings for unknown keywords in $defs #940

Closed aznan2 closed 7 months ago

aznan2 commented 7 months ago

After the upgrade I see log messages complaining of unknown keywords in $defs, so for example

String schema = """
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$ref": "#/$defs/greeting",
  "$defs": {
    "greeting": {}
  }
}""";
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);
factory.getSchema(schema);

will cause this log line 2024-01-29 16:19:59.545 WARN 24200 --- [ main] com.networknt.schema.JsonMetaSchema : Unknown keyword greeting - you should define your own Meta Schema. If the keyword is irrelevant for validation, just use a NonValidationKeyword.

It looks lite the NonValidationKeyword class sets up a special case for $defs and definitions, but it seems it doesn't apply anymore.

Taschee commented 7 months ago

We observed the same warnings

justin-tay commented 7 months ago

This should be fixed in the next version.

shivam-sharma7 commented 7 months ago

@justin-tay look at this, I got during migration with 1.3.0


[WARNING] COMPILATION WARNING :
[INFO] -------------------------------------------------------------
[WARNING] [options] system modules path not set in conjunction with -source 15
[INFO] 1 warning
[INFO] -------------------------------------------------------------
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] C:\Users\shivam sharma\Desktop\My Project\open-source\Horreum\horreum-backend\src\main\java\io\hyperfoil\tools\horreum\svc\SchemaServiceImpl.java:[82,31] error: package com.networknt.schema.uri does not exist
[ERROR] C:\Users\shivam sharma\Desktop\My Project\open-source\Horreum\horreum-backend\src\main\java\io\hyperfoil\tools\horreum\svc\SchemaServiceImpl.java:[83,31] error: package com.networknt.schema.uri does not exist
[ERROR] C:\Users\shivam sharma\Desktop\My Project\open-source\Horreum\horreum-backend\src\main\java\io\hyperfoil\tools\horreum\svc\SchemaServiceImpl.java:[84,31] error: package com.networknt.schema.uri does not exist
[ERROR] C:\Users\shivam sharma\Desktop\My Project\open-source\Horreum\horreum-backend\src\main\java\io\hyperfoil\tools\horreum\svc\SchemaServiceImpl.java:[117,24] error: cannot find symbol
  symbol:   class URIFactory
  location: class SchemaServiceImpl
[INFO] 4 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for Horreum 0.12-SNAPSHOT:
[INFO]
[INFO] Horreum ............................................ SUCCESS [  1.105 s]
[INFO] horreum-api ........................................ SUCCESS [ 17.519 s]
[INFO] Horreum Client ..................................... SUCCESS [  1.384 s]
[INFO] Horreum infra - common ............................. SUCCESS [  0.575 s]
[INFO] Horreum Dev Services - Parent ...................... SUCCESS [  0.039 s]
[INFO] Horreum Dev Services - Runtime ..................... SUCCESS [  3.374 s]
[INFO] Horreum Dev Services - Deployment .................. SUCCESS [  4.853 s]
[INFO] Horreum Backend .................................... FAILURE [  8.196 s]
[INFO] Horreum Integration Tests .......................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  39.943 s
[INFO] Finished at: 2024-01-30T07:36:10+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.12.1:compile (default-compile) on project horreum-backend: Compilation failure: Compilation failure:
[ERROR] C:\Users\shivam sharma\Desktop\My Project\open-source\Horreum\horreum-backend\src\main\java\io\hyperfoil\tools\horreum\svc\SchemaServiceImpl.java:[82,31] error: package com.networknt.schema.uri does not exist
[ERROR] C:\Users\shivam sharma\Desktop\My Project\open-source\Horreum\horreum-backend\src\main\java\io\hyperfoil\tools\horreum\svc\SchemaServiceImpl.java:[83,31] error: package com.networknt.schema.uri does not exist
[ERROR] C:\Users\shivam sharma\Desktop\My Project\open-source\Horreum\horreum-backend\src\main\java\io\hyperfoil\tools\horreum\svc\SchemaServiceImpl.java:[84,31] error: package com.networknt.schema.uri does not exist
[ERROR] C:\Users\shivam sharma\Desktop\My Project\open-source\Horreum\horreum-backend\src\main\java\io\hyperfoil\tools\horreum\svc\SchemaServiceImpl.java:[117,24] error: cannot find symbol
[ERROR]   symbol:   class URIFactory
[ERROR]   location: class SchemaServiceImpl
[ERROR] -> [Help 1]
justin-tay commented 7 months ago

@shivam-sharma7 that is not related to this issue. 1.3.0 contains breaking changes. You can look at the Upgrading to new versions document for details.

shivam-sharma7 commented 7 months ago

@justin-tay Thanks, can you look at the imports these are causing above errors. I don't know why?


import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.uri.URIFactory;
import com.networknt.schema.uri.URIFetcher;
import com.networknt.schema.uri.URLFactory;
justin-tay commented 7 months ago

These classes no longer exist.

import com.networknt.schema.uri.URIFactory;
import com.networknt.schema.uri.URIFetcher;
import com.networknt.schema.uri.URLFactory;

The replacement is SchemaLoader.

https://github.com/networknt/json-schema-validator/blob/e95642cf66ff031a728fecad5de8a1f6cd9e60af/src/main/java/com/networknt/schema/resource/SchemaLoader.java#L24-L32

You will need to configure it on the JsonSchemaFactory.

https://github.com/networknt/json-schema-validator/blob/e95642cf66ff031a728fecad5de8a1f6cd9e60af/src/main/java/com/networknt/schema/JsonSchemaFactory.java#L87-L93

This is how a typical implementation looks like

https://github.com/networknt/json-schema-validator/blob/e95642cf66ff031a728fecad5de8a1f6cd9e60af/src/main/java/com/networknt/schema/resource/MapSchemaLoader.java#L13-L36