Skip to content

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

Closed

Description

@spring-projects-issues

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:

Referenced from: commits c58da71

Activity

  1. spring-projects-issues commented on Nov 2, 2018

    @spring-projects-issues
    CollaboratorAuthor

    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?

  2. spring-projects-issues commented on Nov 5, 2018

    @spring-projects-issues
    CollaboratorAuthor

    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.

  3. spring-projects-issues commented on Nov 5, 2018

    @spring-projects-issues
    CollaboratorAuthor

    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?

  4. spring-projects-issues commented on Nov 12, 2018

    @spring-projects-issues
    CollaboratorAuthor

    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")

  5. spring-projects-issues commented on Nov 12, 2018

    @spring-projects-issues
    CollaboratorAuthor

    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.

  6. added
    type: bug
    in: web
    on Jan 11, 2019
  7. added this to the 5.1.3 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions