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

Aliases in parameter annotations on a controller interface are not evaluated [SPR-17460] #21992

Closed
spring-issuemaster opened this Issue Nov 2, 2018 · 5 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

spring-issuemaster commented Nov 2, 2018

Bogdan Zafirov opened SPR-17460 and commented

The problem is explained in this Stack Overflow answer: https://stackoverflow.com/a/8005644. As pointed out in the comment to this answer, this is "problem" is already solved in other frameworks such as Jersey.

The goal would be Spring Framework to consider/evaluate the annotations on interface methods (e.g. public void getUser(@RequestParam("user-id")), so a developer doesn't have to define the annotations in the implementing class again.

By generating a server implementation from a OAS3 contract (e.g. https://github.com/OpenAPITools/openapi-generator) one can easily face this problem, since the annotations from the generated code (a Java Interface) will not be evaluated on a HTTP call.


Affects: 5.1.2

Attachments:

Issue Links:

  • #15682 Enable REST controller method parameter annotations on an interface

Referenced from: commits c58da71

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Nov 2, 2018

Juergen Hoeller commented

We aimed to address this in 5.1 through #15682... Could you please double-check that your case indeed does not work against 5.1.2 and ideally provide a particular case to reproduce?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Nov 5, 2018

Bogdan Zafirov commented

Juergen Hoeller I attached an example Spring-Boot application (created with https://start.spring.io). There is one interface (TestInterface.java) and an implementing class annotated with RestController (TestController.java). When I do a HTTP GET with the following URL  http://localhost:8080/v1/test/ZZ95711905000000500000 I get 'Internal Server Error'. The error message is: "Missing URI template variable 'testId' for method parameter of type String". This actually shows that Spring is considering the variable name instead of the value set in the PathVariable (e.g. test-id) annotation. The same thing happens for the header parameter 'X-Request-ID', namely the header parameter has to named xRequestID.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Nov 5, 2018

Juergen Hoeller commented

Brian Clozel, could you please try to debug the repro app and double-check why this still doesn't work against 5.1.x there?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Nov 12, 2018

WiedemannA commented

Juergen Hoeller: I've got the same problem in version 5.1 and debugged the code:
The difference seems to be, that the @AliasFor-Annotation (e.g. in PathVariable) is not evaluated by the new code that takes over the interface annotations.

And the swagger-codegen (OpenApiGenerator likewise) generates interfaces in which only value is set, e.g.:
@PathVariable("test-id")

The PathVariableMethodArgumentResolver only takes over the name out of the PathVariable-Annotation.
 
Therefore the check if the name is empty in AbstractNamedValueMethodArgumentResolver.updateNamedValueInfo() delivers true and falls back to the ParameterName, which causes the error Bogdan Zafirov describes...

Bogdan Zafirov: Can you verifiy, that the Problem doesn't occure, if you set both value and name?
@PathVariable(name= "test-id", value = "test-id")

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Nov 12, 2018

Juergen Hoeller commented

WiedemannA, good catch! I was able to reproduce this; we indeed miss a synthesizeAnnotation step for interface-level annotations, not evaluating their aliases. Fixed in master now for the 5.1.3 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.