From b3b2f531a38e572d12b8397ecfc6f47774283be9 Mon Sep 17 00:00:00 2001 From: Timon Back Date: Fri, 9 Feb 2024 17:56:06 +0100 Subject: [PATCH] feat(core): remove avro schemas in ref schemas as well --- .../channel/message/MessageReference.java | 4 ++ .../AvroSchemaPostProcessor.java | 14 +++++- .../AvroSchemaPostProcessorTest.java | 45 ++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/springwolf-asyncapi/src/main/java/io/github/stavshamir/springwolf/asyncapi/v3/model/channel/message/MessageReference.java b/springwolf-asyncapi/src/main/java/io/github/stavshamir/springwolf/asyncapi/v3/model/channel/message/MessageReference.java index e1148773d..a182b071b 100644 --- a/springwolf-asyncapi/src/main/java/io/github/stavshamir/springwolf/asyncapi/v3/model/channel/message/MessageReference.java +++ b/springwolf-asyncapi/src/main/java/io/github/stavshamir/springwolf/asyncapi/v3/model/channel/message/MessageReference.java @@ -48,4 +48,8 @@ public static MessageReference toChannelMessage(String channelName, String messa public static MessageReference toSchema(String schemaName) { return new MessageReference("#/components/schemas/" + schemaName); } + + public static String extractRefName(String ref) { + return ref.substring(ref.lastIndexOf('/') + 1); + } } diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/AvroSchemaPostProcessor.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/AvroSchemaPostProcessor.java index 1de83a3e4..00dff2abe 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/AvroSchemaPostProcessor.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/AvroSchemaPostProcessor.java @@ -1,6 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 package io.github.stavshamir.springwolf.schemas.postprocessor; +import io.github.stavshamir.springwolf.asyncapi.v3.model.channel.message.MessageReference; import io.swagger.v3.oas.models.media.Schema; import org.springframework.util.StringUtils; @@ -22,10 +23,17 @@ public class AvroSchemaPostProcessor implements SchemasPostProcessor { @Override public void process(Schema schema, Map definitions) { removeAvroSchemas(definitions); - removeAvroProperties(schema); + removeAvroProperties(schema, definitions); } - private void removeAvroProperties(Schema schema) { + private void removeAvroProperties(Schema schema, Map definitions) { + if (schema.get$ref() != null) { + String schemaName = MessageReference.extractRefName(schema.get$ref()); + if (definitions.containsKey(schemaName)) { + removeAvroProperties(definitions.get(schemaName), definitions); + } + } + Map properties = schema.getProperties(); if (properties != null) { Schema schemaPropertySchema = properties.getOrDefault(SCHEMA_PROPERTY, null); @@ -37,6 +45,8 @@ private void removeAvroProperties(Schema schema) { properties.remove(SPECIFIC_DATA_PROPERTY); } } + + properties.forEach((key, value) -> removeAvroProperties(value, definitions)); } } diff --git a/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/postprocessor/AvroSchemaPostProcessorTest.java b/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/postprocessor/AvroSchemaPostProcessorTest.java index e0578a996..0bbc2e18e 100644 --- a/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/postprocessor/AvroSchemaPostProcessorTest.java +++ b/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/postprocessor/AvroSchemaPostProcessorTest.java @@ -26,6 +26,7 @@ void avroSchemasAreRemovedTest() { Map.of("foo", new StringSchema(), "schema", avroSchema, "specificData", avroSpecificData))); var definitions = new HashMap(); + definitions.put("schema", schema); definitions.put("customClassRefUnusedInThisTest", new StringSchema()); definitions.put("org.apache.avro.Schema", new io.swagger.v3.oas.models.media.Schema()); definitions.put("org.apache.avro.ConversionJava.lang.Object", new io.swagger.v3.oas.models.media.Schema()); @@ -35,6 +36,48 @@ void avroSchemasAreRemovedTest() { // then assertThat(schema.getProperties()).isEqualTo(Map.of("foo", new StringSchema())); - assertThat(definitions).isEqualTo(Map.of("customClassRefUnusedInThisTest", new StringSchema())); + assertThat(definitions) + .isEqualTo(Map.of("schema", schema, "customClassRefUnusedInThisTest", new StringSchema())); + } + + @Test + void avroSchemasAreRemovedInRefsTest() { + // given + var avroSchema = new io.swagger.v3.oas.models.media.Schema(); + avroSchema.set$ref("#/components/schemas/org.apache.avro.Schema"); + + var avroSpecificData = new io.swagger.v3.oas.models.media.Schema(); + avroSpecificData.set$ref("#/components/schemas/org.apache.avro.specific.SpecificData"); + + var refSchema = new io.swagger.v3.oas.models.media.Schema(); + refSchema.setProperties(new HashMap<>( + Map.of("foo", new StringSchema(), "schema", avroSchema, "specificData", avroSpecificData))); + + var refProperty = new io.swagger.v3.oas.models.media.Schema(); + refProperty.set$ref("#/components/schemas/refSchema"); + var schema = new io.swagger.v3.oas.models.media.Schema(); + schema.setProperties(new HashMap<>(Map.of("ref", refProperty))); + + var definitions = new HashMap(); + definitions.put("schema", schema); + definitions.put("refSchema", refSchema); + definitions.put("customClassRefUnusedInThisTest", new StringSchema()); + definitions.put("org.apache.avro.Schema", new io.swagger.v3.oas.models.media.Schema()); + definitions.put("org.apache.avro.ConversionJava.lang.Object", new io.swagger.v3.oas.models.media.Schema()); + + // when + processor.process(schema, definitions); + + // then + assertThat(schema.getProperties()).isEqualTo(Map.of("ref", refProperty)); + assertThat(refSchema.getProperties()).isEqualTo(Map.of("foo", new StringSchema())); + assertThat(definitions) + .isEqualTo(Map.of( + "schema", + schema, + "refSchema", + refSchema, + "customClassRefUnusedInThisTest", + new StringSchema())); } }