Update Codec API to use Map<String, Object> for hints

Issue: SPR-14557
This commit is contained in:
Sebastien Deleuze
2016-09-12 11:08:01 +02:00
parent b88ed85994
commit b91867cf45
32 changed files with 212 additions and 173 deletions

View File

@@ -18,7 +18,9 @@ package org.springframework.http.codec.json;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonView;
import org.junit.Test;
@@ -46,15 +48,16 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
public void canDecode() {
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
assertTrue(decoder.canDecode(null, MediaType.APPLICATION_JSON));
assertFalse(decoder.canDecode(null, MediaType.APPLICATION_XML));
assertTrue(decoder.canDecode(null, MediaType.APPLICATION_JSON, Collections.emptyMap()));
assertFalse(decoder.canDecode(null, MediaType.APPLICATION_XML, Collections.emptyMap()));
}
@Test
public void decodePojo() {
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}"));
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null);
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null,
Collections.emptyMap());
TestSubscriber.subscribe(flux).assertNoError().assertComplete().
assertValues(new Pojo("foofoo", "barbar"));
@@ -67,7 +70,8 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
Method method = getClass().getDeclaredMethod("handle", List.class);
ResolvableType elementType = ResolvableType.forMethodParameter(method, 0);
Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType, null);
Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType,
null, Collections.emptyMap());
TestSubscriber.subscribe(mono).assertNoError().assertComplete().
assertValues(Arrays.asList(new Pojo("f1", "b1"), new Pojo("f2", "b2")));
@@ -79,7 +83,8 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
"[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"));
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null);
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null,
Collections.emptyMap());
TestSubscriber.subscribe(flux).assertNoError().assertComplete().
assertValues(new Pojo("f1", "b1"), new Pojo("f2", "b2"));
@@ -89,10 +94,10 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
public void jsonView() throws Exception {
Flux<DataBuffer> source = Flux.just(
stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
ResolvableType elementType = ResolvableType
.forMethodParameter(JacksonController.class.getMethod("foo", JacksonViewBean.class), 0);
ResolvableType elementType = ResolvableType.forClass(JacksonViewBean.class);
Map<String, Object> hints = Collections.singletonMap(Jackson2JsonDecoder.JSON_VIEW_HINT, MyJacksonView1.class);
Flux<JacksonViewBean> flux = new Jackson2JsonDecoder()
.decode(source, elementType, null).cast(JacksonViewBean.class);
.decode(source, elementType, null, hints).cast(JacksonViewBean.class);
TestSubscriber
.subscribe(flux)
@@ -109,7 +114,8 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
public void decodeEmptyBodyToMono() throws Exception {
Flux<DataBuffer> source = Flux.empty();
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
Mono<Object> flux = new Jackson2JsonDecoder().decodeToMono(source, elementType, null);
Mono<Object> flux = new Jackson2JsonDecoder().decodeToMono(source, elementType,
null, Collections.emptyMap());
TestSubscriber.subscribe(flux)
.assertNoError()
@@ -163,12 +169,4 @@ public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCa
}
}
private static class JacksonController {
public JacksonViewBean foo(@JsonView(MyJacksonView1.class) JacksonViewBean bean) {
return bean;
}
}
}

View File

