Repository navigation
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
Activity
spring-projects-issues commentedon Mar 12, 2015
Juergen Hoeller commented
Sébastien Deleuze, I suppose we could change the
elseblock to:this.objectMapper.writerWithType(value.getClass()).writeValue(generator, value);Would that have any negative side effects? Should it rather be conditional?
Juergen
spring-projects-issues commentedon Mar 12, 2015
Juergen Hoeller commented
Hmm, this seems to be what Jackson is doing without a
writerWithTypecall anyway... Where does Jackson evaluate@JsonTypeInfo? Do we have to manually pass this in?Juergen
spring-projects-issues commentedon Mar 12, 2015
Miroslav Senaj commented
This is strange because If I manually convert my List of objects using ObjectMapper like:
It generates correct output with "type" in it.
This indicates that Jackson is not doing writeWithType if you have not specified it.
spring-projects-issues commentedon Mar 16, 2015
Sébastien Deleuze commented
I am not sure this is an issue on Spring side.
When using
List<ParentClass>return type, Jackson just see aList<Object>due to type erasure, and aBeanSerializeris used to writeFooandBarand it seems to not support writing the type info.When using a wrapper like
AnotherClass, Jackson see aList<ParentClass>, anAsPropertyTypeSerializerthat supports writing the type info is used.Maybe we could create an issue on jackson-databind bugtracker, asking to improve Jackson
BeanSerializerto write the type information based on each List element type ? This could have some performance impact, but it could be enabled by a Jackson feature.Please also notice that Jackson bugtracker contains a lot of issues related to TypeInfo.
Any thoughts ?
spring-projects-issues commentedon Mar 16, 2015
Juergen Hoeller commented
A framework cannot use the
TypeReferenceapproach, unfortunately, since that little trick is effectively based on a user-declared inline subclass for each specific type, which we cannot mimic in generic framework code.I suppose we could introspect the
@ResponseBody-declaring method's return type and try to pass a correspondingJavaTypeto Jackson, assuming that the method declaration preserves generic type information which the returned object would otherwise lose. From my understanding, that is the only approach we could actually implement.In any case, this is a rather involved improvement then, so I'm turning this into a 4.2-only issue along those lines.
Juergen
spring-projects-issues commentedon Apr 24, 2015
Rossen Stoyanchev commented
Note that we support this on the reading side, see GenericHttpMessageConverter, since otherwise Jackson would have no way of knowing how to read the body to something like List<Foo>. We didn't do the same for the writing side where the starting point is an actual object and the assumption is it should be possible to obtain all necessary information. Perhaps we can extend GenericHttpMessageConverter to include the writing side. Otherwise Jackson would have to do something to look at the List elements.
spring-projects-issues commentedon Jun 22, 2015
Sébastien Deleuze commented
This commit adds
canWrite()andwrite()methods to theGenericHttpMessageConverterinterface. These are type aware variantsof the methods available in
HttpMessageConverter, in order to keepparametrized type information when serializing objects.
AbstractMessageConverterMethodProcessornow calls those type awaremethods when the message converter implements
GenericHttpMessageConverter.AbstractJackson2HttpMessageConverterandGsonHttpMessageConverterusesthese new methods to make
@ResponseBodymethod return type availablefor type resolution instead of just letting the JSON serializer trying
to guess the type to use from the object to serialize.
See also #17745 for the same improvement applied to request body serialization.
spring-projects-issues commentedon Jun 25, 2015
Sébastien Deleuze commented
This commit enables serialization with type only with Jackson 2.6+ since previous versions do not serialize polymorphic collections correctly when the type is specified. See FasterXML/jackson-databind#727 for the related Jackson fix.