|
26 | 26 | import java.lang.annotation.Retention;
|
27 | 27 | import java.lang.annotation.RetentionPolicy;
|
28 | 28 | import java.lang.annotation.Target;
|
| 29 | +import java.lang.reflect.Method; |
| 30 | +import java.lang.reflect.Parameter; |
29 | 31 | import java.net.URI;
|
30 | 32 | import java.time.Clock;
|
31 | 33 | import java.time.Instant;
|
|
37 | 39 | import java.util.Map;
|
38 | 40 | import java.util.SortedMap;
|
39 | 41 | import org.assertj.core.api.Fail;
|
| 42 | +import org.junit.jupiter.api.Assumptions; |
40 | 43 | import org.junit.jupiter.api.Test;
|
41 | 44 |
|
42 | 45 | /**
|
@@ -901,4 +904,43 @@ public Instant instant() {
|
901 | 904 | return Instant.ofEpochMilli(millis);
|
902 | 905 | }
|
903 | 906 | }
|
| 907 | + |
| 908 | + interface NoValueParamsInterface { |
| 909 | + @RequestLine("GET /getSomething?id={id}") |
| 910 | + String getSomething(@Param String id); |
| 911 | + } |
| 912 | + |
| 913 | + @Test |
| 914 | + void errorMessageOnMissingParamNames() { |
| 915 | + Assumptions.assumeTrue( |
| 916 | + !isCompiledWithParameters(NoValueParamsInterface.class), "Skip if -parameters is enabled"); |
| 917 | + |
| 918 | + Throwable exception = |
| 919 | + assertThrows( |
| 920 | + IllegalStateException.class, |
| 921 | + () -> contract.parseAndValidateMetadata(NoValueParamsInterface.class)); |
| 922 | + |
| 923 | + assertThat(exception.getMessage()) |
| 924 | + .contains("Param annotation was empty on param 0") |
| 925 | + .contains("Hint"); |
| 926 | + } |
| 927 | + |
| 928 | + /** |
| 929 | + * Checks whether the given class was compiled with the `-parameters` compiler flag. If parameter |
| 930 | + * names are preserved (i.e., not default like arg0, arg1...), we assume it was. |
| 931 | + * |
| 932 | + * @param clazz the class to inspect |
| 933 | + * @return true if `-parameters` was likely used, false otherwise |
| 934 | + */ |
| 935 | + private static boolean isCompiledWithParameters(Class<?> clazz) { |
| 936 | + for (Method method : clazz.getDeclaredMethods()) { |
| 937 | + for (Parameter parameter : method.getParameters()) { |
| 938 | + String paramName = parameter.getName(); |
| 939 | + if (!paramName.matches("arg\\d+")) { |
| 940 | + return true; |
| 941 | + } |
| 942 | + } |
| 943 | + } |
| 944 | + return false; |
| 945 | + } |
904 | 946 | }
|
0 commit comments