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

Error when resolving "$ref" with "dot slash" paths #950

Closed deryoman closed 7 months ago

deryoman commented 7 months ago

Describe the bug

When loading a schema via IRL, $ref files cannot be loaded when using the "dot slash" path e.g: ./file.json

To Reproduce

Loading the following schema via an IRL

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "$id": "test.json",
  "title": "test",
  "type": "object",
  "examples": [
    {
      "description": "Foo",
      "uuid": "092BBF46-4069-11E7-9DF9-024BEA7C7CAF"
    }
  ],
  "required": [
    "description",
    "uuid"
  ],
  "properties": {
    "description": {
      "type": "string"
    },
    "uuid": {
      "$ref": "./uuid.json"
    }
  }
}

Java Code:

JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
JsonSchema jsonSchema = jsonSchemaFactory.getSchema(SchemaLocation.of("https://example.com/test.json"));
jsonSchema.validate(" {
      "description": "Bar",
      "uuid": "092BBF46-4069-11E7-9DF9-024BEA7C7CAF"
    }");

This will lead to a FileNotFoundException because it will try to resolve https://example.com/./uuid.json

com.networknt.schema.JsonSchemaException: Failed to load json schema fromhttps://example.com/./uuid.json
    at com.networknt.schema.JsonSchemaFactory.getMappedSchema(JsonSchemaFactory.java:521)
    at com.networknt.schema.JsonSchemaFactory.getSchema(JsonSchemaFactory.java:464)
    at com.networknt.schema.RefValidator.lambda$getRefSchema$0(RefValidator.java:64)
    at com.networknt.schema.CachedSupplier.get(CachedSupplier.java:36)
    at com.networknt.schema.JsonSchemaRef.getSchema(JsonSchemaRef.java:32)
    at com.networknt.schema.RefValidator.validate(RefValidator.java:173)
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:569)
    at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:85)
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:569)
    at com.networknt.schema.RefValidator.validate(RefValidator.java:181)
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:569)
    at com.networknt.schema.PropertiesValidator.validate(PropertiesValidator.java:85)
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:569)
    at com.networknt.schema.ItemsValidator.doValidate(ItemsValidator.java:152)
    at com.networknt.schema.ItemsValidator.validate(ItemsValidator.java:122)
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:569)
    at com.networknt.schema.BaseJsonValidator.validate(BaseJsonValidator.java:287)
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:879)
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:702)
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:682)
    at com.networknt.schema.JsonSchema.validate(JsonSchema.java:629)
        ... 4 more
Caused by: java.io.FileNotFoundException: https://example.com/./uuid.json
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1994)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1599)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:223)
    at com.networknt.schema.resource.UriSchemaLoader.openConnectionCheckRedirects(UriSchemaLoader.java:51)
    at com.networknt.schema.resource.UriSchemaLoader.lambda$getSchema$0(UriSchemaLoader.java:36)
    at com.networknt.schema.JsonSchemaFactory.getMappedSchema(JsonSchemaFactory.java:494)
    ... 24 more

In versions < 1.3.0 it worked this way. Changing the schema to "$ref": "uuid.json" fixes this issue, but it should also work using dot slash paths.

Env