Skip to content

Commit 8921dff

Browse files
authored
feat: warn when GET method has unannotated parameters
Signed-off-by: JuHyeong <[email protected]>
1 parent f5446de commit 8921dff

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringMvcContract.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
import org.springframework.http.MediaType;
6969
import org.springframework.util.Assert;
7070
import org.springframework.util.StringUtils;
71+
import org.springframework.web.bind.annotation.PathVariable;
72+
import org.springframework.web.bind.annotation.RequestHeader;
7173
import org.springframework.web.bind.annotation.RequestMapping;
7274
import org.springframework.web.bind.annotation.RequestMethod;
7375
import org.springframework.web.bind.annotation.RequestParam;
@@ -89,6 +91,7 @@
8991
* @author Ram Anaswara
9092
* @author Sam Kruglov
9193
* @author Tang Xiong
94+
* @author Juhyeong An
9295
*/
9396
public class SpringMvcContract extends Contract.BaseContract implements ResourceLoaderAware {
9497

@@ -234,7 +237,38 @@ protected void processAnnotationOnClass(MethodMetadata data, Class<?> clz) {
234237
@Override
235238
public MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method method) {
236239
processedMethods.put(Feign.configKey(targetType, method), method);
237-
return super.parseAndValidateMetadata(targetType, method);
240+
MethodMetadata metadata = super.parseAndValidateMetadata(targetType, method);
241+
242+
if (isGetMethod(metadata) && method.getParameterCount() > 0 && !hasHttpAnnotations(method)) {
243+
LOG.warn(String.format(
244+
"[OpenFeign Warning] Feign method '%s' is declared as GET with parameters, but none of the parameters are annotated " +
245+
"(e.g. @RequestParam, @RequestHeader, @PathVariable, etc). This may result in fallback to POST at runtime. " +
246+
"Consider explicitly annotating parameters.",
247+
method.toGenericString()
248+
));
249+
}
250+
251+
return metadata;
252+
}
253+
254+
private boolean isGetMethod(MethodMetadata metadata) {
255+
return "GET".equalsIgnoreCase(metadata.template().method());
256+
}
257+
258+
private boolean hasHttpAnnotations(Method method) {
259+
for (Parameter parameter : method.getParameters()) {
260+
for (Annotation annotation : parameter.getAnnotations()) {
261+
Class<? extends Annotation> annotationType = annotation.annotationType();
262+
if (annotationType == RequestParam.class ||
263+
annotationType == RequestHeader.class ||
264+
annotationType == PathVariable.class ||
265+
annotationType == SpringQueryMap.class ||
266+
annotationType == QueryMap.class) {
267+
return true;
268+
}
269+
}
270+
}
271+
return false;
238272
}
239273

240274
@Override

0 commit comments

Comments
 (0)