Support dynamic JSON data content type (#471)

Now checks if `datacontenttype` matches the regex:

`^(application|text)\/([a-zA-Z]+\+)?json$")`

This regex support 
`application/foobar+json`
or standard
```
application/json
text/json
```

Signed-off-by: Isaac Aymerich <isaac.aymerich@roche.com>
This commit is contained in:
Isaac Aymerich 2022-09-05 13:14:38 +02:00 committed by GitHub
parent 9125136530
commit f8d27b08bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 6 deletions

View File

@ -29,6 +29,7 @@ import io.cloudevents.rw.CloudEventDataMapper;
import io.cloudevents.rw.CloudEventRWException; import io.cloudevents.rw.CloudEventRWException;
import java.io.IOException; import java.io.IOException;
import java.util.regex.Pattern;
/** /**
* Implementation of {@link EventFormat} for <a href="https://github.com/cloudevents/spec/blob/v1.0/json-format.md">JSON event format</a> * Implementation of {@link EventFormat} for <a href="https://github.com/cloudevents/spec/blob/v1.0/json-format.md">JSON event format</a>
@ -43,7 +44,10 @@ public final class JsonFormat implements EventFormat {
* Content type associated with the JSON event format * Content type associated with the JSON event format
*/ */
public static final String CONTENT_TYPE = "application/cloudevents+json"; public static final String CONTENT_TYPE = "application/cloudevents+json";
/**
* 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; private final ObjectMapper mapper;
private final JsonFormatOptions options; private final JsonFormatOptions options;
@ -214,6 +218,6 @@ public final class JsonFormat implements EventFormat {
static boolean dataIsJsonContentType(String contentType) { static boolean dataIsJsonContentType(String contentType) {
// If content type, spec states that we should assume is json // 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 || JSON_CONTENT_TYPE_PATTERN.matcher(contentType).matches();
} }
} }

View File

@ -23,16 +23,21 @@ import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.mock.MyCloudEventData; import io.cloudevents.core.mock.MyCloudEventData;
import io.cloudevents.core.provider.EventFormatProvider; import io.cloudevents.core.provider.EventFormatProvider;
import io.cloudevents.core.test.Data; import io.cloudevents.core.test.Data;
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; import static org.assertj.core.api.Assertions.assertThat;
public class JsonCloudEventDataTest { public class JsonCloudEventDataTest {
@Test @ParameterizedTest
public void testMapper() { @MethodSource("textContentArguments")
public void testMapper(String contentType) {
CloudEvent event = CloudEventBuilder.v1(Data.V1_MIN) 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(); .build();
byte[] serialized = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE) byte[] serialized = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE)
@ -47,10 +52,19 @@ public class JsonCloudEventDataTest {
return new MyCloudEventData(((JsonCloudEventData) data).getNode().asInt()); return new MyCloudEventData(((JsonCloudEventData) data).getNode().asInt());
}); });
assertThat(deserialized.getDataContentType())
.isEqualTo(contentType);
assertThat(deserialized.getData()) assertThat(deserialized.getData())
.isInstanceOf(MyCloudEventData.class); .isInstanceOf(MyCloudEventData.class);
assertThat(((MyCloudEventData) deserialized.getData()).getValue()) assertThat(((MyCloudEventData) deserialized.getData()).getValue())
.isEqualTo(10); .isEqualTo(10);
} }
public static Stream<Arguments> textContentArguments() {
return Stream.of(
Arguments.of("application/json"),
Arguments.of("text/json"),
Arguments.of("application/foobar+json")
);
}
} }