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

MethodParameter.isOptional() fails with ArrayIndexOutOfBoundsException [SPR-17629] #22160

Closed
spring-issuemaster opened this Issue Dec 28, 2018 · 3 comments

Comments

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

spring-issuemaster commented Dec 28, 2018

shollander opened SPR-17629 and commented

Calling org.springframework.core.MethodParameter.isOptional() can fail with an ArrayIndexOutOfBoundsException. I have a custom ResponseBodyAdvice class that calls this method in supports(). It fails when it hits org.springframework.boot.actuate.endpoint.web.servelt.WebMvcEndpointHandlerMappings$WebMvcLinksHandler.links with the following exception:

java.lang.ArrayIndexOutOfBoundsException: -1
    at org.springframework.web.method.HandlerMethod$HandlerMethodParamater.getParameterAnnotations(HandlerMethod.java:484) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.core.MethodParameter.hasNullableAnnotation(MethodParameter.java:357) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.core.MethodParameter.isOptional(MethodParameter.java:345) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at com.example.advice.OptionalResponseBodyAdvice.supports(OptionalResponseBodyAdvice.java:18) ~[main/:na]
    at org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceChain.processBody(RequestBodyAdviceChain.java:140) ~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]

Affects: 5.1.3

Issue Links:

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

Referenced from: commits 1faeeae

@shollander

This comment has been minimized.

Copy link

shollander commented Jan 16, 2019

@jhoeller This issue has not been fixed in 5.1.4. I still get the same ArrayIndexOutOfBoundsException, this time at line 486:

java.lang.ArrayIndexOutOfBoundsException: -1
    at org.springframework.web.method.HandlerMethod$HandlerMethodParamater.getParameterAnnotations(HandlerMethod.java:486) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.core.MethodParameter.hasNullableAnnotation(MethodParameter.java:357) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.core.MethodParameter.isOptional(MethodParameter.java:345) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at com.example.advice.OptionalResponseBodyAdvice.supports(OptionalResponseBodyAdvice.java:19) ~[main/:na]
    at org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceChain.processBody(RequestBodyAdviceChain.java:140) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]

The problem is that the getParameterIndex() method called at

will return -1 (as documented in its JavaDoc) in the case of a return type. The change in 1faeeae only addresses checking the upper bounds of index but not the lower bounds.

@rstoyanchev

This comment has been minimized.

Copy link
Contributor

rstoyanchev commented Jan 17, 2019

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

@shollander

This comment has been minimized.

Copy link

shollander commented Jan 18, 2019

@rstoyanchev Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment