Skip to content

Commit 34c69bf

Browse files
committed
Allow empty comment in ServerResponse.SseBuilder
Closes gh-34608
1 parent 37d7af4 commit 34c69bf

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/function/SseServerResponse.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -173,7 +173,6 @@ public SseBuilder retry(Duration duration) {
173173

174174
@Override
175175
public SseBuilder comment(String comment) {
176-
Assert.hasLength(comment, "Comment must not be empty");
177176
String[] lines = comment.split("\n");
178177
for (String line : lines) {
179178
field("", line);

Diff for: spring-webmvc/src/test/java/org/springframework/web/servlet/function/SseServerResponseTests.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -173,6 +173,26 @@ void sendWithoutData() throws Exception {
173173
assertThat(this.mockResponse.getContentAsString()).isEqualTo(expected);
174174
}
175175

176+
@Test // gh-34608
177+
void sendHeartbeat() throws Exception {
178+
ServerResponse response = ServerResponse.sse(sse -> {
179+
try {
180+
sse.comment("").send();
181+
}
182+
catch (IOException ex) {
183+
throw new UncheckedIOException(ex);
184+
}
185+
});
186+
187+
ServerResponse.Context context = Collections::emptyList;
188+
189+
ModelAndView mav = response.writeTo(this.mockRequest, this.mockResponse, context);
190+
assertThat(mav).isNull();
191+
192+
String expected = ":\n\n";
193+
assertThat(this.mockResponse.getContentAsString()).isEqualTo(expected);
194+
}
195+
176196

177197
private static final class Person {
178198

0 commit comments

Comments
 (0)