cdevents / sdk-java

Java SDK for CDEvents
Apache License 2.0
5 stars 6 forks source link

The CloudEvents binding uses CloudEvents extensions #31

Closed afrittoli closed 1 year ago

afrittoli commented 1 year ago

The CloudEvents binding does not match the spec, it uses CloudEvents extensions as opposed to including a JSON body that matches the jsonschema from the spec.

afrittoli commented 1 year ago

It looks like jackson is the way to create object -> json bindings in Java nowadays, I wonder if we could use the existing object to produce JSON through the existing objects.

afrittoli commented 1 year ago

I used this code to generate an event and render it to JSON in structured mode:

package dev.cdevents;

import dev.cdevents.constants.CDEventConstants;
import io.cloudevents.CloudEvent;
import io.cloudevents.core.provider.EventFormatProvider;
import io.cloudevents.jackson.JsonFormat;
import java.net.URI;
import java.nio.charset.StandardCharsets;

public class CDEventsExample {

    public static void main(String[] args){
        try {
            URI uri = new URI("cdevents.dev");
            CloudEvent prfe = CDEventTypes.createPipelineRunFinishedEvent(
                CDEventConstants.CDEventTypes.PipelineRunFinishedEvent.getEventType(),
                "id", uri, "name", uri,
                        CDEventConstants.Outcome.OutcomeSuccess, "errors", "{\"foo\": \"bar\"}");
            byte[]serialized = EventFormatProvider
                .getInstance()
                .resolveFormat(JsonFormat.CONTENT_TYPE)
                .serialize(prfe);
            String s = new String(serialized, StandardCharsets.UTF_8);
            System.out.println(s);
        } catch (Exception e) {
            System.err.printf("Exception occurred when creating "
            + "PipelineRun Finished Event {}", e);
        }
    }
}

The resulting event is rendered as

{
  "specversion": "0.3",
  "id": "id",
  "source": "cdevents.dev",
  "type": "dev.cdevents.pipelinerun.finished.0.1.0",
  "time": "2023-04-19T13:42:44.610621+02:00",
  "pipelinename": "name",
  "url": "cdevents.dev",
  "outcome": "success",
  "errors": "errors",
  "data": {
    "foo": "bar"
  }
}

Which is quite different from what the cdevents spec defines. To compare, I include here the event in cloudevents structured mode. The content of the "data" section is the actual CDEvent.

{
    "specversion": "0.3",
    "id": "id",
    "source": "cdevents.dev",
    "type": "dev.cdevents.pipelinerun.finished.0.1.0",
    "time": "2023-04-19T13:42:44.610621+02:00",
    "data": {
        "context": {
            "version": "0.2.0",
            "id": "271069a8-fc18-44f1-b38f-9d70a1695819",
            "source": "cdevents.dev",
            "type": "dev.cdevents.pipelinerun.finished.0.1.1",
            "timestamp": "2023-04-19T13:42:44.610621+02:00"
        },
        "subject": {
            "id": "id",
            "source": "cdevents.dev",
            "type": "pipelineRun",
            "content": {
                "pipelineName": "name",
                "url": "cdevents.dev",
                "outcome": "success",
                "errors": "errors"
            }
        },
        "customData": {
            "foo": "bar"
        }
    }
}
afrittoli commented 1 year ago

It looks like the only thing missing from the data model is customData - see https://github.com/cdevents/sdk-java/issues/30.

Once that is added, I think it would be nice to make the following changes:

Eventually, it would be good to add functions to parse JSON blobs back into CDEvent objects as well.

cdevents-bot commented 1 year ago

🎉 This issue has been resolved in v0.1.2 (Release Notes)