-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
Miroslav Senaj opened SPR-12811 and commented
In Spring MVC I have following object hierarchy.
@JsonTypeInfo( use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
public abstract class ParentClass {
}
@JsonTypeName("foo")
public class Foo extends ParentClass {
private String person;
}
@JsonTypeName("bar")
public class Bar extends ParentClass {
private String item;
}
ACTUAL:
When I generate response from method as List<ParentClass> it generates JSON without "type" : "foo" or "type" : "bar".
@ResponseBody
public List<ParentClass> method() {
List<ParentClass> result = // generate classes foo and bar
return result;
}
[{ "person" : "John Doe" }, { "item" : "rifle" }]
EXPECTED:
In List<T> response type it should generate "type" as well.
@ResponseBody
public List<ParentClass> method() {
List<ParentClass> result = // generate classes foo and bar
return result;
}
[{ "type" : "foo", "person" : "John Doe" }, { "type" : "bar", "item" : "rifle" }]
TRIVIA:
When I return from @Controller
in @ResponseBody
object which contains List<ParentClass> as
public class AnotherClass {
public List<ParentClass> values;
}
It generates proper response with proper "type" in it.
@ResponseBody
public AnotherClass method() {
AnotherClass result = // generate Another class and list of foo and bar
return result;
}
{ "values" :[
{ "type" : "foo", "person" : "John Doe" },
{ "type" : "bar", "item" : "rifle" } ]
}
See also stackoverflow issue
What I found so far that in
org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter:228
this.objectMapper.writerWithView(serializationView).writeValue(generator, value);
Is serializing data without its type and base on this topic it should have writerWithType in order to write type.
Affects: 4.1.1
Issue Links:
- Type aware canWrite() method should be called for GenericHttpMessageConverter [SPR-13161] #17752 Type aware canWrite() method should be called for GenericHttpMessageConverter
- Jackson 2.6: message converter should use type only for collections [SPR-13318] #17903 Jackson 2.6: message converter should use type only for collections
- Revise AbstractJackson2HttpMessageConverter's generic type adaptation [SPR-13728] #18301 Revise AbstractJackson2HttpMessageConverter's generic type adaptation
- HTTP Response should not contain both Transfer-Encoding and Content-Length headers [SPR-15212] #19776 HTTP Response should not contain both Transfer-Encoding and Content-Length headers
- GSON converter only serialises fields of controller method return type, ignoring subclass fields of response object [SPR-16461] #21006 GSON converter only serialises fields of controller method return type, ignoring subclass fields of response object
- RequestPartMethodArgumentResolver should defensively handle MethodParameter nesting level and java.util.Optional access [SPR-13850] #18423 RequestPartMethodArgumentResolver should defensively handle MethodParameter nesting level and java.util.Optional access
- Better parameterized type serialization for the request body in RestTemplate [SPR-13154] #17745 Better parameterized type serialization for the request body in RestTemplate
Referenced from: commits 31a5434, 3bff7bd
1 votes, 6 watchers