Use specified ResolvableType in JacksonJsonEncoder

This commit also fixes an issue in the HTTP client that used the
wrapper type instead of the element type. As a consequence, due
to type erasure, we now have to specify the type of the content
in DefaultHttpRequestBuilder#contentStream().
This commit is contained in:
Sebastien Deleuze
2016-06-20 16:23:02 +02:00
parent 5141a198d9
commit a0e2231779
3 changed files with 58 additions and 10 deletions

View File

@@ -16,11 +16,14 @@
package org.springframework.core.codec.support;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.test.TestSubscriber;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.MediaType;
@@ -50,8 +53,9 @@ public class JacksonJsonEncoderTests extends AbstractDataBufferAllocatingTestCas
public void write() {
Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
ResolvableType type = ResolvableType.forClass(Pojo.class);
Flux<DataBuffer> output =
this.encoder.encode(source, this.dataBufferFactory, null, null);
this.encoder.encode(source, this.dataBufferFactory, type, null);
TestSubscriber
.subscribe(output)
@@ -64,4 +68,35 @@ public class JacksonJsonEncoderTests extends AbstractDataBufferAllocatingTestCas
stringConsumer("]"));
}
@Test
public void writeWithType() {
Flux<ParentClass> source = Flux.just(new Foo(), new Bar());
ResolvableType type = ResolvableType.forClass(ParentClass.class);
Flux<DataBuffer> output =
this.encoder.encode(source, this.dataBufferFactory, type, null);
TestSubscriber
.subscribe(output)
.assertComplete()
.assertNoError()
.assertValuesWith(stringConsumer("["),
stringConsumer("{\"type\":\"foo\"}"),
stringConsumer(","),
stringConsumer("{\"type\":\"bar\"}"),
stringConsumer("]"));
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
private static class ParentClass {
}
@JsonTypeName("foo")
private static class Foo extends ParentClass {
}
@JsonTypeName("bar")
private static class Bar extends ParentClass {
}
}