Skip to content

Commit

Permalink
Add parameter annotation @tag for setting request tags
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Feb 15, 2019
1 parent 3c68997 commit 4a83173
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
12 changes: 12 additions & 0 deletions retrofit/src/main/java/retrofit2/ParameterHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,16 @@ static final class Body<T> extends ParameterHandler<T> {
builder.setBody(body);
}
}

static final class Tag<T> extends ParameterHandler<T> {
final Class<T> cls;

Tag(Class<T> cls) {
this.cls = cls;
}

@Override void apply(RequestBuilder builder, @Nullable T value) {
builder.addTag(cls, value);
}
}
}
4 changes: 4 additions & 0 deletions retrofit/src/main/java/retrofit2/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ void setBody(RequestBody body) {
this.body = body;
}

<T> void addTag(Class<T> cls, @Nullable T value) {
requestBuilder.tag(cls, value);
}

Request.Builder get() {
HttpUrl url;
HttpUrl.Builder urlBuilder = this.urlBuilder;
Expand Down
19 changes: 19 additions & 0 deletions retrofit/src/main/java/retrofit2/RequestFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import retrofit2.http.Query;
import retrofit2.http.QueryMap;
import retrofit2.http.QueryName;
import retrofit2.http.Tag;
import retrofit2.http.Url;

import static retrofit2.Utils.methodError;
Expand Down Expand Up @@ -712,6 +713,24 @@ private ParameterHandler<?> parseParameterAnnotation(
}
gotBody = true;
return new ParameterHandler.Body<>(method, p, converter);

} else if (annotation instanceof Tag) {
validateResolvableType(p, type);

Class<?> tagType = Utils.getRawType(type);
for (int i = p - 1; i >= 0; i--) {
ParameterHandler<?> otherHandler = parameterHandlers[i];
if (otherHandler instanceof ParameterHandler.Tag
&& ((ParameterHandler.Tag) otherHandler).cls.equals(tagType)) {
throw parameterError(method, p, "@Tag type "
+ tagType.getName()
+ " is duplicate of parameter #"
+ (i + 1)
+ " and would always overwrite its value.");
}
}

return new ParameterHandler.Tag<>(tagType);
}

return null; // Not a Retrofit annotation.
Expand Down
39 changes: 39 additions & 0 deletions retrofit/src/main/java/retrofit2/http/Tag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2019 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit2.http;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Adds the argument instance as a request tag using the type as the key.
* <pre><code>
* &#64;GET("/")
* Call&lt;ResponseBody&gt; foo(@Tag String tag);
* </code></pre>
* Tag arguments may be {@code null} which will omit them from the request. Passing a parameterized
* type such as {@code List<String>} will use the raw type (i.e., {@code List.class}) as the key.
* Duplicate tag types are not allowed.
*/
@Documented
@Target(PARAMETER)
@Retention(RUNTIME)
public @interface Tag {
}
61 changes: 60 additions & 1 deletion retrofit/src/test/java/retrofit2/RequestFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@
import retrofit2.http.Query;
import retrofit2.http.QueryMap;
import retrofit2.http.QueryName;
import retrofit2.http.Tag;
import retrofit2.http.Url;

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;
Expand Down Expand Up @@ -2250,7 +2252,7 @@ Call<ResponseBody> method(@PartMap List<Object> parts) {
}

try {
buildRequest(Example.class, Collections.emptyList());
buildRequest(Example.class, emptyList());
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage(
Expand Down Expand Up @@ -2877,6 +2879,63 @@ class Example {
}
}

@Test public void tag() {
class Example {
@GET("/") Call<ResponseBody> method(@Tag String tag) {
return null;
}
}

Request request = buildRequest(Example.class, "tagValue");
assertThat(request.tag(String.class)).isEqualTo("tagValue");
}

@Test public void tagGeneric() {
class Example {
@GET("/") Call<ResponseBody> method(@Tag List<String> tag) {
return null;
}
}

List<String> strings = Arrays.asList("tag", "value");
Request request = buildRequest(Example.class, strings);
assertThat(request.tag(List.class)).isSameAs(strings);
}

@Test public void tagDuplicateFails() {
class Example {
@GET("/") Call<ResponseBody> method(@Tag String one, @Tag String two) {
return null;
}
}

try {
buildRequest(Example.class, "one", "two");
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage(
"@Tag type java.lang.String is duplicate of parameter #1 and would always overwrite its value. (parameter #2)\n"
+ " for method Example.method");
}
}

@Test public void tagGenericDuplicateFails() {
class Example {
@GET("/") Call<ResponseBody> method(@Tag List<String> one, @Tag List<Long> two) {
return null;
}
}

try {
buildRequest(Example.class, emptyList(), emptyList());
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage(
"@Tag type java.util.List is duplicate of parameter #1 and would always overwrite its value. (parameter #2)\n"
+ " for method Example.method");
}
}

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

0 comments on commit 4a83173

Please sign in to comment.