Skip to content

Commit 8cc50ea

Browse files
authored
Strip parameters from data content types to assess if it's JSON format (#484) (#485)
Signed-off-by: Frederic Delechamp <fdelechamp@guidewire.com>
1 parent ada31a4 commit 8cc50ea

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final class JsonFormat implements EventFormat {
4747
/**
4848
* JSON Data Content Type Discriminator
4949
*/
50-
private static final Pattern JSON_CONTENT_TYPE_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json$");
50+
private static final Pattern JSON_CONTENT_TYPE_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json(;.*)*$");
5151
private final ObjectMapper mapper;
5252
private final JsonFormatOptions options;
5353

formats/json-jackson/src/test/java/io/cloudevents/jackson/JsonFormatTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ class JsonFormatTest {
4747

4848
private final ObjectMapper mapper = new ObjectMapper();
4949

50+
@ParameterizedTest
51+
@MethodSource("jsonContentTypes")
52+
void isJsonContentType(String contentType) {
53+
boolean json = JsonFormat.dataIsJsonContentType(contentType);
54+
55+
assertThat(json).isTrue();
56+
}
57+
58+
@ParameterizedTest
59+
@MethodSource("wrongJsonContentTypes")
60+
void isNotJsonContentType(String contentType) {
61+
boolean json = JsonFormat.dataIsJsonContentType(contentType);
62+
63+
assertThat(json).isFalse();
64+
}
65+
5066
@ParameterizedTest
5167
@MethodSource("serializeTestArgumentsDefault")
5268
void serialize(CloudEvent input, String outputFile) throws IOException {
@@ -151,6 +167,39 @@ void verifyDeserializeError(String inputFile){
151167

152168
}
153169

170+
static Stream<Arguments> jsonContentTypes() {
171+
return Stream.of(
172+
Arguments.of("application/json"),
173+
Arguments.of("application/json;charset=utf-8"),
174+
Arguments.of("application/json;\tcharset = \"utf-8\""),
175+
Arguments.of("application/cloudevents+json;charset=UTF-8"),
176+
Arguments.of("text/json"),
177+
Arguments.of("text/json;charset=utf-8"),
178+
Arguments.of("text/cloudevents+json;charset=UTF-8"),
179+
Arguments.of("text/json;\twhatever"),
180+
Arguments.of("text/json; boundary=something"),
181+
Arguments.of("text/json;foo=\"bar\""),
182+
Arguments.of("text/json; charset = \"us-ascii\""),
183+
Arguments.of("text/json; \t"),
184+
Arguments.of("text/json;"),
185+
//https://www.rfc-editor.org/rfc/rfc2045#section-5.1
186+
// any us-ascii char can be part of parameters (except CTRLs and tspecials)
187+
Arguments.of("text/json; char-set = $!#$%&'*+.^_`|"),
188+
Arguments.of((Object) null)
189+
);
190+
}
191+
192+
static Stream<Arguments> wrongJsonContentTypes() {
193+
return Stream.of(
194+
Arguments.of("applications/json"),
195+
Arguments.of("application/jsom"),
196+
Arguments.of("application/jsonwrong"),
197+
Arguments.of("text/json "),
198+
Arguments.of("text/json ;"),
199+
Arguments.of("test/json")
200+
);
201+
}
202+
154203
public static Stream<Arguments> serializeTestArgumentsDefault() {
155204
return Stream.of(
156205
Arguments.of(V03_MIN, "v03/min.json"),

0 commit comments

Comments
 (0)