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

Since 1.3.1: unevaluatedProperties doesn't work with external schemas #943

Closed
aznan2 opened this issue Feb 1, 2024 · 3 comments · Fixed by #944
Closed

Since 1.3.1: unevaluatedProperties doesn't work with external schemas #943

aznan2 opened this issue Feb 1, 2024 · 3 comments · Fixed by #944

Comments

@aznan2
Copy link
Contributor

aznan2 commented Feb 1, 2024

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
Copy link
Contributor

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
Copy link
Contributor Author

aznan2 commented Feb 1, 2024

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
Copy link
Contributor

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

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

Successfully merging a pull request may close this issue.

2 participants