mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
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:
@@ -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"),
|
||||
|
||||
Reference in New Issue
Block a user