Skip to content

Commit

Permalink
Expose parameter annotations from interfaces across entire class hier…
Browse files Browse the repository at this point in the history
…archy

Closes spring-projectsgh-24127
  • Loading branch information
jhoeller committed Mar 23, 2020
1 parent dbb0933 commit 9f71c98
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -339,7 +339,7 @@ private List<Annotation[][]> getInterfaceParameterAnnotations() {
List<Annotation[][]> parameterAnnotations = this.interfaceParameterAnnotations;
if (parameterAnnotations == null) {
parameterAnnotations = new ArrayList<>();
for (Class<?> ifc : this.method.getDeclaringClass().getInterfaces()) {
for (Class<?> ifc : ClassUtils.getAllInterfacesForClassAsSet(this.method.getDeclaringClass())) {
for (Method candidate : ifc.getMethods()) {
if (isOverrideFor(candidate)) {
parameterAnnotations.add(candidate.getParameterAnnotations());
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -703,6 +703,27 @@ public void resolveArgumentTypeVariableWithGenericInterface() throws Exception {

RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);

assertTrue(processor.supportsParameter(methodParameter));
String value = (String) processor.readWithMessageConverters(
this.request, methodParameter, methodParameter.getGenericParameterType());
assertEquals("foo", value);
}

@Test // gh-24127
public void resolveArgumentTypeVariableWithGenericInterfaceAndSubclass() throws Exception {
this.servletRequest.setContent("\"foo\"".getBytes("UTF-8"));
this.servletRequest.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);

Method method = SubControllerImplementingInterface.class.getMethod("handle", Object.class);
HandlerMethod handlerMethod = new HandlerMethod(new SubControllerImplementingInterface(), method);
MethodParameter methodParameter = handlerMethod.getMethodParameters()[0];

List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());

RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);

assertTrue(processor.supportsParameter(methodParameter));
String value = (String) processor.readWithMessageConverters(
this.request, methodParameter, methodParameter.getGenericParameterType());
assertEquals("foo", value);
Expand Down Expand Up @@ -1055,4 +1076,13 @@ default A handle(@RequestBody A arg) {
static class MyControllerImplementingInterface implements MappingInterface<String> {
}


static class SubControllerImplementingInterface extends MyControllerImplementingInterface {

@Override
public String handle(String arg) {
return arg;
}
}

}

0 comments on commit 9f71c98

Please sign in to comment.