From d34b27fdac37c24a78e636571b4ad5e22fbf82e0 Mon Sep 17 00:00:00 2001 From: Isaac Aymerich Date: Wed, 24 Aug 2022 09:37:00 +0200 Subject: [PATCH 1/4] support dynamic data content type Signed-off-by: Isaac Aymerich --- .../io/cloudevents/jackson/JsonFormat.java | 8 +++++-- .../jackson/JsonCloudEventDataTest.java | 21 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java b/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java index 49467980b..b6d35b9f7 100644 --- a/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java +++ b/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java @@ -29,6 +29,7 @@ import io.cloudevents.rw.CloudEventRWException; import java.io.IOException; +import java.util.regex.Pattern; /** * Implementation of {@link EventFormat} for JSON event format @@ -43,7 +44,10 @@ public final class JsonFormat implements EventFormat { * Content type associated with the JSON event format */ public static final String CONTENT_TYPE = "application/cloudevents+json"; - + /** + * Suppoted Content type + */ + private static final Pattern PATTERN_CONTENT_TYPE = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json$"); private final ObjectMapper mapper; private final JsonFormatOptions options; @@ -214,6 +218,6 @@ public static SimpleModule getCloudEventJacksonModule(JsonFormatOptions options) static boolean dataIsJsonContentType(String contentType) { // If content type, spec states that we should assume is json - return contentType == null || contentType.startsWith("application/json") || contentType.startsWith("text/json"); + return contentType == null || PATTERN_CONTENT_TYPE.matcher(contentType).matches(); } } diff --git a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java index 9a4537e12..8b71d3a77 100644 --- a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java +++ b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java @@ -20,19 +20,27 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory; import io.cloudevents.CloudEvent; import io.cloudevents.core.builder.CloudEventBuilder; +import io.cloudevents.core.impl.StringUtils; import io.cloudevents.core.mock.MyCloudEventData; import io.cloudevents.core.provider.EventFormatProvider; import io.cloudevents.core.test.Data; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; public class JsonCloudEventDataTest { - @Test - public void testMapper() { + @ParameterizedTest + @MethodSource("textContentArguments") + public void testMapper(String contentType) { CloudEvent event = CloudEventBuilder.v1(Data.V1_MIN) - .withData("application/json", new JsonCloudEventData(JsonNodeFactory.instance.numberNode(10))) + .withData(contentType, new JsonCloudEventData(JsonNodeFactory.instance.numberNode(10))) .build(); byte[] serialized = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) @@ -53,4 +61,11 @@ public void testMapper() { .isEqualTo(10); } + public static Stream textContentArguments() { + return Stream.of( + Arguments.of("application/json"), + Arguments.of("text/json"), + Arguments.of("application/fubar+json") + ); + } } From 83431452a72f5166af7af21b0fc3d893367c2349 Mon Sep 17 00:00:00 2001 From: Isaac Aymerich Date: Fri, 2 Sep 2022 08:47:16 +0200 Subject: [PATCH 2/4] minor renames Signed-off-by: Isaac Aymerich --- .../src/main/java/io/cloudevents/jackson/JsonFormat.java | 4 ++-- .../java/io/cloudevents/jackson/JsonCloudEventDataTest.java | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java b/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java index b6d35b9f7..8282d305d 100644 --- a/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java +++ b/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java @@ -47,7 +47,7 @@ public final class JsonFormat implements EventFormat { /** * Suppoted Content type */ - private static final Pattern PATTERN_CONTENT_TYPE = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json$"); + private static final Pattern JSON_CONTENT_TYPE = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json$"); private final ObjectMapper mapper; private final JsonFormatOptions options; @@ -218,6 +218,6 @@ public static SimpleModule getCloudEventJacksonModule(JsonFormatOptions options) static boolean dataIsJsonContentType(String contentType) { // If content type, spec states that we should assume is json - return contentType == null || PATTERN_CONTENT_TYPE.matcher(contentType).matches(); + return contentType == null || JSON_CONTENT_TYPE.matcher(contentType).matches(); } } diff --git a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java index 8b71d3a77..e83d08cd1 100644 --- a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java +++ b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java @@ -20,12 +20,9 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory; import io.cloudevents.CloudEvent; import io.cloudevents.core.builder.CloudEventBuilder; -import io.cloudevents.core.impl.StringUtils; import io.cloudevents.core.mock.MyCloudEventData; import io.cloudevents.core.provider.EventFormatProvider; import io.cloudevents.core.test.Data; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -65,7 +62,7 @@ public static Stream textContentArguments() { return Stream.of( Arguments.of("application/json"), Arguments.of("text/json"), - Arguments.of("application/fubar+json") + Arguments.of("application/foobar+json") ); } } From fd39af2901d62977240753522130676f2026c42f Mon Sep 17 00:00:00 2001 From: Isaac Aymerich Date: Fri, 2 Sep 2022 08:50:47 +0200 Subject: [PATCH 3/4] minor renames Signed-off-by: Isaac Aymerich --- .../src/main/java/io/cloudevents/jackson/JsonFormat.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java b/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java index 8282d305d..96e579b51 100644 --- a/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java +++ b/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java @@ -47,7 +47,7 @@ public final class JsonFormat implements EventFormat { /** * Suppoted Content type */ - private static final Pattern JSON_CONTENT_TYPE = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json$"); + private static final Pattern JSON_CONTENT_TYPE_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json$"); private final ObjectMapper mapper; private final JsonFormatOptions options; @@ -218,6 +218,6 @@ public static SimpleModule getCloudEventJacksonModule(JsonFormatOptions options) static boolean dataIsJsonContentType(String contentType) { // If content type, spec states that we should assume is json - return contentType == null || JSON_CONTENT_TYPE.matcher(contentType).matches(); + return contentType == null || JSON_CONTENT_TYPE_PATTERN.matcher(contentType).matches(); } } From 523486379f88377c1bd964593d191129b49c7dd5 Mon Sep 17 00:00:00 2001 From: Isaac Aymerich Date: Sat, 3 Sep 2022 13:40:52 +0200 Subject: [PATCH 4/4] check dataContentType matches on deserialized CloudEvent Signed-off-by: Isaac Aymerich --- .../src/main/java/io/cloudevents/jackson/JsonFormat.java | 2 +- .../java/io/cloudevents/jackson/JsonCloudEventDataTest.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java b/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java index 96e579b51..8454a5a22 100644 --- a/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java +++ b/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java @@ -45,7 +45,7 @@ public final class JsonFormat implements EventFormat { */ public static final String CONTENT_TYPE = "application/cloudevents+json"; /** - * Suppoted Content type + * JSON Data Content Type Discriminator */ private static final Pattern JSON_CONTENT_TYPE_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json$"); private final ObjectMapper mapper; diff --git a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java index e83d08cd1..9ae8cfc26 100644 --- a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java +++ b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonCloudEventDataTest.java @@ -52,6 +52,8 @@ public void testMapper(String contentType) { return new MyCloudEventData(((JsonCloudEventData) data).getNode().asInt()); }); + assertThat(deserialized.getDataContentType()) + .isEqualTo(contentType); assertThat(deserialized.getData()) .isInstanceOf(MyCloudEventData.class); assertThat(((MyCloudEventData) deserialized.getData()).getValue())