Skip to content

Commit cbdec80

Browse files
committed
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
1 parent 438b101 commit cbdec80

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

spring-web/src/main/java/org/springframework/http/codec/ServerSentEvent.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,10 @@ public Builder<T> event(String event) {
267267
}
268268

269269
private static void checkEvent(String content) {
270-
Assert.isTrue(content.indexOf('\n') == -1 && content.indexOf('\r') == -1,
271-
"illegal character '\\n' or '\\r' in event content");
270+
if (content != null) {
271+
Assert.isTrue(content.indexOf('\n') == -1 && content.indexOf('\r') == -1,
272+
"illegal character '\\n' or '\\r' in event content");
273+
}
272274
}
273275

274276
@Override

spring-web/src/test/java/org/springframework/http/codec/ServerSentEventTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import java.util.stream.Stream;
2020

21+
import org.junit.jupiter.api.Test;
2122
import org.junit.jupiter.params.ParameterizedTest;
2223
import org.junit.jupiter.params.provider.Arguments;
2324
import org.junit.jupiter.params.provider.MethodSource;
2425

26+
import static org.assertj.core.api.Assertions.assertThat;
2527
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2628

2729
/**
@@ -44,6 +46,18 @@ void rejectsInvalidEvent(String newLine, String description) {
4446
ServerSentEvent.<String>builder().event("first" + newLine + "second").build());
4547
}
4648

49+
@Test // gh-36634
50+
void allowsNullId() {
51+
ServerSentEvent<String> event = ServerSentEvent.<String>builder().id(null).event("first").build();
52+
assertThat(event.format()).contains("event:first").doesNotContain("id");
53+
}
54+
55+
@Test // gh-36634
56+
void allowsNullEvent() {
57+
ServerSentEvent<String> event = ServerSentEvent.<String>builder().id("42").event(null).build();
58+
assertThat(event.format()).contains("id:42").doesNotContain("event");
59+
}
60+
4761
private static Stream<Arguments> newLineCharacters() {
4862
return Stream.of(
4963
Arguments.of("\n", "LF"),

0 commit comments

Comments
 (0)