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.1: unevaluatedProperties doesn't work with external schemas #943

Closed aznan2 closed 7 months ago

aznan2 commented 7 months ago

If a schema contains all properties then unevaluatedProperties works as expected, but if some properties come from an external schema then they are ignored. So, given this instance:

{
  "type": "Point",
  "coordinates": [1, 1]
}

The following schema will work:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$ref": "#/$defs/point",
  "unevaluatedProperties": false,
  "$defs": {
    "point": {
      "properties": {
        "type": {
          "const": "Point"
        },
        "coordinates": {
          "type": "array"
        }
      }
    }
  }
}

But this will not, resulting in error messages relating to unevaluated properties $.type and $.coordinates:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$ref": "https://geojson.org/schema/Point.json",
  "unevaluatedProperties": false
}
justin-tay commented 7 months ago

This is happening because I have some logic to determine if annotations need to be collected for evaluation and one of the checks is checking if the unevaluatedProperties keyword exists in the meta-schema. In this case the schema at https://geojson.org/schema/Point.json is a Draft 7 schema so it doesn't collect the annotations. It sounds like I may need to remove that check.

As a workaround you can switch on annotation collection.

        String schemaData = "{\r\n"
                + "  \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\r\n"
                + "  \"$ref\": \"https://geojson.org/schema/Point.json\",\r\n"
                + "  \"unevaluatedProperties\": false\r\n"
                + "}";

        String inputData = "{\r\n"
                + "  \"type\": \"Point\",\r\n"
                + "  \"coordinates\": [1, 1]\r\n"
                + "}";
        JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012);
        SchemaValidatorsConfig config = new SchemaValidatorsConfig();
        config.setPathType(PathType.JSON_POINTER);
        JsonSchema schema = factory.getSchema(schemaData, config);
        System.out.println(schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL, executionContext -> {
            executionContext.getExecutionConfig().setAnnotationCollectionEnabled(true);
            executionContext.getExecutionConfig().setAnnotationCollectionPredicate(keyword -> true);
        }));

The results if annotation collection is turned on.

{
  "valid" : true,
  "evaluationPath" : "",
  "schemaLocation" : "#",
  "instanceLocation" : "",
  "annotations" : {
    "unevaluatedProperties" : [ ]
  },
  "details" : [ {
    "valid" : true,
    "evaluationPath" : "/$ref",
    "schemaLocation" : "https://geojson.org/schema/Point.json#",
    "instanceLocation" : "",
    "annotations" : {
      "properties" : [ "type", "coordinates" ],
      "title" : "GeoJSON Point"
    },
    "details" : [ {
      "valid" : true,
      "evaluationPath" : "/$ref/properties/coordinates",
      "schemaLocation" : "https://geojson.org/schema/Point.json#/properties/coordinates",
      "instanceLocation" : "/coordinates",
      "annotations" : {
        "items" : true
      }
    } ]
  } ]
}
aznan2 commented 7 months ago

Thanks! I tried making a copy of the Point schema locally and change it to 2020-12, but I still get the same thing. As for the annotation collection, the setAnnotationCollectionEnabled() method is protected, so it can't be accessed.

justin-tay commented 7 months ago

Must have used the wrong access modifier by mistake. Shall also fix that.