@@ -16,6 +16,9 @@
package org.springframework.http.codec.json;
import java.util.Collections;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonView;
@@ -42,8 +45,8 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
@Test
public void canEncode() {
assertTrue(this.encoder.canEncode(null, MediaType.APPLICATION_JSON));
assertFalse(this.encoder.canEncode(null, MediaType.APPLICATION_XML));
assertTrue(this.encoder.canEncode(null, MediaType.APPLICATION_JSON, Collections.emptyMap()));
assertFalse(this.encoder.canEncode(null, MediaType.APPLICATION_XML, Collections.emptyMap()));
}
@Test
@@ -54,7 +57,7 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
new Pojo("foofoofoo", "barbarbar")
);
ResolvableType type = ResolvableType.forClass(Pojo.class);
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null);
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, Collections.emptyMap());
TestSubscriber.subscribe(output)
.assertComplete()
@@ -74,7 +77,7 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
public void encodeWithType() {
Flux<ParentClass> source = Flux.just(new Foo(), new Bar());
ResolvableType type = ResolvableType.forClass(ParentClass.class);
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null);
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, Collections.emptyMap());
TestSubscriber.subscribe(output)
.assertComplete()
@@ -93,8 +96,9 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
bean.setWithView2("with");
bean.setWithoutView("without");
ResolvableType type = ResolvableType.forMethodReturnType(JacksonController.class.getMethod("foo"));
Flux<DataBuffer> output = this.encoder.encode(Mono.just(bean), this.bufferFactory, type, null);
ResolvableType type = ResolvableType.forClass(JacksonViewBean.class);
Map<String, Object> hints = Collections.singletonMap(Jackson2JsonEncoder.JSON_VIEW_HINT, MyJacksonView1.class);
Flux<DataBuffer> output = this.encoder.encode(Mono.just(bean), this.bufferFactory, type, null, hints);
TestSubscriber.subscribe(output)
.assertComplete()
@@ -157,13 +161,4 @@ public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCa
}
}
private static class JacksonController {
@JsonView(MyJacksonView1.class)
public JacksonViewBean foo() {
return null;
}
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.http.codec.json;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.junit.Test;
import reactor.core.publisher.Flux;
@@ -36,7 +37,7 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase
Flux<DataBuffer> source =
Flux.just(stringBuffer("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}"));
Flux<String> output =
decoder.decode(source, null, null).map(JsonObjectDecoderTests::toString);
decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString);
TestSubscriber
.subscribe(output)
.assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}");
@@ -48,7 +49,7 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\": \"foofoo\""),
stringBuffer(", \"bar\": \"barbar\"}"));
Flux<String> output =
decoder.decode(source, null, null).map(JsonObjectDecoderTests::toString);
decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString);
TestSubscriber
.subscribe(output)
.assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}");
@@ -60,7 +61,7 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase
Flux<DataBuffer> source = Flux.just(stringBuffer(
"[{\"foo\": \"foofoo\", \"bar\": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]"));
Flux<String> output =
decoder.decode(source, null, null).map(JsonObjectDecoderTests::toString);
decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString);
TestSubscriber
.subscribe(output)
.assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}",
@@ -74,7 +75,7 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase
Flux.just(stringBuffer("[{\"foo\": \"foofoo\", \"bar\""), stringBuffer(
": \"barbar\"},{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}]"));
Flux<String> output =
decoder.decode(source, null, null).map(JsonObjectDecoderTests::toString);
decoder.decode(source, null, null, Collections.emptyMap()).map(JsonObjectDecoderTests::toString);
TestSubscriber
.subscribe(output)
.assertValues("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}",

View File

@@ -16,6 +16,7 @@
package org.springframework.http.codec.xml;
import java.util.Collections;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.stream.events.XMLEvent;
@@ -71,23 +72,23 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test
public void canDecode() {
assertTrue(this.decoder.canDecode(ResolvableType.forClass(Pojo.class),
MediaType.APPLICATION_XML));
MediaType.APPLICATION_XML, Collections.emptyMap()));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(Pojo.class),
MediaType.TEXT_XML));
MediaType.TEXT_XML, Collections.emptyMap()));
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Pojo.class),
MediaType.APPLICATION_JSON));
MediaType.APPLICATION_JSON, Collections.emptyMap()));
assertTrue(this.decoder.canDecode(ResolvableType.forClass(TypePojo.class),
MediaType.APPLICATION_XML));
MediaType.APPLICATION_XML, Collections.emptyMap()));
assertFalse(this.decoder.canDecode(ResolvableType.forClass(getClass()),
MediaType.APPLICATION_XML));
MediaType.APPLICATION_XML, Collections.emptyMap()));
}
@Test
public void splitOneBranches() {
Flux<XMLEvent> xmlEvents = this.xmlEventDecoder
.decode(Flux.just(stringBuffer(POJO_ROOT)), null, null);
.decode(Flux.just(stringBuffer(POJO_ROOT)), null, null, Collections.emptyMap());
Flux<List<XMLEvent>> result = this.decoder.split(xmlEvents, new QName("pojo"));
TestSubscriber
@@ -112,7 +113,7 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test
public void splitMultipleBranches() {
Flux<XMLEvent> xmlEvents = this.xmlEventDecoder
.decode(Flux.just(stringBuffer(POJO_CHILD)), null, null);
.decode(Flux.just(stringBuffer(POJO_CHILD)), null, null, Collections.emptyMap());
Flux<List<XMLEvent>> result = this.decoder.split(xmlEvents, new QName("pojo"));
TestSubscriber
@@ -160,8 +161,8 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test
public void decodeSingleXmlRootElement() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_ROOT));
Flux<Object> output =
this.decoder.decode(source, ResolvableType.forClass(Pojo.class), null);
Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(Pojo.class),
null, Collections.emptyMap());
TestSubscriber
.subscribe(output)
@@ -173,8 +174,8 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test
public void decodeSingleXmlTypeElement() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_ROOT));
Flux<Object> output = this.decoder
.decode(source, ResolvableType.forClass(TypePojo.class), null);
Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(TypePojo.class),
null, Collections.emptyMap());
TestSubscriber
.subscribe(output)
@@ -186,8 +187,8 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test
public void decodeMultipleXmlRootElement() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_CHILD));
Flux<Object> output =
this.decoder.decode(source, ResolvableType.forClass(Pojo.class), null);
Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(Pojo.class),
null, Collections.emptyMap());
TestSubscriber
.subscribe(output)
@@ -199,8 +200,8 @@ public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
@Test
public void decodeMultipleXmlTypeElement() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_CHILD));
Flux<Object> output = this.decoder
.decode(source, ResolvableType.forClass(TypePojo.class), null);
Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(TypePojo.class),
null, Collections.emptyMap());
TestSubscriber
.subscribe(output)

