Skip to content

Commit 32b8ebc

Browse files
YangSan0622jsenko
authored andcommitted
Json backward compatability fix for type keyword
Motivation: #2835
1 parent 7c10506 commit 32b8ebc

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

schema-util/json/src/main/java/io/apicurio/registry/rules/compatibility/jsonschema/diff/ObjectSchemaDiffVisitor.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
package io.apicurio.registry.rules.compatibility.jsonschema.diff;
1818

1919
import io.apicurio.registry.rules.compatibility.jsonschema.JsonSchemaWrapperVisitor;
20+
import io.apicurio.registry.rules.compatibility.jsonschema.wrapper.CombinedSchemaWrapper;
2021
import io.apicurio.registry.rules.compatibility.jsonschema.wrapper.ObjectSchemaWrapper;
2122
import io.apicurio.registry.rules.compatibility.jsonschema.wrapper.SchemaWrapper;
23+
import org.everit.json.schema.CombinedSchema;
2224
import org.everit.json.schema.ObjectSchema;
2325
import org.everit.json.schema.Schema;
26+
import org.everit.json.schema.StringSchema;
2427

2528
import java.util.ArrayList;
2629
import java.util.HashSet;
@@ -300,8 +303,18 @@ public void visitPropertySchemas(Map<String, SchemaWrapper> propertySchemas) {
300303
@Override
301304
public void visitPropertySchema(String propertyName, SchemaWrapper schema) {
302305
if (original.getPropertySchemas().containsKey(propertyName)) {
306+
Schema originalPropertySchema = original.getPropertySchemas().get(propertyName);
307+
if (originalPropertySchema instanceof StringSchema
308+
&& schema instanceof CombinedSchemaWrapper) {
309+
originalPropertySchema = CombinedSchema
310+
.builder()
311+
.criterion(CombinedSchema.ANY_CRITERION)
312+
.subschema(originalPropertySchema)
313+
.build();
314+
}
315+
303316
schema.accept(new SchemaDiffVisitor(ctx.sub("properties/" + propertyName),
304-
original.getPropertySchemas().get(propertyName))); // TODO null/invalid schema
317+
originalPropertySchema)); // TODO null/invalid schema
305318
}
306319
super.visitPropertySchema(propertyName, schema);
307320
}

schema-util/json/src/test/resources/io/apicurio/registry/rules/compatibility/jsonschema/compatibility-test-data.json

+57
Original file line numberDiff line numberDiff line change
@@ -3385,6 +3385,63 @@
33853385
}
33863386
}
33873387
}
3388+
},
3389+
{
3390+
"enabled": true,
3391+
"id": "Properties: type keyword from string to array compatability 1",
3392+
"compatibility": "backward",
3393+
"original": {
3394+
"$schema": "http://json-schema.org/draft-07/schema#",
3395+
"title": "Person",
3396+
"type": "object",
3397+
"properties": {
3398+
"test": {
3399+
"type": "string"
3400+
}
3401+
}
3402+
},
3403+
"updated": {
3404+
"$schema": "http://json-schema.org/draft-07/schema#",
3405+
"title": "Person",
3406+
"type": "object",
3407+
"properties": {
3408+
"test": {
3409+
"anyOf": [
3410+
{
3411+
"type": "string"
3412+
},
3413+
{
3414+
"type": "number"
3415+
}
3416+
]
3417+
}
3418+
}
3419+
}
3420+
},
3421+
{
3422+
"enabled": true,
3423+
"id": "Properties: type keyword from string to array compatability 2",
3424+
"compatibility": "backward",
3425+
"original": {
3426+
"$schema": "http://json-schema.org/draft-07/schema#",
3427+
"title": "Person",
3428+
"type": "object",
3429+
"properties": {
3430+
"test": {
3431+
"type": "string"
3432+
}
3433+
}
3434+
},
3435+
"updated": {
3436+
"$schema": "http://json-schema.org/draft-07/schema#",
3437+
"title": "Person",
3438+
"type": "object",
3439+
"properties": {
3440+
"test": {
3441+
"type": ["string", "number"]
3442+
}
3443+
}
3444+
}
33883445
}
33893446
]
33903447
}

0 commit comments

Comments
 (0)