Skip to content

Commit 9a7a560

Browse files
committed
Properly handle SSE comments in RESTEasy Reactive client and server code
Fixes: #30169
1 parent 3e7eb23 commit 9a7a560

File tree

5 files changed

+24
-12
lines changed
  • extensions/resteasy-reactive
  • independent-projects/resteasy-reactive
    • client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl
    • server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core

5 files changed

+24
-12
lines changed

extensions/resteasy-reactive/quarkus-resteasy-reactive-jsonb/deployment/src/test/java/io/quarkus/resteasy/reactive/jsonb/deployment/test/sse/SseParserTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public void testParser() {
4343

4444
// all fields
4545
testParser("data:DATA\nid:ID\n:COMMENT\nretry:23\nevent:NAME\n\n", "DATA", "COMMENT", "ID", "NAME", 23);
46-
// all fields and no data: no event
47-
testParser("id:ID\n:COMMENT\nretry:23\nevent:NAME\n\n", null, null, null, null, SseEvent.RECONNECT_NOT_SET);
46+
// all fields and no data
47+
testParser("id:ID\n:COMMENT\nretry:23\nevent:NAME\n\n", null, "COMMENT", "ID", "NAME", 23);
4848

4949
// optional space after colon
5050
testParser("data:foo\n\n", "foo", null, null, null, SseEvent.RECONNECT_NOT_SET);
@@ -147,6 +147,13 @@ private void testParser(String event, String data, String comment, String lastId
147147
.setId(lastId)
148148
.setName(name)
149149
.setReconnectDelay(reconnectDelay)));
150+
} else if (comment != null) {
151+
testParser(Collections.singletonList(event), Collections.singletonList(new InboundSseEventImpl(null, null)
152+
.setData(null)
153+
.setComment(comment)
154+
.setId(lastId)
155+
.setName(name)
156+
.setReconnectDelay(reconnectDelay)));
150157
} else {
151158
testParser(Collections.singletonList(event), Collections.emptyList());
152159
}

extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/stream/StreamResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ public Multi<String> sseThrows() {
160160
@GET
161161
@Produces(MediaType.SERVER_SENT_EVENTS)
162162
public Multi<OutboundSseEvent> sseRaw(@Context Sse sse) {
163-
return Multi.createFrom().items(sse.newEventBuilder().id("one").data("uno").name("eins").build(),
163+
return Multi.createFrom().items(sse.newEventBuilder().comment("dummy").build(),
164+
sse.newEventBuilder().id("one").data("uno").name("eins").build(),
164165
sse.newEventBuilder().id("two").data("dos").name("zwei").build(),
165166
sse.newEventBuilder().id("three").data("tres").name("drei").build());
166167
}

extensions/resteasy-reactive/quarkus-resteasy-reactive/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/stream/StreamTestCase.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.net.URI;
44
import java.nio.charset.StandardCharsets;
55
import java.time.Duration;
6-
import java.util.Arrays;
76
import java.util.List;
87
import java.util.concurrent.CompletableFuture;
98
import java.util.concurrent.CopyOnWriteArrayList;
@@ -211,7 +210,7 @@ public void testSse() throws InterruptedException {
211210
});
212211
sse.open();
213212
Assertions.assertTrue(latch.await(20, TimeUnit.SECONDS));
214-
Assertions.assertEquals(Arrays.asList("a", "b", "c"), results);
213+
org.assertj.core.api.Assertions.assertThat(results).containsExactly("a", "b", "c");
215214
Assertions.assertEquals(0, errors.size());
216215
}
217216
}
@@ -248,7 +247,9 @@ public void testSseForMultiWithOutboundSseEvent() throws InterruptedException {
248247
List<String> results = new CopyOnWriteArrayList<>();
249248
List<String> ids = new CopyOnWriteArrayList<>();
250249
List<String> names = new CopyOnWriteArrayList<>();
250+
List<String> comments = new CopyOnWriteArrayList<>();
251251
sse.register(event -> {
252+
comments.add(event.getComment());
252253
results.add(event.readData());
253254
ids.add(event.getId());
254255
names.add(event.getName());
@@ -259,9 +260,10 @@ public void testSseForMultiWithOutboundSseEvent() throws InterruptedException {
259260
});
260261
sse.open();
261262
Assertions.assertTrue(latch.await(20, TimeUnit.SECONDS));
262-
Assertions.assertEquals(Arrays.asList("uno", "dos", "tres"), results);
263-
Assertions.assertEquals(Arrays.asList("one", "two", "three"), ids);
264-
Assertions.assertEquals(Arrays.asList("eins", "zwei", "drei"), names);
263+
org.assertj.core.api.Assertions.assertThat(results).containsExactly(null, "uno", "dos", "tres");
264+
org.assertj.core.api.Assertions.assertThat(ids).containsExactly(null, "one", "two", "three");
265+
org.assertj.core.api.Assertions.assertThat(names).containsExactly(null, "eins", "zwei", "drei");
266+
org.assertj.core.api.Assertions.assertThat(comments).containsExactly("dummy", null, null, null);
265267
Assertions.assertEquals(0, errors.size());
266268
}
267269
}

independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/SseParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private void parseEvent() {
145145

146146
private void dispatchEvent() {
147147
// ignore empty events
148-
if (dataBuffer.length() == 0)
148+
if (dataBuffer.length() == 0 && commentBuffer.length() == 0)
149149
return;
150150
WebTargetImpl webTarget = sseEventSource.getWebTarget();
151151
InboundSseEventImpl event;
@@ -159,7 +159,7 @@ private void dispatchEvent() {
159159
event.setComment(commentBuffer.length() == 0 ? null : commentBuffer.toString());
160160
// SSE spec says empty string is the default, but JAX-RS says null if not specified
161161
event.setId(lastEventId);
162-
event.setData(dataBuffer.toString());
162+
event.setData(dataBuffer.length() == 0 ? null : dataBuffer.toString());
163163
// SSE spec says "message" is the default, but JAX-RS says null if not specified
164164
event.setName(eventType);
165165
event.setReconnectDelay(eventReconnectTime);

independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/SseUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ private static String serialiseEvent(ResteasyReactiveRequestContext context, Out
6969
if (event.getReconnectDelay() >= 0)
7070
serialiseField(context, sb, "retry", Long.toString(event.getReconnectDelay()), false);
7171
}
72-
String data = serialiseDataToString(context, event, eventMediaType);
73-
serialiseField(context, sb, "data", data, true);
72+
if (event.getData() != null) {
73+
String data = serialiseDataToString(context, event, eventMediaType);
74+
serialiseField(context, sb, "data", data, true);
75+
}
7476
sb.append(NL);
7577
// return a UTF8 buffer
7678
return sb.toString();

0 commit comments

Comments
 (0)