Closed afrittoli closed 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.
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"
}
}
}
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:
new*
helper functions to produce CDEvents
objects instead of CloudEvents
CDEvents
object to JSONCDEvents
object as a CloudEvent
. This function needs to create a CloudEvent, set some of the fields using the CDEvents
context, and set the CDEvents
rendered as JSON into the data part of the CloudEvent
Eventually, it would be good to add functions to parse JSON blobs back into CDEvent
objects as well.
🎉 This issue has been resolved in v0.1.2
(Release Notes)
The
CloudEvents
binding does not match the spec, it usesCloudEvents
extensions as opposed to including a JSON body that matches the jsonschema from the spec.