diff --git a/json-schema-provider/src/main/java/io/confluent/kafka/schemaregistry/json/JsonSchema.java b/json-schema-provider/src/main/java/io/confluent/kafka/schemaregistry/json/JsonSchema.java index 9facb66aa03..ff819ce1d7f 100644 --- a/json-schema-provider/src/main/java/io/confluent/kafka/schemaregistry/json/JsonSchema.java +++ b/json-schema-provider/src/main/java/io/confluent/kafka/schemaregistry/json/JsonSchema.java @@ -933,8 +933,13 @@ private void getInlineTaggedEntitiesRecursively(Map> t Map defns = (Map) unprocessedProperties.get("definitions"); if (defns != null) { for (Map.Entry entry : defns.entrySet()) { + Object rawSchema = null; if (entry.getValue() instanceof Map) { - Map rawSchema = replaceRefs((Map) entry.getValue()); + rawSchema = replaceRefs((Map) entry.getValue()); + } else if (entry.getValue() instanceof List) { + rawSchema = replaceRefs((List) entry.getValue()); + } + if (rawSchema != null) { JsonNode jsonNode = objectMapper.valueToTree(rawSchema); JsonSchema jsonSchema = new JsonSchema(jsonNode); getInlineTaggedEntitiesRecursively( @@ -962,6 +967,22 @@ private Map replaceRefs(Map defs) { for (Map.Entry entry : result.entrySet()) { if (entry.getValue() instanceof Map) { entry.setValue(replaceRefs((Map) entry.getValue())); + } else if (entry.getValue() instanceof List) { + entry.setValue(replaceRefs((List) entry.getValue())); + } + } + return result; + } + + private List replaceRefs(List items) { + List result = new ArrayList<>(); + for (Object item : items) { + if (item instanceof Map) { + result.add(replaceRefs((Map) item)); + } else if (item instanceof List) { + result.add(replaceRefs((List) item)); + } else { + result.add(item); } } return result; diff --git a/json-schema-provider/src/test/java/io/confluent/kafka/schemaregistry/json/JsonSchemaTest.java b/json-schema-provider/src/test/java/io/confluent/kafka/schemaregistry/json/JsonSchemaTest.java index 0f07ca47b8e..96719b5b210 100644 --- a/json-schema-provider/src/test/java/io/confluent/kafka/schemaregistry/json/JsonSchemaTest.java +++ b/json-schema-provider/src/test/java/io/confluent/kafka/schemaregistry/json/JsonSchemaTest.java @@ -653,6 +653,88 @@ public void testBasicAddAndRemoveTags() { assertEquals(ImmutableSet.of("EXTERNAL"), resultSchema.tags()); } + @Test + public void testInlineTagsForRefInArray() { + String schemaString = "{\n" + + " \"definitions\": {\n" + + " \"MetricData\": {\n" + + " \"additionalProperties\": false,\n" + + " \"properties\": {\n" + + " \"name\": {\n" + + " \"oneOf\": [\n" + + " {\n" + + " \"title\": \"Not included\",\n" + + " \"type\": \"null\"\n" + + " },\n" + + " {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " ],\n" + + " \"confluent:tags\": [ \"PII\" ]\n" + + " },\n" + + " \"value\": {\n" + + " \"oneOf\": [\n" + + " {\n" + + " \"title\": \"Not included\",\n" + + " \"type\": \"null\"\n" + + " },\n" + + " {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " },\n" + + " \"type\": \"object\"\n" + + " },\n" + + " \"SensorData\": {\n" + + " \"additionalProperties\": false,\n" + + " \"properties\": {\n" + + " \"metricData\": {\n" + + " \"oneOf\": [\n" + + " {\n" + + " \"title\": \"Not included\",\n" + + " \"type\": \"null\"\n" + + " },\n" + + " {\n" + + " \"items\": {\n" + + " \"$ref\": \"#/definitions/MetricData\"\n" + + " },\n" + + " \"type\": \"array\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " },\n" + + " \"type\": \"object\"\n" + + " }\n" + + " },\n" + + " \"properties\": {\n" + + " \"sensorData\": {\n" + + " \"oneOf\": [\n" + + " {\n" + + " \"title\": \"Not included\",\n" + + " \"type\": \"null\"\n" + + " },\n" + + " {\n" + + " \"items\": {\n" + + " \"$ref\": \"#/definitions/SensorData\"\n" + + " },\n" + + " \"type\": \"array\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " },\n" + + " \"type\": \"object\"\n" + + "}\n"; + JsonSchema schema = new JsonSchema(schemaString); + + Map> tags = new HashMap<>(); + tags.put(new SchemaEntity("object.definitions.object.name", + SchemaEntity.EntityType.SR_FIELD), + Collections.singleton("PII")); + Map> expectedTags = new HashMap<>(tags); + assertEquals(expectedTags, schema.inlineTaggedEntities()); + } + @Test public void testAddTagsToConditional() { String schemaString = "{\n" +