cloudevents / cloudevents/sdk-java
JSON serialization - extensions of types OffsetDateTime and bytes[] deserialized as Strings
Open
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 446
- Forks
- 172
- PR merge metrics
- No merged PRs in 30d
Description
Extensions of types OffsetDateTime and bytes[] are not restored properly after serialization and deserialization using JSON format. Values are Strings.
class JsonSerializationTest {
final CloudEventJsonSerializer serializer = new CloudEventJsonSerializer();
final CloudEventJsonDeserializer deserializer = new CloudEventJsonDeserializer();
@Test
void shouldSerializeAndDeserialize() {
CloudEvent serialized = createTestEvent();
Buffer buffer = serializer.serialize(serialized);
CloudEvent deserialized = deserializer.deserialize(buffer);
assertEvent(serialized, deserialized);
}
private CloudEvent createTestEvent() {
return CloudEventBuilder.v1()
.withId("id")
.withSource(URI.create("source"))
.withType("type")
.withDataContentType("datacontenttype")
.withDataSchema(URI.create("https://data/schema"))
.withSubject("subject")
.withTime(OffsetDateTime.now(ZoneId.of("UTC")))
.withExtension("extstring", "extstring-value")
.withExtension("extboolean", true)
.withExtension("extint", 123)
// Not working, deserialized value is string
// .withExtension("exttime", OffsetDateTime.now(ZoneId.of("UTC")))
// Not working, deserialized value is string
// .withExtension("extbytes", "bytes".getBytes(StandardCharsets.UTF_8))
.withData("test data".getBytes(StandardCharsets.UTF_8))
.build();
}
private void assertEvent(CloudEvent expected, CloudEvent actual) {
assertNotNull(actual);
assertEquals(expected.getId(), actual.getId());
assertEquals(expected.getSource(), actual.getSource());
assertEquals(expected.getSpecVersion(), actual.getSpecVersion());
assertEquals(expected.getType(), actual.getType());
assertEquals(expected.getDataContentType(), actual.getDataContentType());
assertEquals(expected.getDataSchema(), actual.getDataSchema());
assertEquals(expected.getSubject(), actual.getSubject());
assertEquals(expected.getTime(), actual.getTime());
assertEquals(expected.getData(), actual.getData());
assertEquals(expected.getExtensionNames(), actual.getExtensionNames());
expected.getExtensionNames().forEach(extName -> {
if (expected.getExtension(extName) instanceof byte[]) {
assertInstanceOf(byte[].class, actual.getExtension(extName),
"Extension " + extName + " is not a byte[]");
assertArrayEquals((byte[]) expected.getExtension(extName),
(byte[]) actual.getExtension(extName),
"Values not equal for extension " + extName);
} else {
assertEquals(expected.getExtension(extName), actual.getExtension(extName),
"Values not equal for extension " + extName);
}
}
);
}
}
public class CloudEventJsonSerializer implements Serializer<CloudEvent> {
@Override
public boolean handles(Object payload) {
return payload instanceof CloudEvent;
}
@Override
public Buffer serialize(CloudEvent payload) {
byte[] serialized = Objects.requireNonNull(
EventFormatProvider
.getInstance()
.resolveFormat(ContentType.JSON),
"JSON event format dependency missing")
.serialize(payload);
return Buffer.buffer(serialized);
}
}
public class CloudEventJsonDeserializer implements Deserializer<CloudEvent> {
@Override
public CloudEvent deserialize(Buffer payload) {
return Objects.requireNonNull(
EventFormatProvider
.getInstance()
.resolveFormat(ContentType.JSON),
"JSON event format dependency missing")
.deserialize(payload.getBytes());
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the JsonSerializationTest reproduction and the CloudEventJsonSerializer and CloudEventJsonDeserializer entry points shown in the issue. Run the test with OffsetDateTime and bytes[] extensions enabled, then trace the EventFormatProvider JSON format handling. Done means those extension values retain their original types and the assertions pass after round-trip serialization.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100