-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into return-kt-result-type
- Loading branch information
Showing
37 changed files
with
887 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...rs/jackson/src/test/java/retrofit2/converter/jackson/JacksonCborConverterFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (C) 2024 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.converter.jackson; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper; | ||
import java.io.IOException; | ||
import okhttp3.MediaType; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import okhttp3.mockwebserver.RecordedRequest; | ||
import okio.Buffer; | ||
import okio.ByteString; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import retrofit2.Call; | ||
import retrofit2.Response; | ||
import retrofit2.Retrofit; | ||
import retrofit2.http.Body; | ||
import retrofit2.http.POST; | ||
|
||
public class JacksonCborConverterFactoryTest { | ||
static class IntWrapper { | ||
public int value; | ||
|
||
public IntWrapper(int v) { | ||
value = v; | ||
} | ||
|
||
protected IntWrapper() {} | ||
} | ||
|
||
interface Service { | ||
@POST("/") | ||
Call<IntWrapper> post(@Body IntWrapper person); | ||
} | ||
|
||
@Rule public final MockWebServer server = new MockWebServer(); | ||
|
||
private Service service; | ||
|
||
@Before | ||
public void setUp() { | ||
Retrofit retrofit = | ||
new Retrofit.Builder() | ||
.baseUrl(server.url("/")) | ||
.addConverterFactory( | ||
JacksonConverterFactory.create(new CBORMapper(), MediaType.get("application/cbor"))) | ||
.build(); | ||
service = retrofit.create(Service.class); | ||
} | ||
|
||
@Test | ||
public void post() throws IOException, InterruptedException { | ||
server.enqueue( | ||
new MockResponse() | ||
.setBody(new Buffer().write(ByteString.decodeHex("bf6576616c7565182aff")))); | ||
|
||
Call<IntWrapper> call = service.post(new IntWrapper(12)); | ||
Response<IntWrapper> response = call.execute(); | ||
assertThat(response.body().value).isEqualTo(42); | ||
|
||
RecordedRequest request = server.takeRequest(); | ||
assertThat(request.getBody().readByteString()) | ||
.isEqualTo(ByteString.decodeHex("bf6576616c75650cff")); | ||
assertThat(request.getHeader("Content-Type")).isEqualTo("application/cbor"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Response Type Keeper | ||
|
||
Generates keep rules for types mentioned in generic parameter positions of Retrofit service methods. | ||
|
||
## Problem | ||
|
||
Given a service method like | ||
```java | ||
@GET("users/{id}") | ||
Call<User> getUser( | ||
@Path("id") String id); | ||
``` | ||
|
||
If you execute this request and do not actually use the returned `User` instance, R8 will remove it | ||
and replace the return type as `Call<?>`. This fails Retrofit's runtime validation since a wildcard | ||
is not a valid type to pass to a converter. Note: this removal only occurs if the Retrofit's service | ||
method definition is the only reference to `User`. | ||
|
||
## Solution | ||
|
||
This module contains an annotation processor which looks at each Retrofit method and generates | ||
explicit `-keep` rules for the types mentioned. | ||
|
||
Add it to Gradle Java projects with | ||
```groovy | ||
annotationProcessor 'com.squareup.retrofit2:response-type-keeper:<version>' | ||
``` | ||
Or Gradle Kotlin projects with | ||
```groovy | ||
kapt 'com.squareup.retrofit2:response-type-keeper:<version>' | ||
``` | ||
|
||
For other build systems, the `com.squareup.retrofit2:response-type-keeper` needs added to the Java | ||
compiler `-processor` classpath. | ||
|
||
For the example above, the annotation processor's generated file would contain | ||
``` | ||
-keep com.example.User | ||
``` | ||
|
||
This works for nested generics, such as `Call<ApiResponse<User>>`, which would produce: | ||
``` | ||
-keep com.example.ApiResponse | ||
-keep com.example.User | ||
``` | ||
|
||
It also works on Kotlin `suspend` functions which turn into a type like | ||
`Continuation<? extends User>` in the Java bytecode. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
apply plugin: 'org.jetbrains.kotlin.jvm' | ||
apply plugin: 'org.jetbrains.kotlin.kapt' | ||
apply plugin: 'com.vanniktech.maven.publish' | ||
|
||
dependencies { | ||
compileOnly libs.autoService.annotations | ||
compileOnly libs.incap.runtime | ||
kapt libs.autoService.compiler | ||
kapt libs.incap.processor | ||
|
||
testImplementation libs.junit | ||
testImplementation libs.compileTesting | ||
testImplementation libs.truth | ||
testImplementation projects.retrofit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
POM_ARTIFACT_ID=response-type-keeper | ||
POM_NAME=Response Type Keeper | ||
POM_DESCRIPTION=Annotation processor to generate R8 keep rules for types mentioned in generics. |
Oops, something went wrong.