Fix bug in response when JSON is empty
This commit is contained in:
@@ -56,7 +56,8 @@ class ResponseBodyEmitterSubscriber<T> implements Subscriber<T>, Runnable {
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription subscription) {
|
||||
if (!MediaType.ALL.equals(mediaType) && MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
|
||||
if (!MediaType.ALL.equals(mediaType)
|
||||
&& MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
|
||||
try {
|
||||
this.responseBodyEmitter.send("[");
|
||||
}
|
||||
@@ -70,18 +71,20 @@ class ResponseBodyEmitterSubscriber<T> implements Subscriber<T>, Runnable {
|
||||
|
||||
@Override
|
||||
public void onNext(T value) {
|
||||
|
||||
|
||||
Object object = value;
|
||||
|
||||
try {
|
||||
if (!MediaType.ALL.equals(mediaType) && MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
|
||||
if (!MediaType.ALL.equals(mediaType)
|
||||
&& MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
|
||||
if (!this.firstElementWritten) {
|
||||
this.firstElementWritten = true;
|
||||
}
|
||||
else {
|
||||
responseBodyEmitter.send(",");
|
||||
}
|
||||
if (value.getClass()==String.class && !((String)value).contains("\"")) {
|
||||
if (value.getClass() == String.class
|
||||
&& !((String) value).contains("\"")) {
|
||||
object = "\"" + value + "\"";
|
||||
}
|
||||
}
|
||||
@@ -106,14 +109,12 @@ class ResponseBodyEmitterSubscriber<T> implements Subscriber<T>, Runnable {
|
||||
if (!completed) {
|
||||
completed = true;
|
||||
try {
|
||||
if (!MediaType.ALL.equals(mediaType) && MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
|
||||
if (!MediaType.ALL.equals(mediaType)
|
||||
&& MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
|
||||
if (!this.firstElementWritten) {
|
||||
|
||||
this.firstElementWritten = true;
|
||||
}
|
||||
else {
|
||||
responseBodyEmitter.send("]");
|
||||
}
|
||||
responseBodyEmitter.send("]");
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.cloud.function.web;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -80,6 +81,16 @@ public class RestApplicationTests {
|
||||
String.class).getBody()).isEqualTo("foobar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyJson() throws Exception {
|
||||
assertThat(rest
|
||||
.exchange(
|
||||
RequestEntity.get(new URI("http://localhost:" + port + "/empty"))
|
||||
.accept(MediaType.APPLICATION_JSON).build(),
|
||||
String.class)
|
||||
.getBody()).isEqualTo("[]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sentences() throws Exception {
|
||||
assertThat(rest.exchange(RequestEntity
|
||||
@@ -104,7 +115,8 @@ public class RestApplicationTests {
|
||||
.accept(MediaType.APPLICATION_JSON).build(),
|
||||
String.class);
|
||||
assertThat(result.getBody()).isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
|
||||
assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
|
||||
assertThat(result.getHeaders().getContentType())
|
||||
.isEqualTo(MediaType.APPLICATION_JSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -114,8 +126,9 @@ public class RestApplicationTests {
|
||||
.accept(EVENT_STREAM).build(),
|
||||
String.class);
|
||||
assertThat(result.getBody())
|
||||
.isEqualTo(sse("[\"go\",\"home\"]", "[\"come\",\"back\"]"));
|
||||
assertThat(result.getHeaders().getContentType().isCompatibleWith(EVENT_STREAM)).isTrue();
|
||||
.isEqualTo(sse("[\"go\",\"home\"]", "[\"come\",\"back\"]"));
|
||||
assertThat(result.getHeaders().getContentType().isCompatibleWith(EVENT_STREAM))
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -181,6 +194,11 @@ public class RestApplicationTests {
|
||||
return () -> Flux.fromArray(new String[] { "foo", "bar" });
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Supplier<Flux<String>> empty() {
|
||||
return () -> Flux.fromIterable(Collections.emptyList());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Supplier<Flux<List<String>>> sentences() {
|
||||
return () -> Flux.just(Arrays.asList("go", "home"),
|
||||
|
||||
Reference in New Issue
Block a user