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

Add additional CloudEvent fields (pubsubname, topic, time, etc) #866

Merged
merged 23 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
499b5af
Added additional CloudEvent fields (pubsubname, topic, time, traceid,…
siebenluke May 24, 2023
d449a4e
Merge branch 'master' into add-additional-cloudevent-fields
siebenluke May 24, 2023
0df85b3
Refactored new field names to be camelCase
siebenluke May 24, 2023
a6c79fa
Merge branch 'master' into add-additional-cloudevent-fields
siebenluke May 24, 2023
1ec1ec4
Added @JsonProperty("{lowercasename}")s to properly serdes camelCaseN…
siebenluke May 25, 2023
d313ee1
Merge branch 'master' into add-additional-cloudevent-fields
siebenluke Jun 8, 2023
d077371
Merge branch 'master' into add-additional-cloudevent-fields
siebenluke Jun 26, 2023
f528923
Removed com.fasterxml.jackson.datatype:jackson-datatype-jsr310 depend…
siebenluke Jul 11, 2023
97f2920
Merge branch 'master' into add-additional-cloudevent-fields
siebenluke Aug 14, 2023
421ca34
Fixed "Line is longer than 120 characters" build issue by pushing the…
siebenluke Aug 16, 2023
3ee6df1
Merge branch 'master' into add-additional-cloudevent-fields
siebenluke Dec 21, 2023
698200e
Merge branch 'master' into add-additional-cloudevent-fields
dapr-bot Dec 22, 2023
2c45493
Merge branch 'master' into add-additional-cloudevent-fields
dapr-bot Jan 5, 2024
1321db3
Merge branch 'master' into add-additional-cloudevent-fields
cicoyle Jan 5, 2024
78e9575
Added more CloudEvent test cases to appease Codecov
siebenluke Jan 5, 2024
ce8c677
Merge branch 'master' into add-additional-cloudevent-fields
dapr-bot Jan 6, 2024
210ac6e
Merge branch 'master' into add-additional-cloudevent-fields
dapr-bot Jan 8, 2024
56d7e8c
Merge branch 'master' into add-additional-cloudevent-fields
dapr-bot Jan 8, 2024
54dc867
Merge branch 'master' into add-additional-cloudevent-fields
dapr-bot Jan 9, 2024
a340cab
Added null binaryData test case for Codecov
siebenluke Jan 10, 2024
aa091ee
Added cloudEventDifferent test cases for Codecov
siebenluke Jan 10, 2024
d38e033
Removed extraneous ;
siebenluke Jan 10, 2024
e6237bb
Added comments for time test cases
siebenluke Jan 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
<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>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
Expand All @@ -64,7 +69,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
4 changes: 3 additions & 1 deletion sdk/src/main/java/io/dapr/client/ObjectSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.protobuf.MessageLite;
import io.dapr.client.domain.CloudEvent;
import io.dapr.utils.TypeRef;
Expand All @@ -35,7 +36,8 @@ public class ObjectSerializer {
*/
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
siebenluke marked this conversation as resolved.
Show resolved Hide resolved
.setSerializationInclusion(JsonInclude.Include.NON_NULL).findAndRegisterModules();

/**
* Default constructor to avoid class from being instantiated outside package but still inherited.
Expand Down
151 changes: 147 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 @@ -17,8 +17,10 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

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

Expand All @@ -38,7 +40,8 @@
*/
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL).findAndRegisterModules();

/**
* Identifier of the message being processed.
Expand Down Expand Up @@ -76,6 +79,41 @@
@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.
*/
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 @@ -127,7 +165,7 @@
this.datacontenttype = "application/octet-stream";
this.binaryData = binaryData == null ? null : Arrays.copyOf(binaryData, binaryData.length);;
}

/**
* Deserialize a message topic from Dapr.
*
Expand Down Expand Up @@ -255,6 +293,104 @@
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 +409,21 @@
&& 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,

Check warning on line 426 in sdk/src/main/java/io/dapr/client/domain/CloudEvent.java

View check run for this annotation

Codecov / codecov/patch

sdk/src/main/java/io/dapr/client/domain/CloudEvent.java#L426

Added line #L426 was not covered by tests
traceId, traceParent, traceState);
}
}
19 changes: 18 additions & 1 deletion sdk/src/test/java/io/dapr/client/CloudEventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import io.dapr.client.domain.CloudEvent;
import org.junit.Test;

import java.time.OffsetDateTime;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
Expand All @@ -42,7 +44,12 @@ public void deserializeObjectClass() throws Exception {
" \"comexampleextension1\" : \"value\",\n" +
" \"comexampleothervalue\" : 5,\n" +
" \"datacontenttype\" : \"application/json\",\n" +
" \"data\" : {\"id\": 1, \"name\": \"hello world\"}\n" +
" \"data\" : {\"id\": 1, \"name\": \"hello world\"},\n" +
" \"pubsubname\" : \"mypubsubname\",\n" +
" \"topic\" : \"mytopic\",\n" +
" \"traceid\" : \"Z987-0987-0987\",\n" +
" \"traceparent\" : \"Z987-0987-0987\",\n" +
" \"tracestate\" : \"\"\n" +
"}";

MyClass expected = new MyClass() {{
Expand All @@ -51,7 +58,17 @@ public void deserializeObjectClass() throws Exception {
}};

CloudEvent cloudEvent = CloudEvent.deserialize(content.getBytes());
assertEquals("1.0", cloudEvent.getSpecversion());
assertEquals("com.github.pull_request.opened", cloudEvent.getType());
assertEquals("https://github.com/cloudevents/spec/pull", cloudEvent.getSource());
assertEquals("A234-1234-1234", cloudEvent.getId());
assertEquals(OffsetDateTime.parse("2018-04-05T17:31:00Z"), cloudEvent.getTime());
assertEquals("application/json", cloudEvent.getDatacontenttype());
assertEquals("mypubsubname", cloudEvent.getPubsubName());
assertEquals("mytopic", cloudEvent.getTopic());
assertEquals("Z987-0987-0987", cloudEvent.getTraceId());
assertEquals("Z987-0987-0987", cloudEvent.getTraceParent());
assertEquals("", cloudEvent.getTraceState());
MyClass myObject = OBJECT_MAPPER.convertValue(cloudEvent.getData(), MyClass.class);
assertEquals(expected.id, myObject.id);
assertEquals(expected.name, myObject.name);
Expand Down