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

OutboundSseEvent is not correctly serialized #29623

Merged
merged 1 commit into from
Dec 3, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.sse.OutboundSseEvent;
import javax.ws.rs.sse.Sse;

import org.jboss.resteasy.reactive.common.util.MultiCollectors;
import org.reactivestreams.Publisher;
Expand Down Expand Up @@ -152,4 +155,13 @@ public Multi<String> sse() {
public Multi<String> sseThrows() {
throw new IllegalStateException("STOP");
}

@Path("sse/raw")
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public Multi<OutboundSseEvent> sseRaw(@Context Sse sse) {
return Multi.createFrom().items(sse.newEventBuilder().id("one").data("uno").name("eins").build(),
sse.newEventBuilder().id("two").data("dos").name("zwei").build(),
sse.newEventBuilder().id("three").data("tres").name("drei").build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,32 @@ public void testSseThrows() throws InterruptedException {
Assertions.assertEquals(1, errors.size());
}
}

@Test
public void testSseForMultiWithOutboundSseEvent() throws InterruptedException {
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target(this.uri.toString() + "stream/sse/raw");
try (SseEventSource sse = SseEventSource.target(target).build()) {
CountDownLatch latch = new CountDownLatch(1);
List<Throwable> errors = new CopyOnWriteArrayList<>();
List<String> results = new CopyOnWriteArrayList<>();
List<String> ids = new CopyOnWriteArrayList<>();
List<String> names = new CopyOnWriteArrayList<>();
sse.register(event -> {
results.add(event.readData());
ids.add(event.getId());
names.add(event.getName());
}, error -> {
errors.add(error);
}, () -> {
latch.countDown();
});
sse.open();
Assertions.assertTrue(latch.await(20, TimeUnit.SECONDS));
Assertions.assertEquals(Arrays.asList("uno", "dos", "tres"), results);
Assertions.assertEquals(Arrays.asList("one", "two", "three"), ids);
Assertions.assertEquals(Arrays.asList("eins", "zwei", "drei"), names);
Assertions.assertEquals(0, errors.size());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.function.Consumer;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.sse.OutboundSseEvent;

import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.common.util.RestMediaType;
Expand Down Expand Up @@ -49,7 +50,12 @@ private static class SseMultiSubscriber extends AbstractMultiSubscriber {

@Override
public void onNext(Object item) {
OutboundSseEventImpl event = new OutboundSseEventImpl.BuilderImpl().data(item).build();
OutboundSseEvent event;
if (item instanceof OutboundSseEvent) {
event = (OutboundSseEvent) item;
} else {
event = new OutboundSseEventImpl.BuilderImpl().data(item).build();
}
SseUtil.send(requestContext, event, customizers).whenComplete(new BiConsumer<Object, Throwable>() {
@Override
public void accept(Object v, Throwable t) {
Expand Down