-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Remove avro fields from schema (#503)
* feat: Remove avro fields from schema * refactor(core): extract SchemasPostProcessor * chore(kafka): better avro topic name * chore(kafka): use kafka kraft in example No need for zookeeper anymore * chore(cloud-stream): use kafka kraft in example No need for zookeeper anymore * chore(kafka): add akhq as kafka example development container * chore(kafka): remove zookeeper_jaas.conf as well * feat(kafka): add avro publication support to example * chore(core): reduce ExampleJsonGenerator log level * chore(kafka): fix asyncapi.json * chore(kafka): rename example kafka container internal listener * test(cloud-stream): add sasl to test setup * chore(kafka): extract avro listener to own class * fix(kafka): kafka example can publish again * docs(kafka): kafka-schema-registry is not started by default
- Loading branch information
Showing
25 changed files
with
507 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,8 @@ allprojects { | |
} | ||
|
||
mavenCentral() | ||
|
||
maven { url "https://packages.confluent.io/maven/" } | ||
} | ||
|
||
spotless { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...n/java/io/github/stavshamir/springwolf/schemas/postprocessor/AvroSchemaPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.schemas.postprocessor; | ||
|
||
import io.swagger.v3.oas.models.media.Schema; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Removes internal avro fields and classes from the schema. | ||
* <br/> | ||
* For now, this class is located in springwolf-core as it provides value for the cloud-stream and kafka plugin. | ||
* To avoid to many (one-class) add-ons, this class was not moved to yet another artifact - as also no new dependencies are required. | ||
* This may change in the future. | ||
*/ | ||
public class AvroSchemaPostProcessor implements SchemasPostProcessor { | ||
private static final String SCHEMA_PROPERTY = "schema"; | ||
private static final String SPECIFIC_DATA_PROPERTY = "specificData"; | ||
private static final String SCHEMA_REF = "org.apache.avro.Schema"; | ||
private static final String SPECIFIC_DAT_REF = "org.apache.avro.specific.SpecificData"; | ||
|
||
@Override | ||
public void process(Schema schema, Map<String, Schema> definitions) { | ||
removeAvroSchemas(definitions); | ||
removeAvroProperties(schema); | ||
} | ||
|
||
private void removeAvroProperties(Schema schema) { | ||
Map<String, Schema> properties = schema.getProperties(); | ||
if (properties != null) { | ||
Schema schemaPropertySchema = properties.getOrDefault(SCHEMA_PROPERTY, null); | ||
Schema specificDataPropertySchema = properties.getOrDefault(SPECIFIC_DATA_PROPERTY, null); | ||
if (schemaPropertySchema != null && specificDataPropertySchema != null) { | ||
if (StringUtils.endsWithIgnoreCase(schemaPropertySchema.get$ref(), SCHEMA_REF) | ||
&& StringUtils.endsWithIgnoreCase(specificDataPropertySchema.get$ref(), SPECIFIC_DAT_REF)) { | ||
properties.remove(SCHEMA_PROPERTY); | ||
properties.remove(SPECIFIC_DATA_PROPERTY); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void removeAvroSchemas(Map<String, Schema> definitions) { | ||
definitions.entrySet().removeIf(entry -> StringUtils.startsWithIgnoreCase(entry.getKey(), "org.apache.avro")); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../io/github/stavshamir/springwolf/schemas/postprocessor/ExampleGeneratorPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.schemas.postprocessor; | ||
|
||
import io.github.stavshamir.springwolf.schemas.example.ExampleGenerator; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Map; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class ExampleGeneratorPostProcessor implements SchemasPostProcessor { | ||
private final ExampleGenerator exampleGenerator; | ||
|
||
@Override | ||
public void process(Schema schema, Map<String, Schema> definitions) { | ||
if (schema.getExample() == null) { | ||
log.debug("Generate example for {}", schema.getName()); | ||
|
||
Object example = exampleGenerator.fromSchema(schema, definitions); | ||
schema.setExample(example); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...main/java/io/github/stavshamir/springwolf/schemas/postprocessor/SchemasPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.schemas.postprocessor; | ||
|
||
import io.swagger.v3.oas.models.media.Schema; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Internal interface to allow post-processing of a new schema (and their definition) after detection. | ||
* <br/> | ||
* It is closely coupled with the data structure of the SchemaService. | ||
*/ | ||
public interface SchemasPostProcessor { | ||
void process(Schema schema, Map<String, Schema> definitions); | ||
} |
22 changes: 22 additions & 0 deletions
22
...ava/io/github/stavshamir/springwolf/schemas/postprocessor/SwaggerSchemaPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.schemas.postprocessor; | ||
|
||
import io.swagger.v3.oas.models.media.Schema; | ||
|
||
import java.util.Map; | ||
|
||
public class SwaggerSchemaPostProcessor implements SchemasPostProcessor { | ||
@Override | ||
public void process(Schema schema, Map<String, Schema> definitions) { | ||
removeAdditionalProperties(schema); | ||
} | ||
|
||
private void removeAdditionalProperties(Schema schema) { | ||
schema.setAdditionalProperties(null); | ||
|
||
Map<String, Schema> properties = schema.getProperties(); | ||
if (properties != null) { | ||
properties.values().forEach((property) -> removeAdditionalProperties(property)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...va/io/github/stavshamir/springwolf/schemas/postprocessor/AvroSchemaPostProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.schemas.postprocessor; | ||
|
||
import io.swagger.v3.oas.models.media.StringSchema; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class AvroSchemaPostProcessorTest { | ||
SchemasPostProcessor processor = new AvroSchemaPostProcessor(); | ||
|
||
@Test | ||
void avroSchemasAreRemovedTest() { | ||
// 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 schema = new io.swagger.v3.oas.models.media.Schema(); | ||
schema.setProperties(new HashMap<>( | ||
Map.of("foo", new StringSchema(), "schema", avroSchema, "specificData", avroSpecificData))); | ||
|
||
var definitions = new HashMap<String, io.swagger.v3.oas.models.media.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()); | ||
|
||
// when | ||
processor.process(schema, definitions); | ||
|
||
// then | ||
assertThat(schema.getProperties()).isEqualTo(Map.of("foo", new StringSchema())); | ||
assertThat(definitions).isEqualTo(Map.of("customClassRefUnusedInThisTest", new StringSchema())); | ||
} | ||
} |
Oops, something went wrong.