Skip to content

Commit d4e9340

Browse files
authored
feat(kafka): add support for avro publishing (#1591)
1 parent d98bfab commit d4e9340

File tree

13 files changed

+26
-16
lines changed

13 files changed

+26
-16
lines changed

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/json/ExampleJsonValueGenerator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.swagger.v3.oas.models.media.Schema;
1616
import jakarta.validation.constraints.NotNull;
1717
import lombok.extern.slf4j.Slf4j;
18+
import org.springframework.http.MediaType;
1819

1920
import java.util.List;
2021
import java.util.Optional;
@@ -23,7 +24,8 @@
2324
@Slf4j
2425
public class ExampleJsonValueGenerator implements ExampleValueGenerator<JsonNode, JsonNode> {
2526

26-
private static final Set<String> SUPPORTED_CONTENT_TYPES = Set.of("application/json");
27+
private static final Set<String> SUPPORTED_CONTENT_TYPES =
28+
Set.of(MediaType.APPLICATION_JSON_VALUE, "application/avro");
2729

2830
private static final YAMLMapper yamlMapper = YAMLMapper.builder().build();
2931

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/xml/ExampleXmlValueGenerator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.swagger.v3.oas.models.media.Schema;
88
import io.swagger.v3.oas.models.media.StringSchema;
99
import lombok.extern.slf4j.Slf4j;
10+
import org.springframework.http.MediaType;
1011
import org.w3c.dom.DOMException;
1112
import org.w3c.dom.Document;
1213
import org.w3c.dom.Element;
@@ -32,7 +33,8 @@
3233
@Slf4j
3334
public class ExampleXmlValueGenerator implements ExampleValueGenerator<Node, String> {
3435

35-
private final Set<String> SUPPORTED_CONTENT_TYPES = Set.of("text/xml", "application/xml");
36+
private final Set<String> SUPPORTED_CONTENT_TYPES =
37+
Set.of(MediaType.TEXT_XML_VALUE, MediaType.APPLICATION_XML_VALUE);
3638
private final Schema<String> OVERRIDE_SCHEMA = new StringSchema();
3739

3840
private final ExampleXmlValueSerializer exampleXmlValueSerializer;

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/components/examples/walkers/yaml/ExampleYamlValueGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.swagger.v3.oas.models.media.StringSchema;
1212
import lombok.RequiredArgsConstructor;
1313
import lombok.extern.slf4j.Slf4j;
14+
import org.springframework.http.MediaType;
1415

1516
import java.util.List;
1617
import java.util.Optional;
@@ -20,7 +21,7 @@
2021
@RequiredArgsConstructor
2122
public class ExampleYamlValueGenerator implements ExampleValueGenerator<JsonNode, String> {
2223

23-
private final Set<String> SUPPORTED_CONTENT_TYPES = Set.of("application/yaml");
24+
private final Set<String> SUPPORTED_CONTENT_TYPES = Set.of(MediaType.APPLICATION_YAML_VALUE);
2425
private final Schema<String> OVERRIDE_SCHEMA = new StringSchema();
2526

2627
private final ExampleJsonValueGenerator exampleJsonValueGenerator;

springwolf-examples/e2e/tests/publishing.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ function testPublishingEveryChannelItem() {
6565
const payloadName = messageName;
6666

6767
if (
68-
messageTitle === "AnotherPayloadAvroDto" || // Avro publishing is not supported
6968
messageTitle === "XmlPayloadDto" || // Unable to create correct xml payload
7069
messageTitle === "YamlPayloadDto" || // Unable to create correct yaml payload
7170
messageTitle === "MonetaryAmount" || // Issue with either MonetaryAmount of ModelConverters

springwolf-examples/springwolf-kafka-example/docker-compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ services:
1212
- "8080:8080"
1313
depends_on:
1414
- kafka
15+
- kafka-schema-registry
1516

1617
kafka:
1718
image: confluentinc/cp-kafka:8.1.1
@@ -40,8 +41,6 @@ services:
4041
image: confluentinc/cp-schema-registry:latest
4142
ports:
4243
- "8081:8081"
43-
profiles:
44-
- test # avoid starting (and pulling) this container by default
4544
environment:
4645
SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: kafka:29092
4746
SCHEMA_REGISTRY_HOST_NAME: kafka-schema-registry

springwolf-examples/springwolf-kafka-example/src/main/avro/ExamplePayloadAvroDto.avsc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"default": "FOO1"
1818
},
1919
{
20-
"name": "ExamplePayloadAvroDto",
20+
"name": "examplePayloadAvroDto",
2121
"type": {
2222
"type": "record",
2323
"name": "ExamplePayloadAvroDto",

springwolf-examples/springwolf-kafka-example/src/main/java/io/github/springwolf/examples/kafka/consumers/AvroConsumer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import io.github.springwolf.bindings.kafka.annotations.KafkaAsyncOperationBinding;
55
import io.github.springwolf.core.asyncapi.annotations.AsyncListener;
6+
import io.github.springwolf.core.asyncapi.annotations.AsyncMessage;
67
import io.github.springwolf.core.asyncapi.annotations.AsyncOperation;
78
import io.github.springwolf.examples.kafka.dto.avro.AnotherPayloadAvroDto;
89
import lombok.RequiredArgsConstructor;
@@ -29,7 +30,8 @@ public class AvroConsumer {
2930
@AsyncOperation(
3031
channelName = "avro-topic",
3132
description =
32-
"Requires a running kafka-schema-registry. See docker-compose.yml to start it"))
33+
"Requires a running kafka-schema-registry. See docker-compose.yml to start it",
34+
message = @AsyncMessage(contentType = "application/avro")))
3335
@KafkaAsyncOperationBinding
3436
public void receiveExampleAvroPayload(AnotherPayloadAvroDto payloads) {
3537
log.debug("Received new message in avro-topic: {}", payloads.toString());

springwolf-examples/springwolf-kafka-example/src/test/java/io/github/springwolf/examples/kafka/KafkaProducerSystemTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.github.springwolf.examples.kafka.dtos.ExamplePayloadDto;
66
import lombok.extern.slf4j.Slf4j;
77
import org.awaitility.Awaitility;
8-
import org.junit.jupiter.api.Disabled;
98
import org.junit.jupiter.api.Test;
109
import org.junit.jupiter.api.condition.DisabledIf;
1110
import org.springframework.http.HttpEntity;
@@ -39,7 +38,7 @@ public class KafkaProducerSystemTest {
3938
private static final String KAFKA_NAME = "kafka";
4039
private static final String topic = "example-topic";
4140

42-
private static final boolean USE_SCHEMA_REGISTRY = false;
41+
private static final boolean USE_SCHEMA_REGISTRY = true;
4342

4443
@Container
4544
public static ComposeContainer environment = new ComposeContainer(new File("docker-compose.yml"))
@@ -98,7 +97,6 @@ void producerCanUseSpringwolfConfigurationToSendMessage() throws Exception {
9897
}
9998

10099
@Test
101-
@Disabled("Publishing AVRO is not supported")
102100
@DisabledIf(
103101
value = "withoutSchemaRegistry",
104102
disabledReason = "because it requires a running kafka-schema-registry instance (docker image= ~1GB).")
@@ -108,7 +106,7 @@ void producerCanUseSpringwolfConfigurationToSendAvroMessage() throws Exception {
108106
headers.put("Content-Type", List.of("application/json"));
109107

110108
String payloadAsString =
111-
"{\"someEnum\": \"FOO1\", \"ExamplePayloadAvroDto\": {\"someString\": \"string\", \"someLong\": 0}}";
109+
"{\"someEnum\": \"FOO1\", \"examplePayloadAvroDto\": {\"someString\": \"string\", \"someLong\": 0}}";
112110
String message = "{\n" //
113111
+ " \"bindings\": {},\n"
114112
+ " \"headers\": {},\n"

springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,7 @@
19611961
"$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dto.avro.AnotherPayloadAvroDto"
19621962
}
19631963
},
1964+
"contentType": "application/avro",
19641965
"name": "io.github.springwolf.examples.kafka.dto.avro.AnotherPayloadAvroDto",
19651966
"title": "AnotherPayloadAvroDto",
19661967
"bindings": {

springwolf-examples/springwolf-kafka-example/src/test/resources/asyncapi.openapiv31.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,7 @@
11801180
"$ref": "#/components/schemas/io.github.springwolf.examples.kafka.dto.avro.AnotherPayloadAvroDto"
11811181
}
11821182
},
1183+
"contentType": "application/avro",
11831184
"name": "io.github.springwolf.examples.kafka.dto.avro.AnotherPayloadAvroDto",
11841185
"title": "AnotherPayloadAvroDto",
11851186
"bindings": {

0 commit comments

Comments
 (0)