cloudevents / cloudevents/sdk-java

Unable to serialize complex (custom) extension in the expected json format

Open
#667 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
446
Forks
172
PR merge metrics
No merged PRs in 30d

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
Screenshot 2024-09-06 at 4 22 54 PM

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.

CloudEventExtension_Methods

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at core/src/main/java/io/cloudevents/core/impl/BaseCloudEvent.java around line 84, where extension values are read during serialization. Reproduce the issue with the custom Recipient extension and the withExtension overloads shown in the report, then compare the serialized output with the expected nested recipient JSON. Done means custom extension objects serialize without JsonMappingException and are not emitted as escaped JSON strings.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.