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

Subclass-resolved generic return type declarations for handler methods [SPR-16877] #21416

Closed
spring-issuemaster opened this issue May 27, 2018 · 3 comments
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented May 27, 2018

Daniel Theuke opened SPR-16877 and commented

If you have a generic AbstractController that specifies a method with a @RequestMapping and you don't overwrite it in the SpecificController then the GenericHttpMessageConverter won't get the actual data type that was returned from the method.

Due to this behavior it is impossible to use inheritance with generics for type dependent data formats such as CSV.

 

Provided data type: java.util.List<T>

Actual/Expected data type: java.util.List<MyDTO>

See the attached demo project for an easy way to reproduce.


There is a workaround for that though, override all the inherited methods in the SpecificController and just delegate to super.

 

@Override
public List<MyDTO> getList() {
     return super.getList();
}

 

However this is easy to forget, hard to spot and just adds plenty of noise to the controllers.


I haven't made any recent tests with Spring 4.x, but IIRC that version is also affected.


Affects: 5.0.6

Attachments:

Issue Links:

  • #19944 Revise generic type handling in GsonHttpMessageConverter ("duplicates")
  • #15682 Enable REST controller method parameter annotations on an interface

Referenced from: commits b915e42

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 16, 2018

Juergen Hoeller commented

We resolve the target type for GenericHttpMessageConverter.canWrite/write against the concrete controller class now, exposing an artificial type reference with substituted type variables in a scenario like yours.

This will be available in the upcoming 5.1.0.BUILD-SNAPSHOT. Feel free to give it an early try...

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 16, 2018

Daniel Theuke commented

Works as expected.
Thank you.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 16, 2018

Juergen Hoeller commented

Good to hear :-) Thanks for the immediate feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.