Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSON converter only serialises fields of controller method return type, ignoring subclass fields of response object [SPR-16461] #21006

Closed
spring-projects-issues opened this issue Feb 2, 2018 · 1 comment
Assignees
Labels
in: web status: backported type: regression
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Feb 2, 2018

Fleur Kelpin opened SPR-16461 and commented

The GSON message converter used to serialise using the class of the object that was being serialised. Now it uses the return type of the controller method.

So when I have

@GetMapping("/animal")
public Animal getAnimal() { return new Cat(...); }

the Cat-specific fields do not get serialised, only Animal fields.

Workarounds:

  • When I replace GSON with Jackson on the classpath, Cat fields get serialised again.
  • When I subclass the GsonHttpMessageSerializer and override writeInternal as follows:
@Override
protected void writeInternal(Object o, @Nullable Type type, Writer writer) throws Exception
{
    // replaces the type info with null value
    super.writeInternal(o, null, writer); 
}

the Cat fields get serialized again.

Probably introduced in #17408, the last version that serialized Cats as Cats was 4.1.9.

To reproduce:
Run the repro project, and GET http://localhost:8080/SPR-16461/animal
Response should be the same as http://localhost:8080/SPR-16461/cat but isn't.


Affects: 4.3.14, 5.0.3

Reference URL: spring-attic/spring-framework-issues#175

Issue Links:

  • #17408 Make @ResponseBody method return type available to message converters
  • #21072 GsonHttpMessageConverter cannot be used in an SseEmitter because it closes the response stream

Referenced from: commits 817a836, fd964ca

Backported to: 4.3.15

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 5, 2018

Juergen Hoeller commented

It turns out that Gson effectively reduces the rendering type information to the given type token... but only for the single element case, not for the collection case (i.e. a set of base type declaration will still have all concrete element instances rendered with all their fields). This smells like a bug in Gson but we can easily work around it through only passing in parameterized type information (but not regular Class types which don't add any value for our message converter needs). We do that as of 5.0.4 now, also for the JSON Binding API (Jsonb); to be backported to 4.3.15 soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web status: backported type: regression
Projects
None yet
Development

No branches or pull requests

2 participants