Rename decodeOne->decodeToMono and readOne->readMono

This commit is contained in:
Rossen Stoyanchev 2016-07-06 12:38:05 -04:00
parent 6525504e17
commit 54bfbbc607
9 changed files with 35 additions and 51 deletions

View File

@ -55,7 +55,7 @@ public abstract class AbstractDecoder<T> implements Decoder<T> {
}
@Override
public Mono<T> decodeOne(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Object... hints) {
public Mono<T> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Object... hints) {
throw new UnsupportedOperationException();
}
}

View File

@ -72,7 +72,7 @@ public interface Decoder<T> {
* @param hints additional information about how to do decode, optional
* @return the output stream with the decoded element
*/
Mono<T> decodeOne(Publisher<DataBuffer> inputStream, ResolvableType elementType,
Mono<T> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints);
/**

View File

@ -95,7 +95,7 @@ public class StringDecoder extends AbstractDecoder<String> {
}
@Override
public Mono<String> decodeOne(Publisher<DataBuffer> inputStream, ResolvableType elementType,
public Mono<String> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) {
return decodeInternal(Flux.from(inputStream), mimeType).

View File

@ -78,7 +78,7 @@ public class JacksonJsonDecoder extends AbstractDecoder<Object> {
}
@Override
public Mono<Object> decodeOne(Publisher<DataBuffer> inputStream, ResolvableType elementType,
public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
MimeType mimeType, Object... hints) {
JsonObjectDecoder objectDecoder = this.monoObjectDecoder;

View File

@ -123,12 +123,12 @@ public class CodecHttpMessageConverter<T> implements HttpMessageConverter<T> {
}
@Override
public Mono<T> readOne(ResolvableType type, ReactiveHttpInputMessage inputMessage) {
public Mono<T> readMono(ResolvableType type, ReactiveHttpInputMessage inputMessage) {
if (this.decoder == null) {
return Mono.error(new IllegalStateException("No decoder set"));
}
MediaType contentType = getContentType(inputMessage);
return this.decoder.decodeOne(inputMessage.getBody(), type, contentType);
return this.decoder.decodeToMono(inputMessage.getBody(), type, contentType);
}
private MediaType getContentType(ReactiveHttpInputMessage inputMessage) {

View File

@ -69,7 +69,7 @@ public interface HttpMessageConverter<T> {
* @param inputMessage the HTTP input message to read from
* @return the converted {@link Mono} of object
*/
Mono<T> readOne(ResolvableType type, ReactiveHttpInputMessage inputMessage);
Mono<T> readMono(ResolvableType type, ReactiveHttpInputMessage inputMessage);
/**
* Indicates whether the given class can be written by this converter.

View File

@ -151,14 +151,16 @@ public class RequestBodyArgumentResolver implements HandlerMethodArgumentResolve
return Mono.just(getConversionService().convert(flux, FLUX_TYPE, typeDescriptor));
}
else {
Mono<?> mono = converter.readOne(elementType, request);
Mono<?> mono = converter.readMono(elementType, request);
if (this.validator != null) {
mono = mono.map(applyValidationIfApplicable(parameter));
}
if (!convertFromMono) {
return mono.map(value-> value); // TODO: MonoToObjectConverter
if (convertFromMono) {
return Mono.just(getConversionService().convert(mono, MONO_TYPE, typeDescriptor));
}
else {
return Mono.from(mono);
}
return Mono.just(getConversionService().convert(mono, MONO_TYPE, typeDescriptor));
}
}
}

View File

@ -16,7 +16,6 @@
package org.springframework.core.codec;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@ -37,37 +36,25 @@ import static org.junit.Assert.assertTrue;
*/
public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
private StringDecoder decoder;
@Before
public void createEncoder() {
this.decoder = new StringDecoder();
}
private StringDecoder decoder = new StringDecoder();
@Test
public void canDecode() {
assertTrue(this.decoder
.canDecode(ResolvableType.forClass(String.class), MediaType.TEXT_PLAIN));
assertTrue(this.decoder
.canDecode(ResolvableType.forClass(String.class), MediaType.TEXT_HTML));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class),
MediaType.APPLICATION_JSON));
assertFalse(this.decoder
.canDecode(ResolvableType.forClass(Integer.class), MediaType.TEXT_PLAIN));
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Object.class),
MediaType.APPLICATION_JSON));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MediaType.TEXT_PLAIN));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MediaType.TEXT_HTML));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(String.class), MediaType.APPLICATION_JSON));
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Integer.class), MediaType.TEXT_PLAIN));
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Object.class), MediaType.APPLICATION_JSON));
}
@Test
public void decode() throws InterruptedException {
this.decoder = new StringDecoder(false);
Flux<DataBuffer> source =
Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz"));
Flux<String> output =
this.decoder.decode(source, ResolvableType.forClass(String.class), null);
TestSubscriber
.subscribe(output)
Flux<DataBuffer> source = Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz"));
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class), null);
TestSubscriber.subscribe(output)
.assertNoError()
.assertComplete()
.assertValues("foo", "bar", "baz");
@ -78,10 +65,9 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
DataBuffer fooBar = stringBuffer("\nfoo\r\nbar\r");
DataBuffer baz = stringBuffer("\nbaz");
Flux<DataBuffer> source = Flux.just(fooBar, baz);
Flux<String> output =
decoder.decode(source, ResolvableType.forClass(String.class), null);
TestSubscriber
.subscribe(output)
Flux<String> output = decoder.decode(source, ResolvableType.forClass(String.class), null);
TestSubscriber.subscribe(output)
.assertNoError()
.assertComplete().assertValues("\n", "foo\r", "\n", "bar\r", "\n", "baz");
}
@ -89,22 +75,18 @@ public class StringDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test
public void decodeEmpty() throws InterruptedException {
Flux<DataBuffer> source = Flux.just(stringBuffer(""));
Flux<String> output =
this.decoder.decode(source, ResolvableType.forClass(String.class), null);
TestSubscriber
.subscribe(output)
.assertValues("");
Flux<String> output = this.decoder.decode(source, ResolvableType.forClass(String.class), null);
TestSubscriber.subscribe(output).assertValues("");
}
@Test
public void decodeOne() throws InterruptedException {
public void decodeToMono() 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)
Flux<DataBuffer> source = Flux.just(stringBuffer("foo"), stringBuffer("bar"), stringBuffer("baz"));
Mono<String> output = this.decoder.decodeToMono(source, ResolvableType.forClass(String.class), null);
TestSubscriber.subscribe(output)
.assertNoError()
.assertComplete()
.assertValues("foobarbaz");

View File

@ -66,7 +66,7 @@ public class JacksonJsonDecoderTests extends AbstractDataBufferAllocatingTestCas
Method method = getClass().getDeclaredMethod("handle", List.class);
ResolvableType elementType = ResolvableType.forMethodParameter(method, 0);
Mono<Object> mono = new JacksonJsonDecoder().decodeOne(source, elementType, null);
Mono<Object> mono = new JacksonJsonDecoder().decodeToMono(source, elementType, null);
TestSubscriber.subscribe(mono).assertNoError().assertComplete().
assertValues(Arrays.asList(new Pojo("f1", "b1"), new Pojo("f2", "b2")));