Polymorphic payload in json schema produce invalid diff
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());
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.
@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.
Any progress here?