Skip to content

Commit

Permalink
feat(core): remove avro schemas in ref schemas as well
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed Feb 9, 2024
1 parent 5f6f1cb commit b3b2f53
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,10 +23,17 @@ public class AvroSchemaPostProcessor implements SchemasPostProcessor {
@Override
public void process(Schema schema, Map<String, Schema> definitions) {
removeAvroSchemas(definitions);
removeAvroProperties(schema);
removeAvroProperties(schema, definitions);
}

private void removeAvroProperties(Schema schema) {
private void removeAvroProperties(Schema schema, Map<String, Schema> definitions) {
if (schema.get$ref() != null) {
String schemaName = MessageReference.extractRefName(schema.get$ref());
if (definitions.containsKey(schemaName)) {
removeAvroProperties(definitions.get(schemaName), definitions);
}
}

Map<String, Schema> properties = schema.getProperties();
if (properties != null) {
Schema schemaPropertySchema = properties.getOrDefault(SCHEMA_PROPERTY, null);
Expand All @@ -37,6 +45,8 @@ private void removeAvroProperties(Schema schema) {
properties.remove(SPECIFIC_DATA_PROPERTY);
}
}

properties.forEach((key, value) -> removeAvroProperties(value, definitions));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void avroSchemasAreRemovedTest() {
Map.of("foo", new StringSchema(), "schema", avroSchema, "specificData", avroSpecificData)));

var definitions = new HashMap<String, io.swagger.v3.oas.models.media.Schema>();
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());
Expand All @@ -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<String, io.swagger.v3.oas.models.media.Schema>();
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()));
}
}

0 comments on commit b3b2f53

Please sign in to comment.