Allow null id/event in ServerSentEvent

The builder itself does not allow `null` values so this shouldn't be
necessary, but because the offending change was introduced late in the
6.2.x line, we'll be more flexible here.

Fixes gh-36634
This commit is contained in:
Brian Clozel
2026-04-10 14:43:29 +02:00
parent 438b1011fe
commit cbdec804a5
2 changed files with 18 additions and 2 deletions
@@ -267,8 +267,10 @@ public final class ServerSentEvent<T> {
}
private static void checkEvent(String content) {
Assert.isTrue(content.indexOf('\n') == -1 && content.indexOf('\r') == -1,
"illegal character '\\n' or '\\r' in event content");
if (content != null) {
Assert.isTrue(content.indexOf('\n') == -1 && content.indexOf('\r') == -1,
"illegal character '\\n' or '\\r' in event content");
}
}
@Override
@@ -18,10 +18,12 @@ package org.springframework.http.codec;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
@@ -44,6 +46,18 @@ class ServerSentEventTests {
ServerSentEvent.<String>builder().event("first" + newLine + "second").build());
}
@Test // gh-36634
void allowsNullId() {
ServerSentEvent<String> event = ServerSentEvent.<String>builder().id(null).event("first").build();
assertThat(event.format()).contains("event:first").doesNotContain("id");
}
@Test // gh-36634
void allowsNullEvent() {
ServerSentEvent<String> event = ServerSentEvent.<String>builder().id("42").event(null).build();
assertThat(event.format()).contains("id:42").doesNotContain("event");
}
private static Stream<Arguments> newLineCharacters() {
return Stream.of(
Arguments.of("\n", "LF"),