Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions sdk/src/main/java/io/dapr/client/DaprClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,29 @@ public Mono<Void> publishEvent(PublishEventRequest request) {
String pubsubName = request.getPubsubName();
String topic = request.getTopic();
Object data = request.getData();
DaprPubsubProtos.PublishEventRequest.Builder envelopeBuilder = DaprPubsubProtos.PublishEventRequest.newBuilder()
.setTopic(topic)
.setPubsubName(pubsubName)
.setData(ByteString.copyFrom(objectSerializer.serialize(data)));

// Content-type can be overwritten on a per-request basis.
// It allows CloudEvents to be handled differently, for example.
String contentType = request.getContentType();
if (contentType == null || contentType.isEmpty()) {
byte[] serializedData;
if (Strings.isNullOrEmpty(contentType)) {
serializedData = objectSerializer.serialize(data);
contentType = objectSerializer.getContentType();
} else if (objectSerializer instanceof DefaultObjectSerializer
&& DefaultContentTypeConverter.isStringContentType(contentType)
&& (data instanceof String || data instanceof Number || data instanceof Boolean)) {
// Text content is sent as-is, otherwise the JSON serializer would quote it and the
// payload would not match the given content type.
serializedData = DefaultContentTypeConverter.convertEventToBytesForGrpc(data, contentType);
} else {
serializedData = objectSerializer.serialize(data);
}
envelopeBuilder.setDataContentType(contentType);

DaprPubsubProtos.PublishEventRequest.Builder envelopeBuilder = DaprPubsubProtos.PublishEventRequest.newBuilder()
.setTopic(topic)
.setPubsubName(pubsubName)
.setData(ByteString.copyFrom(serializedData))
.setDataContentType(contentType);

Map<String, String> metadata = request.getMetadata();
if (metadata != null) {
Expand Down
93 changes: 92 additions & 1 deletion sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public void publishEventContentTypeOverrideTest() {
return false;
}

if (!"\"hello\"".equals(new String(publishEventRequest.getData().toByteArray()))) {
if (!"hello".equals(new String(publishEventRequest.getData().toByteArray()))) {
return false;
}
return true;
Expand All @@ -254,6 +254,97 @@ public void publishEventContentTypeOverrideTest() {
result.block();
}

@Test
public void publishEventTextContentTypeWithNumberTest() {
doAnswer((Answer<Void>) invocation -> {
StreamObserver<Empty> observer = (StreamObserver<Empty>) invocation.getArguments()[1];
observer.onNext(Empty.getDefaultInstance());
observer.onCompleted();
return null;
}).when(daprStub).publishEvent(ArgumentMatchers.argThat(publishEventRequest ->
"text/plain".equals(publishEventRequest.getDataContentType())
&& "42".equals(new String(publishEventRequest.getData().toByteArray()))), any());

client.publishEvent(new PublishEventRequest("pubsubname", "topic", 42).setContentType("text/plain")).block();
}

@Test
public void publishEventTextContentTypeWithObjectFallsBackToSerializerTest() {
MyObject event = new MyObject(1, "Event");
doAnswer((Answer<Void>) invocation -> {
StreamObserver<Empty> observer = (StreamObserver<Empty>) invocation.getArguments()[1];
observer.onNext(Empty.getDefaultInstance());
observer.onCompleted();
return null;
}).when(daprStub).publishEvent(ArgumentMatchers.argThat(publishEventRequest -> {
String data = new String(publishEventRequest.getData().toByteArray());
return "text/plain".equals(publishEventRequest.getDataContentType())
&& ("{\"id\":1,\"value\":\"Event\"}".equals(data) || "{\"value\":\"Event\",\"id\":1}".equals(data));
}), any());

client.publishEvent(new PublishEventRequest("pubsubname", "topic", event).setContentType("text/plain")).block();
}

@Test
public void publishEventTextContentTypeWithBooleanTest() {
doAnswer((Answer<Void>) invocation -> {
StreamObserver<Empty> observer = (StreamObserver<Empty>) invocation.getArguments()[1];
observer.onNext(Empty.getDefaultInstance());
observer.onCompleted();
return null;
}).when(daprStub).publishEvent(ArgumentMatchers.argThat(publishEventRequest ->
"text/plain".equals(publishEventRequest.getDataContentType())
&& "true".equals(new String(publishEventRequest.getData().toByteArray()))), any());

client.publishEvent(new PublishEventRequest("pubsubname", "topic", true).setContentType("text/plain")).block();
}

@Test
public void publishEventNonTextContentTypeWithStringTest() {
doAnswer((Answer<Void>) invocation -> {
StreamObserver<Empty> observer = (StreamObserver<Empty>) invocation.getArguments()[1];
observer.onNext(Empty.getDefaultInstance());
observer.onCompleted();
return null;
}).when(daprStub).publishEvent(ArgumentMatchers.argThat(publishEventRequest ->
"application/json".equals(publishEventRequest.getDataContentType())
&& "\"hello\"".equals(new String(publishEventRequest.getData().toByteArray()))), any());

client.publishEvent(
new PublishEventRequest("pubsubname", "topic", "hello").setContentType("application/json")).block();
}

@Test
public void publishEventTextContentTypeWithCustomSerializerTest() throws IOException {
DaprObjectSerializer mockSerializer = mock(DaprObjectSerializer.class);
when(mockSerializer.serialize("hello")).thenReturn("custom".getBytes());
client = new DaprClientImpl(channel, daprStub, daprHttp, mockSerializer, new DefaultObjectSerializer());
doAnswer((Answer<Void>) invocation -> {
StreamObserver<Empty> observer = (StreamObserver<Empty>) invocation.getArguments()[1];
observer.onNext(Empty.getDefaultInstance());
observer.onCompleted();
return null;
}).when(daprStub).publishEvent(ArgumentMatchers.argThat(publishEventRequest ->
"text/plain".equals(publishEventRequest.getDataContentType())
&& "custom".equals(new String(publishEventRequest.getData().toByteArray()))), any());

client.publishEvent(new PublishEventRequest("pubsubname", "topic", "hello").setContentType("text/plain")).block();
}

@Test
public void publishEventStringWithoutContentTypeTest() {
doAnswer((Answer<Void>) invocation -> {
StreamObserver<Empty> observer = (StreamObserver<Empty>) invocation.getArguments()[1];
observer.onNext(Empty.getDefaultInstance());
observer.onCompleted();
return null;
}).when(daprStub).publishEvent(ArgumentMatchers.argThat(publishEventRequest ->
"application/json".equals(publishEventRequest.getDataContentType())
&& "\"hello\"".equals(new String(publishEventRequest.getData().toByteArray()))), any());

client.publishEvent("pubsubname", "topic", "hello").block();
}

@Test
public void invokeBindingIllegalArgumentExceptionTest() {
assertThrows(IllegalArgumentException.class, () -> {
Expand Down
Loading