confluentinc / schema-registry

Confluent Schema Registry for Kafka
https://docs.confluent.io/current/schema-registry/docs/index.html
Other
2.21k stars 1.11k forks source link

Polymorphic payload in json schema produce invalid diff #2849

Open tfactor2 opened 10 months ago

tfactor2 commented 10 months ago

Hi, Having polymorphic property (using anyOf) with different additionalProperties values produce unexpected results. Expected: only additionalProperties related differences (ADDITIONAL_PROPERTIES_ADDED). Actual:

[
Difference{jsonPath='#/properties/parent/anyOf/0/properties/child2NestedField', type=OPTIONAL_PROPERTY_ADDED_TO_UNOPEN_CONTENT_MODEL}, 
Difference{jsonPath='#/properties/parent/anyOf/0/properties/child1NestedField', type=PROPERTY_REMOVED_FROM_OPEN_CONTENT_MODEL}, 
Difference{jsonPath='#/properties/parent/anyOf/0/additionalProperties', type=ADDITIONAL_PROPERTIES_ADDED}, 
Difference{jsonPath='#/properties/parent/anyOf/1/properties/child2NestedField', type=PROPERTY_REMOVED_FROM_OPEN_CONTENT_MODEL}, 
Difference{jsonPath='#/properties/parent/anyOf/1/properties/child1NestedField', type=OPTIONAL_PROPERTY_ADDED_TO_UNOPEN_CONTENT_MODEL}, 
Difference{jsonPath='#/properties/parent/anyOf/1/additionalProperties', type=ADDITIONAL_PROPERTIES_ADDED}, 
Difference{jsonPath='#/additionalProperties', type=ADDITIONAL_PROPERTIES_ADDED}
]

Java code:

JsonSchema src = new JsonSchema("""
                {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "parent": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "child1NestedField": {
              "type": "string"
            },
            "parentNestedField": {
              "type": "string"
            },
            "name": {
              "const": "Child1"
            }
          },
          "additionalProperties": false,
          "required": [
            "name"
          ]
        },
        {
          "type": "object",
          "properties": {
            "child2NestedField": {
              "type": "string"
            },
            "parentNestedField": {
              "type": "string"
            },
            "name": {
              "const": "Child2"
            }
          },
          "additionalProperties": false,
          "required": [
            "name"
          ]
        }
      ]
    },
    "schemaVersion": {
      "type": "string",
      "default": "1.0"
    }
  },
  "additionalProperties": false,
  "required": [
    "schemaVersion"
  ]
}
                """);
        JsonSchema target = new JsonSchema("""
                {
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "parent": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "child1NestedField": {
              "type": "string"
            },
            "parentNestedField": {
              "type": "string"
            },
            "name": {
              "const": "Child1"
            }
          },
          "additionalProperties": true,
          "required": [
            "name"
          ]
        },
        {
          "type": "object",
          "properties": {
            "child2NestedField": {
              "type": "string"
            },
            "parentNestedField": {
              "type": "string"
            },
            "name": {
              "const": "Child2"
            }
          },
          "additionalProperties": true,
          "required": [
            "name"
          ]
        }
      ]
    },
    "schemaVersion": {
      "type": "string",
      "default": "1.0"
    }
  },
  "additionalProperties": true,
  "required": [
    "schemaVersion"
  ]
}
                """);
        var result = SchemaDiff
                .compare(src.rawSchema(), target.rawSchema());
rayokota commented 10 months ago

Thanks @tfactor2 . The goal of the code is to see if there is a compatible set of differences, not necessarily to find the optimal delta between the two schemas. In this case it found that even if src.anyOf[0] is compared to target.anyOf[1] and src.anyOf[1] is compared to target.anyOf[0], the schemas are still compatible.

tfactor2 commented 10 months ago

@rayokota, thx for the response. But the library returns sets of differences that can be interpreted as incompatible: OPTIONAL_PROPERTY_ADDED_TO_UNOPEN_CONTENT_MODEL, PROPERTY_REMOVED_FROM_OPEN_CONTENT_MODEL and so on.

I would expect only ADDITIONAL_PROPERTIES_ADDED.

nizametdinovcrx commented 4 months ago

Any progress here?