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

Serialization Problems #74

Closed
JohannesHampel opened this issue Nov 20, 2019 · 1 comment
Closed

Serialization Problems #74

JohannesHampel opened this issue Nov 20, 2019 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@JohannesHampel
Copy link

Hi there,

i use cloudevents sdk version 1.0.0, jackson 2.9.8.

when I run the following test, the "data" node appears twice in the resulting JSON (see last println line):

import io.cloudevents.CloudEvent;
import io.cloudevents.extensions.ExtensionFormat;
import io.cloudevents.format.StructuredMarshaller;
import io.cloudevents.format.Wire;
import io.cloudevents.format.builder.EventStep;
import io.cloudevents.json.Json;
import io.cloudevents.v1.Accessor;
import io.cloudevents.v1.AttributesImpl;
import io.cloudevents.v1.CloudEventBuilder;
import io.cloudevents.v1.kafka.HeaderMapper;
import org.junit.Test;

import java.net.URI;
import java.net.URISyntaxException;
import java.time.ZonedDateTime;
import java.util.UUID;

public class ATest {

    public static class Amount {
        private int a;

        public Amount() {}

        public Amount(int a) {
            this.a = a;
        }

        public int getA() {
            return a;
        }

        public void setA(int a) {
            this.a = a;
        }
    }

    @Test
    public void test1() throws URISyntaxException {
        Amount payload = new Amount(1);
        CloudEvent<AttributesImpl, Amount> event =
                CloudEventBuilder.<Amount>builder()
                        .withId(UUID.randomUUID().toString())
                        .withTime(ZonedDateTime.now())
                        .withDataContentType("application/cloudevent+json")
                        .withDataschema(new URI("structured_schema"))
                        .withSource(new URI("structured_source"))
                        .withSubject("structured_subject")
                        .withData(payload)
                        .withType("structured_type")
                        .build();
        EventStep<AttributesImpl, Amount, byte[], byte[]> builder =
                StructuredMarshaller.<AttributesImpl, Amount, byte[], byte[]>builder()
                        .mime("Content-Type", "application/cloudevents+json".getBytes())
                        .map((e) -> Json.binaryEncode(e))
                        .map(Accessor::extensionsOf)
                        .map(ExtensionFormat::marshal)
                        .map(HeaderMapper::map);
        Wire<byte[], String, byte[]> wire = builder.withEvent(() -> event).marshal();
        System.err.println(new String(wire.getPayload().get()));
    }
}

So the output is something like:

{
  "data": {
    "a": 1
  },
  "id": "88f599ed-e684-4746-a545-5abdd01ac3a5",
  "source": "structured_source",
  "specversion": "1.0",
  "type": "structured_type",
  "datacontenttype": "application/cloudevent+json",
  "dataschema": "structured_schema",
  "subject": "structured_subject",
  "time": "2019-11-20T14:03:53.613+01:00",
  "data": {
    "a": 1
  }
}

Could you please tell me what I am doing wrong?

TIA,

Johannes Hampel

@fabiojose fabiojose added bug Something isn't working good first issue Good for newcomers labels Nov 21, 2019
@sigrist
Copy link

sigrist commented Nov 27, 2019

Hi!

Is there a plan to release this issue? I can't use the API cause is generating invalid JSONs.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants