Skip to content

Commit

Permalink
DGS-9520 Better handling of JSON Schema singleton combined types
Browse files Browse the repository at this point in the history
  • Loading branch information
rayokota committed Dec 20, 2023
1 parent 4eb2331 commit 414ce79
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 5 deletions.
Expand Up @@ -40,7 +40,7 @@ static void compare(
final CombinedSchema original,
final CombinedSchema update
) {
Difference.Type type = compareCriteria(ctx, original.getCriterion(), update.getCriterion());
Difference.Type type = compareCriteria(ctx, original, update);
if (type != COMBINED_TYPE_CHANGED) {
// Use sets to collapse duplicate entries
final Set<JsonSchema> originalSubschemas = original.getSubschemas().stream()
Expand Down Expand Up @@ -96,14 +96,31 @@ static void compare(

private static Difference.Type compareCriteria(
final Context ctx,
final ValidationCriterion original,
final ValidationCriterion update
final CombinedSchema original,
final CombinedSchema update
) {
ValidationCriterion originalCriterion = original.getCriterion();
ValidationCriterion updateCriterion = update.getCriterion();
Difference.Type type;
if (original.equals(update)) {
if (originalCriterion.equals(updateCriterion)) {
type = null;
} else if (update == CombinedSchema.ANY_CRITERION) {
} else if (updateCriterion == CombinedSchema.ANY_CRITERION) {
// Allow allOf/oneOf to anyOf
type = COMBINED_TYPE_EXTENDED;
} else if (updateCriterion == CombinedSchema.ONE_CRITERION) {
if (isSingleton(original)) {
// Allow singleton anyOf/allOf to oneOf
type = COMBINED_TYPE_EXTENDED;
} else {
type = COMBINED_TYPE_CHANGED;
}
} else if (updateCriterion == CombinedSchema.ALL_CRITERION) {
if (isSingleton(original) && isSingleton(update)) {
// Allow singleton oneOf/anyOf to singleton allOf
type = COMBINED_TYPE_EXTENDED;
} else {
type = COMBINED_TYPE_CHANGED;
}
} else {
type = COMBINED_TYPE_CHANGED;
}
Expand All @@ -112,4 +129,8 @@ private static Difference.Type compareCriteria(
}
return type;
}

private static boolean isSingleton(CombinedSchema schema) {
return schema.getSubschemas().size() == 1;
}
}
Expand Up @@ -521,6 +521,240 @@
],
"compatible": true
},
{
"description": "Detect compatible change from anyOf to oneOf schema with more properties",
"original_schema": {
"type": "object",
"properties": {
"prop1": {
"anyOf": [
{
"type": "string"
}
]
}
}
},
"update_schema": {
"type": "object",
"properties": {
"prop1": {
"oneOf": [
{
"type": "number"
},
{
"type": "string"
}
]
}
}
},
"changes": [
"COMBINED_TYPE_EXTENDED #/properties/prop1",
"SUM_TYPE_EXTENDED #/properties/prop1"
],
"compatible": true
},
{
"description": "Detect incompatible change from anyOf with more properties to oneOf schema",
"original_schema": {
"type": "object",
"properties": {
"prop1": {
"anyOf": [
{
"type": "number"
},
{
"type": "string"
}
]
}
}
},
"update_schema": {
"type": "object",
"properties": {
"prop1": {
"oneOf": [
{
"type": "string"
}
]
}
}
},
"changes": [
"COMBINED_TYPE_CHANGED #/properties/prop1"
],
"compatible": false
},
{
"description": "Detect compatible change from allOf to oneOf schema with more properties",
"original_schema": {
"type": "object",
"properties": {
"prop1": {
"allOf": [
{
"type": "number"
}
]
}
}
},
"update_schema": {
"type": "object",
"properties": {
"prop1": {
"oneOf": [
{
"type": "number"
},
{
"type": "string"
}
]
}
}
},
"changes": [
"COMBINED_TYPE_EXTENDED #/properties/prop1",
"SUM_TYPE_EXTENDED #/properties/prop1"
],
"compatible": true
},
{
"description": "Detect compatible change from anyOf to allOf schema",
"original_schema": {
"type": "object",
"properties": {
"prop1": {
"anyOf": [
{
"type": "string"
}
]
}
}
},
"update_schema": {
"type": "object",
"properties": {
"prop1": {
"allOf": [
{
"type": "string"
}
]
}
}
},
"changes": [
"COMBINED_TYPE_EXTENDED #/properties/prop1"
],
"compatible": true
},
{
"description": "Detect compatible change from oneOf to allOf schema",
"original_schema": {
"type": "object",
"properties": {
"prop1": {
"oneOf": [
{
"type": "string"
}
]
}
}
},
"update_schema": {
"type": "object",
"properties": {
"prop1": {
"allOf": [
{
"type": "string"
}
]
}
}
},
"changes": [
"COMBINED_TYPE_EXTENDED #/properties/prop1"
],
"compatible": true
},
{
"description": "Detect incompatible change from anyOf to allOf schema",
"original_schema": {
"type": "object",
"properties": {
"prop1": {
"anyOf": [
{
"type": "string"
}
]
}
}
},
"update_schema": {
"type": "object",
"properties": {
"prop1": {
"allOf": [
{
"type": "number"
},
{
"type": "string"
}
]
}
}
},
"changes": [
"COMBINED_TYPE_CHANGED #/properties/prop1"
],
"compatible": false
},
{
"description": "Detect incompatible change from oneOf to allOf schema",
"original_schema": {
"type": "object",
"properties": {
"prop1": {
"oneOf": [
{
"type": "string"
}
]
}
}
},
"update_schema": {
"type": "object",
"properties": {
"prop1": {
"allOf": [
{
"type": "number"
},
{
"type": "string"
}
]
}
}
},
"changes": [
"COMBINED_TYPE_CHANGED #/properties/prop1"
],
"compatible": false
},
{
"description": "Detect compatible change from non-combined to anyOf schema",
"original_schema": {
Expand Down
24 changes: 24 additions & 0 deletions json-schema-provider/src/test/resources/diff-schema-examples.json
Expand Up @@ -887,12 +887,36 @@
],
"compatible": false
},
{
"description": "Detect changes to validation criteria in composed schema type for singleton",
"original_schema": {
"oneOf": [
{
"type": "string"
}
]
},
"update_schema": {
"allOf": [
{
"type": "string"
}
]
},
"changes": [
"COMBINED_TYPE_EXTENDED #/"
],
"compatible": true
},
{
"description": "Detect changes to validation criteria in composed schema type",
"original_schema": {
"oneOf": [
{
"type": "string"
},
{
"type": "integer"
}
]
},
Expand Down

0 comments on commit 414ce79

Please sign in to comment.