Skip to content

Commit d37d22d

Browse files
authored
Merge pull request #4334 from square/jw.primitive-tags.2025-04-01
Support primitive types for `@Tag`
2 parents 3646596 + 4af3068 commit d37d22d

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
**Fixed**
1818

19-
- Nothing yet!
19+
- Primitive types used with `@Tag` now work by storing the value boxed with the boxed class as the key.
2020

2121

2222
## [2.11.0] - 2024-03-28

retrofit/java-test/src/test/java/retrofit2/RequestFactoryTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -3330,6 +3330,19 @@ Call<ResponseBody> method(@Tag String tag) {
33303330
assertThat(request.tag(String.class)).isEqualTo("tagValue");
33313331
}
33323332

3333+
@Test
3334+
public void tagPrimitive() {
3335+
class Example {
3336+
@GET("/")
3337+
Call<ResponseBody> method(@Tag long timestamp) {
3338+
return null;
3339+
}
3340+
}
3341+
3342+
Request request = buildRequest(Example.class, 42L);
3343+
assertThat(request.tag(Long.class)).isEqualTo(42L);
3344+
}
3345+
33333346
@Test
33343347
public void tagGeneric() {
33353348
class Example {

retrofit/src/main/java/retrofit2/RequestFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ private ParameterHandler<?> parseParameterAnnotation(
800800
} else if (annotation instanceof Tag) {
801801
validateResolvableType(p, type);
802802

803-
Class<?> tagType = Utils.getRawType(type);
803+
Class<?> tagType = boxIfPrimitive(Utils.getRawType(type));
804804
for (int i = p - 1; i >= 0; i--) {
805805
ParameterHandler<?> otherHandler = parameterHandlers[i];
806806
if (otherHandler instanceof ParameterHandler.Tag

retrofit/src/main/java/retrofit2/http/Tag.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
* </code></pre>
3232
*
3333
* Tag arguments may be {@code null} which will omit them from the request. Passing a parameterized
34-
* type such as {@code List<String>} will use the raw type (i.e., {@code List.class}) as the key.
34+
* type will use the raw type as the key (e.g., {@code List<String>} uses {@code List.class}).
35+
* Primitive types will be boxed and stored using the boxed type
36+
* (e.g., {@code long} uses {@code Long.class}).
3537
* Duplicate tag types are not allowed.
3638
*/
3739
@Documented

0 commit comments

Comments
 (0)