View File

@@ -17,6 +17,7 @@
package org.springframework.http.codec.xml;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.junit.Test;
import reactor.core.publisher.Flux;
@@ -45,18 +46,18 @@ public class Jaxb2XmlEncoderTests extends AbstractDataBufferAllocatingTestCase {
@Test
public void canEncode() {
assertTrue(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
MediaType.APPLICATION_XML));
MediaType.APPLICATION_XML, Collections.emptyMap()));
assertTrue(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
MediaType.TEXT_XML));
MediaType.TEXT_XML, Collections.emptyMap()));
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Pojo.class),
MediaType.APPLICATION_JSON));
MediaType.APPLICATION_JSON, Collections.emptyMap()));
assertTrue(this.encoder.canEncode(
ResolvableType.forClass(Jaxb2XmlDecoderTests.TypePojo.class),
MediaType.APPLICATION_XML));
MediaType.APPLICATION_XML, Collections.emptyMap()));
assertFalse(this.encoder.canEncode(ResolvableType.forClass(getClass()),
MediaType.APPLICATION_XML));
MediaType.APPLICATION_XML, Collections.emptyMap()));
}
@Test
@@ -64,7 +65,7 @@ public class Jaxb2XmlEncoderTests extends AbstractDataBufferAllocatingTestCase {
Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory,
ResolvableType.forClass(Pojo.class),
MediaType.APPLICATION_XML);
MediaType.APPLICATION_XML, Collections.emptyMap());
TestSubscriber
.subscribe(output)
.assertValuesWith(dataBuffer -> {

View File

@@ -16,6 +16,7 @@
package org.springframework.http.codec.xml;
import java.util.Collections;
import javax.xml.stream.events.XMLEvent;
import org.junit.Test;
@@ -44,7 +45,7 @@ public class XmlEventDecoderTests extends AbstractDataBufferAllocatingTestCase {
public void toXMLEventsAalto() {
Flux<XMLEvent> events =
this.decoder.decode(Flux.just(stringBuffer(XML)), null, null);
this.decoder.decode(Flux.just(stringBuffer(XML)), null, null, Collections.emptyMap());
TestSubscriber
.subscribe(events)
@@ -66,7 +67,7 @@ public class XmlEventDecoderTests extends AbstractDataBufferAllocatingTestCase {
decoder.useAalto = false;
Flux<XMLEvent> events =
this.decoder.decode(Flux.just(stringBuffer(XML)), null, null);
this.decoder.decode(Flux.just(stringBuffer(XML)), null, null, Collections.emptyMap());
TestSubscriber
.subscribe(events)