Skip to content

Commit

Permalink
feat(kafka): demonstrate cloud event headers in example (#426)
Browse files Browse the repository at this point in the history
* feat(kafka): demonstrate cloud event headers in example

* feat(kafka): rename schema to SpringDefaultHeaderAndCloudEvent
  • Loading branch information
timonback authored Nov 3, 2023
1 parent 6aa38ba commit c8d333d
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
String name();

/**
* Mapped to {@link AsyncHeaderSchema#getDescription()} ()}
* Mapped to {@link AsyncHeaderSchema#getDescription()}
*/
String description() default "";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.stavshamir.springwolf.asyncapi.types.channel.operation.message.header;

public class AsyncHeadersCloudEventConstants {
public static final String CONTENT_TYPE = "content-type";
public static final String CONTENT_TYPE_DESC = "CloudEvent Content-Type Header";
public static final String ID = "ce_id";
public static final String ID_DESC = "CloudEvent Id Header";
public static final String SPECVERSION = "ce_specversion";
public static final String SPECVERSION_DESC = "CloudEvent Spec Version Header";
public static final String SOURCE = "ce_source";
public static final String SOURCE_DESC = "CloudEvent Source Header";
public static final String SUBJECT = "ce_subject";
public static final String SUBJECT_DESC = "CloudEvent Subject Header";
public static final String TIME = "ce_time";
public static final String TIME_DESC = "CloudEvent Time Header";
public static final String TYPE = "ce_type";
public static final String TYPE_DESC = "CloudEvent Payload Type Header";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

import java.util.List;

/**
* Only used in combination with AsyncApiDocket bean
* If not, let us know by raising a GitHub issue
*/
@Deprecated
public class AsyncHeadersForCloudEventsBuilder {

private final AsyncHeaders headers;
Expand All @@ -31,58 +36,79 @@ public AsyncHeadersForCloudEventsBuilder withContentTypeHeader(
List<String> contentTypeStringValues =
contentTypeValues.stream().map(MimeType::toString).toList();
return withHeader(
"content-type",
AsyncHeadersCloudEventConstants.CONTENT_TYPE,
contentTypeStringValues,
exampleContentType.toString(),
"CloudEvent Content-Type Header");
AsyncHeadersCloudEventConstants.CONTENT_TYPE_DESC);
}

public AsyncHeadersForCloudEventsBuilder withSpecVersionHeader(String specVersion) {
return withSpecVersionHeader(specVersion, List.of(specVersion));
}

public AsyncHeadersForCloudEventsBuilder withSpecVersionHeader(String specVersion, List<String> specValues) {
return withHeader("ce_specversion", specValues, specVersion, "CloudEvent Spec Version Header");
return withHeader(
AsyncHeadersCloudEventConstants.SPECVERSION,
specValues,
specVersion,
AsyncHeadersCloudEventConstants.SPECVERSION_DESC);
}

public AsyncHeadersForCloudEventsBuilder withIdHeader(String idExample) {
return withIdHeader(idExample, List.of(idExample));
}

public AsyncHeadersForCloudEventsBuilder withIdHeader(String idExample, List<String> idValues) {
return withHeader("ce_id", idValues, idExample, "CloudEvent Id Header");
return withHeader(
AsyncHeadersCloudEventConstants.ID, idValues, idExample, AsyncHeadersCloudEventConstants.ID_DESC);
}

public AsyncHeadersForCloudEventsBuilder withTimeHeader(String timeExample) {
return withTimeHeader(timeExample, List.of(timeExample));
}

public AsyncHeadersForCloudEventsBuilder withTimeHeader(String timeExample, List<String> timeValues) {
return withHeader("ce_time", timeValues, timeExample, "CloudEvent Time Header");
return withHeader(
AsyncHeadersCloudEventConstants.TIME,
timeValues,
timeExample,
AsyncHeadersCloudEventConstants.TIME_DESC);
}

public AsyncHeadersForCloudEventsBuilder withTypeHeader(String typeExample) {
return withTypeHeader(typeExample, List.of(typeExample));
}

public AsyncHeadersForCloudEventsBuilder withTypeHeader(String typeExample, List<String> typeValues) {
return withHeader("ce_type", typeValues, typeExample, "CloudEvent Payload Type Header");
return withHeader(
AsyncHeadersCloudEventConstants.TYPE,
typeValues,
typeExample,
AsyncHeadersCloudEventConstants.TYPE_DESC);
}

public AsyncHeadersForCloudEventsBuilder withSourceHeader(String sourceExample) {
return withSourceHeader(sourceExample, List.of(sourceExample));
}

public AsyncHeadersForCloudEventsBuilder withSourceHeader(String sourceExample, List<String> sourceValues) {
return withHeader("ce_source", sourceValues, sourceExample, "CloudEvent Source Header");
return withHeader(
AsyncHeadersCloudEventConstants.SOURCE,
sourceValues,
sourceExample,
AsyncHeadersCloudEventConstants.SOURCE_DESC);
}

public AsyncHeadersForCloudEventsBuilder withSubjectHeader(String subjectExample) {
return withSubjectHeader(subjectExample, List.of(subjectExample));
}

public AsyncHeadersForCloudEventsBuilder withSubjectHeader(String subjectExample, List<String> subjectValues) {
return withHeader("ce_subject", subjectValues, subjectExample, "CloudEvent Subject Header");
return withHeader(
AsyncHeadersCloudEventConstants.SUBJECT,
subjectValues,
subjectExample,
AsyncHeadersCloudEventConstants.SUBJECT_DESC);
}

public AsyncHeadersForCloudEventsBuilder withExtension(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.KafkaAsyncOperationBinding;
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.KafkaAsyncOperationBinding.KafkaAsyncKey;
import io.github.stavshamir.springwolf.asyncapi.scanners.channels.operationdata.annotation.KafkaAsyncOperationBinding.KafkaAsyncMessageBinding;
import io.github.stavshamir.springwolf.asyncapi.types.channel.operation.message.header.AsyncHeadersCloudEventConstants;
import io.github.stavshamir.springwolf.example.kafka.configuration.KafkaConfiguration;
import io.github.stavshamir.springwolf.example.kafka.dtos.NestedPayloadDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

Expand All @@ -27,13 +29,41 @@ public class NestedProducer {
description = "Custom, optional description defined in the AsyncPublisher annotation",
headers =
@AsyncOperation.Headers(
schemaName = "SpringKafkaDefaultHeaders",
schemaName = "SpringDefaultHeaderAndCloudEvent",
values = {
@AsyncOperation.Headers.Header(
name = DEFAULT_CLASSID_FIELD_NAME,
description = "Spring Type Id Header",
value =
"io.github.stavshamir.springwolf.example.kafka.dtos.NestedPayloadDto"),
@AsyncOperation.Headers.Header(
name = AsyncHeadersCloudEventConstants.CONTENT_TYPE,
description = AsyncHeadersCloudEventConstants.CONTENT_TYPE_DESC,
value = MediaType.APPLICATION_JSON_VALUE),
@AsyncOperation.Headers.Header(
name = AsyncHeadersCloudEventConstants.ID,
description = AsyncHeadersCloudEventConstants.ID_DESC,
value = "2c60089e-6f39-459d-8ced-2d6df7e4c03a"),
@AsyncOperation.Headers.Header(
name = AsyncHeadersCloudEventConstants.SPECVERSION,
description = AsyncHeadersCloudEventConstants.SPECVERSION_DESC,
value = "1.0"),
@AsyncOperation.Headers.Header(
name = AsyncHeadersCloudEventConstants.SOURCE,
description = AsyncHeadersCloudEventConstants.SOURCE_DESC,
value = "http://localhost"),
@AsyncOperation.Headers.Header(
name = AsyncHeadersCloudEventConstants.SUBJECT,
description = AsyncHeadersCloudEventConstants.SUBJECT_DESC,
value = "${spring.application.name}"),
@AsyncOperation.Headers.Header(
name = AsyncHeadersCloudEventConstants.TIME,
description = AsyncHeadersCloudEventConstants.TIME_DESC,
value = "2023-10-28 20:01:23+00:00"),
@AsyncOperation.Headers.Header(
name = AsyncHeadersCloudEventConstants.TYPE,
description = AsyncHeadersCloudEventConstants.TYPE_DESC,
value = "NestedPayloadDto.v1"),
})))
@KafkaAsyncOperationBinding(
clientId = "foo-clientId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
"$ref": "#/components/schemas/io.github.stavshamir.springwolf.example.kafka.dtos.NestedPayloadDto"
},
"headers": {
"$ref": "#/components/schemas/SpringKafkaDefaultHeaders"
"$ref": "#/components/schemas/SpringDefaultHeaderAndCloudEvent"
},
"bindings": {
"kafka": {
Expand Down Expand Up @@ -354,7 +354,7 @@
"properties": { },
"example": { }
},
"SpringKafkaDefaultHeaders": {
"SpringDefaultHeaderAndCloudEvent": {
"type": "object",
"properties": {
"__TypeId__": {
Expand All @@ -364,10 +364,73 @@
"enum": [
"io.github.stavshamir.springwolf.example.kafka.dtos.NestedPayloadDto"
]
},
"ce_id": {
"type": "string",
"description": "CloudEvent Id Header",
"example": "2c60089e-6f39-459d-8ced-2d6df7e4c03a",
"enum": [
"2c60089e-6f39-459d-8ced-2d6df7e4c03a"
]
},
"ce_source": {
"type": "string",
"description": "CloudEvent Source Header",
"example": "http://localhost",
"enum": [
"http://localhost"
]
},
"ce_specversion": {
"type": "string",
"description": "CloudEvent Spec Version Header",
"example": "1.0",
"enum": [
"1.0"
]
},
"ce_subject": {
"type": "string",
"description": "CloudEvent Subject Header",
"example": "Springwolf example project - Kafka",
"enum": [
"Springwolf example project - Kafka"
]
},
"ce_time": {
"type": "string",
"description": "CloudEvent Time Header",
"example": "2023-10-28 20:01:23+00:00",
"enum": [
"2023-10-28 20:01:23+00:00"
]
},
"ce_type": {
"type": "string",
"description": "CloudEvent Payload Type Header",
"example": "NestedPayloadDto.v1",
"enum": [
"NestedPayloadDto.v1"
]
},
"content-type": {
"type": "string",
"description": "CloudEvent Content-Type Header",
"example": "application/json",
"enum": [
"application/json"
]
}
},
"example": {
"__TypeId__": "io.github.stavshamir.springwolf.example.kafka.dtos.NestedPayloadDto"
"__TypeId__": "io.github.stavshamir.springwolf.example.kafka.dtos.NestedPayloadDto",
"ce_id": "2c60089e-6f39-459d-8ced-2d6df7e4c03a",
"ce_source": "http://localhost",
"ce_specversion": "1.0",
"ce_subject": "Springwolf example project - Kafka",
"ce_time": "2023-10-28 20:01:23+00:00",
"ce_type": "NestedPayloadDto.v1",
"content-type": "application/json"
}
},
"SpringKafkaDefaultHeaders-AnotherPayloadDto": {
Expand Down

0 comments on commit c8d333d

Please sign in to comment.