Add Decoder#decodeOne()

This commit adds a Decoder#decodeOne() method in order
to handle correctly the streaming versus one value
deserialization based on the type provided by the user.

For example, if a List parameter is provided in a controller
method, Jackson will be called once, while if the user provides
a Flux or an Observable parameter, Jackson will be called for
each element.
This commit is contained in:
Sebastien Deleuze
2016-07-01 17:01:29 +02:00
committed by Rossen Stoyanchev
parent 12d7b78169
commit 917a2fb9d0
9 changed files with 147 additions and 29 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.core.codec;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.test.TestSubscriber;
import org.springframework.core.ResolvableType;
@@ -96,4 +97,18 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
.assertValues("");
}
@Test
public void decodeOne() throws InterruptedException {
this.decoder = new StringDecoder(false);
Flux<DataBuffer> source =
Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz"));
Mono<String> output =
this.decoder.decodeOne(source, ResolvableType.forClass(String.class), null);
TestSubscriber
.subscribe(output)
.assertNoError()
.assertComplete()
.assertValues("foobarbaz");
}
}

View File

@@ -20,9 +20,9 @@ import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.test.TestSubscriber;
import org.springframework.core.ResolvableType;
@@ -30,7 +30,6 @@ import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.MediaType;
import org.springframework.http.codec.Pojo;
import org.springframework.http.codec.json.JacksonJsonDecoder;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -61,16 +60,15 @@ public class JacksonJsonDecoderTests extends AbstractDataBufferAllocatingTestCas
}
@Test
@Ignore // Issue 109
public void decodeToList() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer(
"[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"));
Method method = getClass().getDeclaredMethod("handle", List.class);
ResolvableType elementType = ResolvableType.forMethodParameter(method, 0);
Flux<Object> flux = new JacksonJsonDecoder().decode(source, elementType, null);
Mono<Object> mono = new JacksonJsonDecoder().decodeOne(source, elementType, null);
TestSubscriber.subscribe(flux).assertNoError().assertComplete().
TestSubscriber.subscribe(mono).assertNoError().assertComplete().
assertValues(Arrays.asList(new Pojo("f1", "b1"), new Pojo("f2", "b2")));
}