Unable to serialize complex (custom) extension in the expected json format #667
Description
creating a custom extension object implementing CloudEventExtension, when using the extension (customExt) as below, and then serializing the CloudEvent, the serialization for the CloudEvent is failing with JsonMappingException.
CloudEventBuilder.v1()
.withSource(URI.create(source))
.withType(event_type)
.withTime(event_time)
.withId(event.getEventId())
.withDataContentType(contentType)
.withData(eventPayaload)
.withDataSchema(URI.create(XDM_SCHEMA_URI))
.withExtension(DATA_SCHEMA_VERSION_KEY_EXTENSION_NAME, schemaVerExt)
.withExtension(X_ACTION_ID_KEY_EXTENSION_NAME, requestIdExt)
.withExtension(customExt);here is how the extension looks like. The customExt is the Recipient object

Also, if using the other supported methods withExtension (with key, value) as below, I had to send the string version of the customExt, and then if serialized the output is not in the expected format (see below). It can break our customers event
integrations. Kindly suggest.
expected serialized CloudEvent custom extension
"recipient":{"userid":"9CE048DB5E8F485XXXXXXXXX@AdobeOrg"}actual serialized CloudEvent custom extension
"recipient":"{\"userid\":\"9CE048DB5E8F485XXXXXXXXX@AdobeOrg\"}"The serialization is failing while reading extensions here, as I believe the Object type value is not supported.

Activity
abhupadh commentedon Sep 13, 2024
This expected json for extension was working fine with CloudEvent 1.0 , and
ExtensionFormat/InMemoryFormatmarshaling / unmarshaling feature.shikhartanwar commentedon Sep 18, 2024
+1
nicdard commentedon Sep 19, 2024
+1
pierDipi commentedon Sep 26, 2024
In the CloudEvents spec, the type system for metadata https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#type-system describes the type "String" but what you expect is a JSON value ?
Also please, add the full steps to reproduce the issue that I can and paste
abhupadh commentedon Oct 1, 2024
Thanks @pierDipi for responding. Yes, that is the correct expectation (receiving a json value). Below is the code and steps to reproduce the issues while trying both the ways of setting an extension to a
CloudEvent------- Issue 1 ---------
RecipientobjectCreating a custom extension
RecipientExtensionusing theRecipientobjectCreate the CloudEvent object using
CloudEventBuilder.v1()Now serialize the
CloudEventV1objectthe above serialization will fail with


JsonMappingExceptionwith the underlying stacktrace showing failing at the readContext ofCloudEventSerializer-------- Issue 2 --------
Instead of using the custom extension object, if we use the serialized string of it using
withExtension(String key, String value)we are not getting the json representation of itget the
Recipientobject using the recipientExt.getRecipient() and set as extension while constructingCloudEventwith its serialized versionabhupadh commentedon Oct 30, 2024
Hi @pierDipi, is there any update on this? We are blocked with our java17 upgrade due to this.
abhupadh commentedon Dec 3, 2024
Hi @pierDipi, checking up on it, did you get a chance to look into it?