Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support dynamic data content type #471

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.cloudevents.rw.CloudEventRWException;

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>
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it's good to call it "Supported Content type".
Other content type should work too, they will just be exposed as byte[] not objects IIRC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, any suggestion for the naming?

Copy link
Contributor

@JemDay JemDay Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something along the lines of "JSON Data Content Type Discriminator" - long-winded I know ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JemDay Done!

*/
private static final Pattern JSON_CONTENT_TYPE_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json$");
private final ObjectMapper mapper;
private final JsonFormatOptions options;

Expand Down Expand Up @@ -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 || JSON_CONTENT_TYPE_PATTERN.matcher(contentType).matches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@
import io.cloudevents.core.mock.MyCloudEventData;
import io.cloudevents.core.provider.EventFormatProvider;
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;

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)
Expand All @@ -53,4 +58,11 @@ public void testMapper() {
.isEqualTo(10);
}

public static Stream<Arguments> textContentArguments() {
return Stream.of(
Arguments.of("application/json"),
Arguments.of("text/json"),
Arguments.of("application/foobar+json")
);
}
}