Skip to content

[BUG] Serialization of CloudEvent through Jackson does not include the serialized data object #36000

@tomsnor

Description

@tomsnor

Describe the bug
Due to the current implementation of CloudEvent, the data attribute of the CloudEvent is represented as org.example.functions.SerializableContent instead of the desired class.

To Reproduce
Steps to reproduce the behavior:

  • Serialize a CloudEvent with Jackson

Code Snippet

package org.example.functions;

import com.azure.core.models.CloudEvent;
import com.azure.core.models.CloudEventDataFormat;
import com.azure.core.util.BinaryData;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Jackson {

    private static class Foo {
        private final String bar;

        public Foo(String bar) {
            this.bar = bar;
        }

        public String getBar() {
            return bar;
        }
    }

    public static void main(String[] args) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        BinaryData binaryData = BinaryData.fromObject(new Foo("bar"));
        CloudEvent cloudEvent = new CloudEvent("mySource", "myType", binaryData, CloudEventDataFormat.JSON, "application/json");
        System.out.println(objectMapper.writeValueAsString(cloudEvent));
    }
}

Expected behavior
The data object contains a correct representation:

{"id":"c4c53894-46ab-40ff-abe7-9fe2bc8c215e","source":"mySource","data":{"bar":"bar"},"data_base64":null,"type":"myType","time":null,"specversion":"1.0","dataschema":null,"datacontenttype":"application/json","subject":null}

instead of

{"id":"c4c53894-46ab-40ff-abe7-9fe2bc8c215e","source":"mySource","data":{"replayable":true,"length":13},"data_base64":null,"type":"myType","time":null,"specversion":"1.0","dataschema":null,"datacontenttype":"application/json","subject":null}

Setup (please complete the following information):

  • OS: MacOs
  • IDE: IntelliJ
  • Library/Libraries: com.azure:azure-core:1.41.0
  • Java version: 8
  • App Server/Environment: N.A.
  • Frameworks: N.A

Additional context

  @JsonProperty("data")
    private JsonNode data;
    @JsonProperty("data_base64")
    private String dataBase64;
    @JsonIgnore
    private BinaryData binaryData;

    public BinaryData getData() {
        if (this.binaryData == null) {
            if (this.data != null) {
                this.binaryData = BinaryData.fromObject(this.data, SERIALIZER);
            } else if (this.dataBase64 != null) {
                this.binaryData = BinaryData.fromBytes(Base64.getDecoder().decode(this.dataBase64));
            }
        }

        return this.binaryData;
    }

Information Checklist
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

  • Bug Description Added
  • Repro Steps Added
  • Setup information Added

Metadata

Metadata

Labels

ClientThis issue points to a problem in the data-plane of the library.Event Gridcustomer-reportedIssues that are reported by GitHub users external to the Azure organization.questionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions