Skip to content

Commit

Permalink
Add additional CloudEvent fields (pubsubname, topic, time, etc) (#866)
Browse files Browse the repository at this point in the history
* Added additional CloudEvent fields (pubsubname, topic, time, traceid, traceparent, & tracestate)

Added the com.fasterxml.jackson:jackson-datatype-jsr310 dependency to handle serdes of OffsetDateTime for the CloudEvent time field via ObjectMapper settings .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) & .findAndRegisterModules()

Updated com.fasterxml.jackson dependencies to the latest 2.15.1

Added OffsetDateTime as timeValue to test the DefaultObjectSerializer

Added more tests for new & old CloudEvent fields in CloudEventTest & DefaultObjectSerializerTest

Signed-off-by: Luke Sieben <siebenluke@gmail.com>

* Refactored new field names to be camelCase

Removed the 2 new constructors

Signed-off-by: Luke Sieben <siebenluke@gmail.com>

* Added @JsonProperty("{lowercasename}")s to properly serdes camelCaseNames as JSON/OBJECT_MAPPER are case-sensitive

Signed-off-by: Luke Sieben <siebenluke@gmail.com>

* Removed com.fasterxml.jackson.datatype:jackson-datatype-jsr310 dependency in favor of custom field level serdes for time

Signed-off-by: Luke Sieben <siebenluke@gmail.com>

* Fixed "Line is longer than 120 characters" build issue by pushing the end of the offending lines to a new line

Signed-off-by: Luke Sieben <siebenluke@gmail.com>

* Added more CloudEvent test cases to appease Codecov

Signed-off-by: Luke Sieben <siebenluke@gmail.com>

* Added null binaryData test case for Codecov

Signed-off-by: Luke Sieben <siebenluke@gmail.com>

* Added cloudEventDifferent test cases for Codecov

Signed-off-by: Luke Sieben <siebenluke@gmail.com>

* Removed extraneous ;

Signed-off-by: Luke Sieben <siebenluke@gmail.com>

* Added comments for time test cases

Signed-off-by: Luke Sieben <siebenluke@gmail.com>

---------

Signed-off-by: Luke Sieben <siebenluke@gmail.com>
Co-authored-by: Dapr Bot <56698301+dapr-bot@users.noreply.github.com>
Co-authored-by: Cassie Coyle <cassie@diagrid.io>
  • Loading branch information
3 people committed Jan 11, 2024
1 parent 3dc2a90 commit fdb4200
Show file tree
Hide file tree
Showing 4 changed files with 447 additions and 39 deletions.
4 changes: 2 additions & 2 deletions sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4.1</version>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
Expand All @@ -64,7 +64,7 @@
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.11.3</version>
<version>2.15.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
176 changes: 172 additions & 4 deletions sdk/src/main/java/io/dapr/client/domain/CloudEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Objects;

Expand Down Expand Up @@ -76,6 +86,43 @@ public class CloudEvent<T> {
@JsonProperty("data_base64")
private byte[] binaryData;

/**
* The pubsub component this CloudEvent came from.
*/
@JsonProperty("pubsubname")
private String pubsubName;

/**
* The topic this CloudEvent came from.
*/
private String topic;

/**
* The time this CloudEvent was created.
*/
@JsonSerialize(using = OffsetDateTimeSerializer.class)
@JsonDeserialize(using = OffsetDateTimeDeserializer.class)
private OffsetDateTime time;

/**
* The trace id is the legacy name for trace parent.
*/
@Deprecated
@JsonProperty("traceid")
private String traceId;

/**
* The trace parent.
*/
@JsonProperty("traceparent")
private String traceParent;

/**
* The trace state.
*/
@JsonProperty("tracestate")
private String traceState;

/**
* Instantiates a CloudEvent.
*/
Expand Down Expand Up @@ -125,9 +172,9 @@ public CloudEvent(
this.type = type;
this.specversion = specversion;
this.datacontenttype = "application/octet-stream";
this.binaryData = binaryData == null ? null : Arrays.copyOf(binaryData, binaryData.length);;
this.binaryData = binaryData == null ? null : Arrays.copyOf(binaryData, binaryData.length);
}

/**
* Deserialize a message topic from Dapr.
*
Expand Down Expand Up @@ -255,6 +302,104 @@ public void setBinaryData(byte[] binaryData) {
this.binaryData = binaryData == null ? null : Arrays.copyOf(binaryData, binaryData.length);
}

/**
* Gets the pubsub component name.
* @return the pubsub component name.
*/
public String getPubsubName() {
return pubsubName;
}

/**
* Sets the pubsub component name.
* @param pubsubName the pubsub component name.
*/
public void setPubsubName(String pubsubName) {
this.pubsubName = pubsubName;
}

/**
* Gets the topic name.
* @return the topic name.
*/
public String getTopic() {
return topic;
}

/**
* Sets the topic name.
* @param topic the topic name.
*/
public void setTopic(String topic) {
this.topic = topic;
}

/**
* Gets the time.
* @return the time.
*/
public OffsetDateTime getTime() {
return time;
}

/**
* Sets the time.
* @param time the time.
*/
public void setTime(OffsetDateTime time) {
this.time = time;
}

/**
* Gets the trace id which is the legacy name for trace parent.
* @return the trace id.
*/
@Deprecated
public String getTraceId() {
return traceId;
}

/**
* Sets the trace id which is the legacy name for trace parent.
* @param traceId the trace id.
*/
@Deprecated
public void setTraceId(String traceId) {
this.traceId = traceId;
}

/**
* Gets the trace parent.
* @return the trace parent.
*/
public String getTraceParent() {
return traceParent;
}

/**
* Sets the trace parent.
* @param traceParent the trace parent.
*/
public void setTraceParent(String traceParent) {
this.traceParent = traceParent;
}

/**
* Gets the trace state.
* @return the trace state.
*/
public String getTraceState() {
return traceState;
}

/**
* Sets the trace state.
* @param traceState the trace state.
*/
public void setTraceState(String traceState) {
this.traceState = traceState;
}

/**
* {@inheritDoc}
*/
Expand All @@ -273,14 +418,37 @@ public boolean equals(Object o) {
&& Objects.equals(specversion, that.specversion)
&& Objects.equals(datacontenttype, that.datacontenttype)
&& Objects.equals(data, that.data)
&& Arrays.equals(binaryData, that.binaryData);
&& Arrays.equals(binaryData, that.binaryData)
&& Objects.equals(pubsubName, that.pubsubName)
&& Objects.equals(topic, that.topic)
&& ((time == null && that.time == null) || (time != null && that.time != null && time.isEqual(that.time)))
&& Objects.equals(traceId, that.traceId)
&& Objects.equals(traceParent, that.traceParent)
&& Objects.equals(traceState, that.traceState);
}

/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hash(id, source, type, specversion, datacontenttype, data, binaryData);
return Objects.hash(id, source, type, specversion, datacontenttype, data, binaryData, pubsubName, topic, time,
traceId, traceParent, traceState);
}

private static class OffsetDateTimeSerializer extends JsonSerializer<OffsetDateTime> {
@Override
public void serialize(OffsetDateTime offsetDateTime, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
}
}

private static class OffsetDateTimeDeserializer extends JsonDeserializer<OffsetDateTime> {
@Override
public OffsetDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException {
return OffsetDateTime.parse(jsonParser.getText());
}
}
}

0 comments on commit fdb4200

Please sign in to comment.