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 9 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
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
172 changes: 169 additions & 3 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 @@ -127,7 +174,7 @@ public CloudEvent(
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 +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,35 @@ 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());
}
}
}
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