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 8454a5a22..7297e044c 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 { /** * JSON Data Content Type Discriminator */ - private static final Pattern JSON_CONTENT_TYPE_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json$"); + private static final Pattern JSON_CONTENT_TYPE_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json(;[\\s\\w\"!#$%&'*+.^_`|~=-]*)*$"); private final ObjectMapper mapper; private final JsonFormatOptions options; diff --git a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java index 7e40fe98e..253e2fc89 100644 --- a/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java +++ b/formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java @@ -47,6 +47,22 @@ class JsonFormatTest { private final ObjectMapper mapper = new ObjectMapper(); + @ParameterizedTest + @MethodSource("jsonContentTypes") + void isJsonContentType(String contentType) { + boolean json = JsonFormat.dataIsJsonContentType(contentType); + + assertThat(json).isTrue(); + } + + @ParameterizedTest + @MethodSource("wrongJsonContentTypes") + void isNotJsonContentType(String contentType) { + boolean json = JsonFormat.dataIsJsonContentType(contentType); + + assertThat(json).isFalse(); + } + @ParameterizedTest @MethodSource("serializeTestArgumentsDefault") void serialize(CloudEvent input, String outputFile) throws IOException { @@ -151,6 +167,39 @@ void verifyDeserializeError(String inputFile){ } + static Stream jsonContentTypes() { + return Stream.of( + Arguments.of("application/json"), + Arguments.of("application/json;charset=utf-8"), + Arguments.of("application/json;\tcharset = \"utf-8\""), + Arguments.of("application/cloudevents+json;charset=UTF-8"), + Arguments.of("text/json"), + Arguments.of("text/json;charset=utf-8"), + Arguments.of("text/cloudevents+json;charset=UTF-8"), + Arguments.of("text/json;\twhatever"), + Arguments.of("text/json; boundary=something"), + Arguments.of("text/json;foo=\"bar\""), + Arguments.of("text/json; charset = \"us-ascii\""), + Arguments.of("text/json; \t"), + Arguments.of("text/json;"), + //https://www.rfc-editor.org/rfc/rfc2045#section-5.1 + // any us-ascii char can be part of parameters (except CTRLs and tspecials) + Arguments.of("text/json; char-set = $!#$%&'*+.^_`|"), + Arguments.of((Object) null) + ); + } + + static Stream wrongJsonContentTypes() { + return Stream.of( + Arguments.of("applications/json"), + Arguments.of("application/jsom"), + Arguments.of("application/jsonwrong"), + Arguments.of("text/json "), + Arguments.of("text/json ;"), + Arguments.of("test/json") + ); + } + public static Stream serializeTestArgumentsDefault() { return Stream.of( Arguments.of(V03_MIN, "v03/min.json"),