Skip to content

Commit 08e87b7

Browse files
committed
Fix JsonType.streamAsLines() for empty Stream
When the stream is empty, we still need to send some response body when used with an http server. The fix for this is to always append a blank empty line to the content. Client consumers are expected to ignore empty content (filter empty lines).
1 parent e4137d5 commit 08e87b7

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

blackbox-test/src/test/java/org/example/customer/stream/StreamBasicTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void streamAsLines_toJsonFromJson() {
2727
JsonType<Stream<MyBasic>> streamType = type.streamAsLines();
2828

2929
String asJson = streamType.toJson(basics.stream());
30-
String expected = "{\"id\":1,\"name\":\"a\"}\n{\"id\":2,\"name\":\"b\"}\n{\"id\":3,\"name\":\"c\"}\n";
30+
String expected = "{\"id\":1,\"name\":\"a\"}\n{\"id\":2,\"name\":\"b\"}\n{\"id\":3,\"name\":\"c\"}\n\n";
3131
assertThat(asJson)
3232
.describedAs("expect new line delimited json content")
3333
.isEqualTo(expected);

jsonb/src/main/java/io/avaje/jsonb/core/StreamAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public void toJson(JsonWriter writer, Stream<T> stream) {
3030
elementAdapter.toJson(writer, bean);
3131
writer.writeNewLine();
3232
});
33+
writer.writeNewLine();
3334
} else {
3435
writer.beginArray();
3536
stream.forEach(bean -> elementAdapter.toJson(writer, bean));

0 commit comments

Comments
 (0)