Skip to content
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

Allow parameters with custom annotations in service methods #3464

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions retrofit/src/main/java/retrofit2/RequestFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ okhttp3.Request create(Object[] args) throws IOException {
List<Object> argumentList = new ArrayList<>(argumentCount);
for (int p = 0; p < argumentCount; p++) {
argumentList.add(args[p]);
handlers[p].apply(requestBuilder, args[p]);
ParameterHandler<Object> handler = handlers[p];
if (handler != null) {
handler.apply(requestBuilder, args[p]);
}
}

return requestBuilder.get().tag(Invocation.class, new Invocation(method, argumentList)).build();
Expand Down Expand Up @@ -347,7 +350,9 @@ private Headers parseHeaders(String[] headers) {
} catch (NoClassDefFoundError ignored) {
}
}
throw parameterError(method, p, "No Retrofit annotation found.");
if (annotations == null || annotations.length == 0) {
throw parameterError(method, p, "No annotation found.");
}
}

return result;
Expand Down
22 changes: 21 additions & 1 deletion retrofit/src/test/java/retrofit2/RequestFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package retrofit2;

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.net.URI;
Expand Down Expand Up @@ -465,7 +467,7 @@ Call<ResponseBody> method(String a) {
} catch (IllegalArgumentException e) {
assertThat(e)
.hasMessage(
"No Retrofit annotation found. (parameter #1)\n for method Example.method");
"No annotation found. (parameter #1)\n for method Example.method");
}
}

Expand Down Expand Up @@ -3252,6 +3254,24 @@ Call<ResponseBody> method(@Tag List<String> one, @Tag List<Long> two) {
}
}

@Retention(RUNTIME)
@interface CustomAnnotation {}

@Test
public void parameterWithCustomAnnotation() {
class Example {
@GET("/") //
Call<ResponseBody> method(@CustomAnnotation String a) {
return null;
}
}
Request request = buildRequest(Example.class, "CustomArgument");
assertThat(request.url().toString()).isEqualTo("http://example.com/");
Invocation invocation = request.tag(Invocation.class);
assertThat(invocation.arguments().get(0)).isEqualTo("CustomArgument");
assertThat(invocation.method().getParameterAnnotations()[0][0]).isInstanceOf(CustomAnnotation.class);
}

private static void assertBody(RequestBody body, String expected) {
assertThat(body).isNotNull();
Buffer buffer = new Buffer();
Expand Down