Skip to content

Commit 087c942

Browse files
committed
Add. hint message for missing @param value
- Added a helpful hint in the exception message when @param is missing a value and parameter name can't be resolved. - Added a unit test to cover this case. Files affected: - Contract.java - DefaultContractTest.java Fixes OpenFeign#2337
1 parent 99d859f commit 087c942

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

core/src/main/java/feign/Contract.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,12 @@ public Default() {
327327
name = annotationName;
328328
}
329329
checkState(
330-
emptyToNull(name) != null, "Param annotation was empty on param %s.", paramIndex);
330+
emptyToNull(name) != null,
331+
"Param annotation was empty on param %s.\nHint: %s",
332+
paramIndex,
333+
"Prefer using @Param(value=\"name\"), or compile your code with the -parameters flag.\n"
334+
+ "If the value is missing, Feign attempts to retrieve the parameter name from bytecode, "
335+
+ "which only works if the class was compiled with the -parameters flag.");
331336
nameParam(data, name, paramIndex);
332337
final Class<? extends Param.Expander> expander = paramAnnotation.expander();
333338
if (expander != Param.ToStringExpander.class) {

core/src/test/java/feign/DefaultContractTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,4 +901,20 @@ public Instant instant() {
901901
return Instant.ofEpochMilli(millis);
902902
}
903903
}
904+
905+
interface NoValueParamsInterface {
906+
@RequestLine("GET /getSomething?id={id}")
907+
String getSomething(@Param String id);
908+
}
909+
910+
@Test
911+
void errorMessageOnMissingParamNames() {
912+
Throwable exception =
913+
assertThrows(
914+
IllegalStateException.class,
915+
() -> contract.parseAndValidateMetadata(NoValueParamsInterface.class));
916+
assertThat(exception.getMessage())
917+
.contains("Param annotation was empty on param 0")
918+
.contains("Hint");
919+
}
904920
}

0 commit comments

Comments
 (0)