Skip to content

Commit

Permalink
fix: use Gson instead of Jackson (#1661)
Browse files Browse the repository at this point in the history
* use GSon instead of Jackson

* remove jackson2

* fix callback
  • Loading branch information
elharo committed Jan 14, 2021
1 parent 8dc6c92 commit 63ed2ca
Show file tree
Hide file tree
Showing 28 changed files with 57 additions and 64 deletions.
Expand Up @@ -20,7 +20,7 @@
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.testing.http.MockHttpTransport;
import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -72,7 +72,7 @@ public void testAppEngineCredentialWrapper() throws IOException {
final Collection<String> emptyScopes = Collections.emptyList();

HttpTransport transport = new MockHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
JsonFactory jsonFactory = new GsonFactory();

MockAppIdentityService appIdentity = new MockAppIdentityService();
appIdentity.setAccessTokenText(expectedAccessToken);
Expand Down Expand Up @@ -108,7 +108,7 @@ public void testAppEngineCredentialWrapperGetAccessToken() throws IOException {
final String expectedAccessToken = "ExpectedAccessToken";

HttpTransport transport = new MockHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
JsonFactory jsonFactory = new GsonFactory();

MockAppIdentityService appIdentity = new MockAppIdentityService();
appIdentity.setAccessTokenText(expectedAccessToken);
Expand All @@ -124,7 +124,7 @@ public void testAppEngineCredentialWrapperGetAccessToken() throws IOException {
}

public void testAppEngineCredentialWrapperNullTransportThrows() throws IOException {
JsonFactory jsonFactory = new JacksonFactory();
JsonFactory jsonFactory = new GsonFactory();
try {
new AppIdentityCredential.AppEngineCredentialWrapper(null, jsonFactory);
fail();
Expand Down
Expand Up @@ -17,21 +17,21 @@
import com.google.api.client.googleapis.notifications.TypedNotificationCallback;
import com.google.api.client.googleapis.notifications.json.JsonNotificationCallback;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.Beta;

/**
* {@link Beta} <br>
* A {@link TypedNotificationCallback} which uses an JSON content encoding with {@link
* JacksonFactory#getDefaultInstance()}.
* GsonFactory#getDefaultInstance()}.
*
* <p>Must NOT be implemented in form of an anonymous class as this will break serialization.
*
* <p>Implementation should be thread-safe. <b>Example usage:</b>
*
* <pre>
* static class MyNotificationCallback
* extends GsonNotificationCallback{@literal <}ListResponse{@literal >} {
* extends JacksonNotificationCallback{@literal <}ListResponse{@literal >} {
*
* private static final long serialVersionUID = 1L;
*
Expand Down Expand Up @@ -67,6 +67,6 @@ public abstract class JacksonNotificationCallback<T> extends JsonNotificationCal

@Override
protected JsonFactory getJsonFactory() {
return JacksonFactory.getDefaultInstance();
return GsonFactory.getDefaultInstance();
}
}
5 changes: 0 additions & 5 deletions google-api-client/pom.xml
Expand Up @@ -121,14 +121,9 @@
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-gson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
Expand Down
Expand Up @@ -43,7 +43,7 @@
* static void requestAccessToken() throws IOException {
* try {
* GoogleTokenResponse response =
* new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(),
* new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(), new GsonFactory(),
* "812741506391.apps.googleusercontent.com", "{client_secret}",
* "4/P7q7W91a-oMsCeLvIaQm6bTrgtp7", "https://oauth2-login-demo.appspot.com/code")
* .execute();
Expand Down
Expand Up @@ -41,7 +41,7 @@
* static void refreshAccessToken() throws IOException {
* try {
* TokenResponse response =
* new GoogleRefreshTokenRequest(new NetHttpTransport(), new JacksonFactory(),
* new GoogleRefreshTokenRequest(new NetHttpTransport(), new GsonFactory(),
* "tGzv3JOkF0XG5Qx2TlKWIA", "s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw").execute();
* System.out.println("Access token: " + response.getAccessToken());
* } catch (TokenResponseException e) {
Expand Down
Expand Up @@ -113,7 +113,7 @@
* headers.setApplicationName("Google-YouTubeSample/1.0");
* headers.gdataVersion = "2";
* JsonCParser parser = new JsonCParser();
* parser.jsonFactory = new JacksonFactory();
* parser.jsonFactory = new GsonFactory();
* transport.addParser(parser);
* // insert authentication code...
* return transport;
Expand Down
Expand Up @@ -51,7 +51,7 @@
*
* {@literal @}Override
* protected ObjectParser getObjectParser(Notification notification) throws IOException {
* return new JsonObjectParser(new JacksonFactory());
* return new JsonObjectParser(new GsonFactory());
* }
*
* {@literal @}Override
Expand Down
Expand Up @@ -50,7 +50,7 @@
*
* {@literal @}Override
* protected JsonFactory getJsonFactory() throws IOException {
* return new JacksonFactory();
* return new GsonFactory();
* }
*
* {@literal @}Override
Expand Down
Expand Up @@ -6,7 +6,7 @@
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.Json;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
Expand Down Expand Up @@ -84,7 +84,7 @@ public MockGoogleCredential build() {
setClientAuthentication(new MockClientAuthentication());
}
if (getJsonFactory() == null) {
setJsonFactory(new JacksonFactory());
setJsonFactory(new GsonFactory());
}
return new MockGoogleCredential(this);
}
Expand Down
Expand Up @@ -21,7 +21,7 @@
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.Json;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebSignature;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
Expand All @@ -47,7 +47,7 @@ public class MockTokenServerTransport extends MockHttpTransport {
private static final Logger LOGGER = Logger.getLogger(MockTokenServerTransport.class.getName());

static final String EXPECTED_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer";
static final JsonFactory JSON_FACTORY = new JacksonFactory();
static final JsonFactory JSON_FACTORY = new GsonFactory();
final String tokenServerUrl;
Map<String, String> serviceAccounts = new HashMap<String, String>();
Map<String, String> clients = new HashMap<String, String>();
Expand Down
Expand Up @@ -20,7 +20,7 @@
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.Json;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
Expand All @@ -41,7 +41,7 @@ public class MockMetadataServerTransport extends MockHttpTransport {
private static final String METADATA_TOKEN_SERVER_URL =
METADATA_SERVER_URL + "/computeMetadata/v1/instance/service-accounts/default/token";

static final JsonFactory JSON_FACTORY = new JacksonFactory();
static final JsonFactory JSON_FACTORY = new GsonFactory();

String accessToken;

Expand Down
Expand Up @@ -17,7 +17,7 @@
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.Beta;

/**
Expand All @@ -39,7 +39,7 @@ private static class JsonFactoryInstanceHolder {
// other APIs to not require one of these for input. This was the most commonly used

This comment has been minimized.

Copy link
@Capstan

Capstan Jun 18, 2022

This comment is out of date now. Certainly we provide Gson as the default, but I don't understand what dependency detection this ever did.

// implementation in public samples. This is a compile-time dependency to help detect the
// dependency as early as possible.
static final JsonFactory INSTANCE = new JacksonFactory();
static final JsonFactory INSTANCE = new GsonFactory();

This comment has been minimized.

Copy link
@Capstan

Capstan Jun 18, 2022

One thing I am noticing in the monorepo when essentially manually patching this in is that it causes a bunch of test failures for clients who expect GoogleCredential.fromStream to only throw an IOException when given bad input. GsonFactory will instead throw IllegalArgumentException and that changes catch expected exception tests, or catch logic. I can only expect the wider world will have more of these. googleapis/google-http-java-client#1353

}

/** Returns a cached default implementation of the HttpTransport interface. */
Expand Down
Expand Up @@ -23,7 +23,7 @@
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import java.io.File;
Expand All @@ -45,7 +45,7 @@
/** Tests {@link DefaultCredentialProvider}. */
public class DefaultCredentialProviderTest extends TestCase {

private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final JsonFactory JSON_FACTORY = new GsonFactory();

private static final Collection<String> SCOPES =
Collections.unmodifiableCollection(Arrays.asList("scope1", "scope2"));
Expand Down
Expand Up @@ -14,7 +14,7 @@

package com.google.api.client.googleapis.auth.oauth2;

import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.common.collect.ImmutableList;
import junit.framework.TestCase;
Expand All @@ -33,7 +33,7 @@ public void testBuilder() {
GoogleAuthorizationCodeFlow.Builder builder =
new GoogleAuthorizationCodeFlow.Builder(
new MockHttpTransport(),
new JacksonFactory(),
new GsonFactory(),
CLIENT_ID,
CLIENT_SECRET,
ImmutableList.of("https://www.googleapis.com/auth/userinfo.email"));
Expand Down
Expand Up @@ -15,7 +15,7 @@
package com.google.api.client.googleapis.auth.oauth2;

import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.testing.http.MockHttpTransport;
import junit.framework.TestCase;

Expand All @@ -35,7 +35,7 @@ public void test() {
GoogleAuthorizationCodeTokenRequest request =
new GoogleAuthorizationCodeTokenRequest(
new MockHttpTransport(),
new JacksonFactory(),
new GsonFactory(),
CLIENT_ID,
CLIENT_SECRET,
CODE,
Expand Down
Expand Up @@ -18,7 +18,7 @@
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.util.SecurityTestUtils;
import java.io.ByteArrayInputStream;
Expand All @@ -36,7 +36,7 @@
*/
public class GoogleCredentialTest extends TestCase {

private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final JsonFactory JSON_FACTORY = new GsonFactory();

private static final Collection<String> SCOPES =
Collections.unmodifiableCollection(Arrays.asList("scope1", "scope2"));
Expand Down
Expand Up @@ -16,7 +16,7 @@

import com.google.api.client.auth.openidconnect.IdTokenVerifier;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebSignature.Header;
import com.google.api.client.testing.http.FixedClock;
import com.google.api.client.util.Clock;
Expand Down Expand Up @@ -52,7 +52,7 @@ public void testBuilder() throws Exception {
GoogleIdTokenVerifier.Builder builder =
new GoogleIdTokenVerifier.Builder(
new GooglePublicKeysManagerTest.PublicCertsMockHttpTransport(),
new JacksonFactory())
new GsonFactory())
.setIssuer(ISSUER)
.setAudience(TRUSTED_CLIENT_IDS);
assertEquals(Clock.SYSTEM, builder.getClock());
Expand All @@ -71,7 +71,7 @@ public void testVerify() throws Exception {
GoogleIdTokenVerifier verifier =
new GoogleIdTokenVerifier.Builder(
new GooglePublicKeysManagerTest.PublicCertsMockHttpTransport(),
new JacksonFactory())
new GsonFactory())
.build();
Header header = new Header();
header.setAlgorithm("RS25");
Expand All @@ -83,7 +83,7 @@ public void testVerify() throws Exception {
assertFalse(verifier.verify(idToken2));
verifier =
new GoogleIdTokenVerifier(
new GooglePublicKeysManagerTest.PublicCertsMockHttpTransport(), new JacksonFactory());
new GooglePublicKeysManagerTest.PublicCertsMockHttpTransport(), new GsonFactory());
assertFalse(verifier.verify(idToken));
assertFalse(verifier.verify(idToken2));
// TODO(yanivi): add a unit test that returns true
Expand Down
Expand Up @@ -20,7 +20,7 @@
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.json.Json;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.testing.http.FixedClock;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
Expand Down Expand Up @@ -68,7 +68,7 @@ public class GooglePublicKeysManagerTest extends TestCase {

public void testBuilder() throws Exception {
HttpTransport transport = new MockHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
JsonFactory jsonFactory = new GsonFactory();
GooglePublicKeysManager.Builder builder =
new GooglePublicKeysManager.Builder(transport, jsonFactory);

Expand Down Expand Up @@ -102,7 +102,7 @@ public LowLevelHttpResponse execute() {
public void testRefresh() throws Exception {
GooglePublicKeysManager certs =
new GooglePublicKeysManager.Builder(
new PublicCertsMockHttpTransport(), new JacksonFactory())
new PublicCertsMockHttpTransport(), new GsonFactory())
.build();
certs.refresh();
assertEquals(2, certs.getPublicKeys().size());
Expand All @@ -112,7 +112,7 @@ public void testLoadCerts_cache() throws Exception {
PublicCertsMockHttpTransport transport = new PublicCertsMockHttpTransport();
transport.useAgeHeader = true;
GooglePublicKeysManager certs =
new GooglePublicKeysManager.Builder(transport, new JacksonFactory())
new GooglePublicKeysManager.Builder(transport, new GsonFactory())
.setClock(new FixedClock(100))
.build();

Expand All @@ -123,7 +123,7 @@ public void testLoadCerts_cache() throws Exception {

public void testGetCacheTimeInSec() throws Exception {
GooglePublicKeysManager certs =
new GooglePublicKeysManager.Builder(new MockHttpTransport(), new JacksonFactory()).build();
new GooglePublicKeysManager.Builder(new MockHttpTransport(), new GsonFactory()).build();
assertEquals(
12000,
certs.getCacheTimeInSec(
Expand Down
Expand Up @@ -25,7 +25,7 @@
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonObjectParser;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.protobuf.ProtoObjectParser;
import com.google.api.client.testing.http.HttpTesting;
import com.google.api.client.testing.http.MockHttpTransport;
Expand Down Expand Up @@ -527,7 +527,7 @@ private BatchRequest getBatchPopulatedWithRequests(
credential = new MockCredential();

ObjectParser parser =
testBinary ? new ProtoObjectParser() : new JsonObjectParser(new JacksonFactory());
testBinary ? new ProtoObjectParser() : new JsonObjectParser(new GsonFactory());
BatchRequest batchRequest =
new BatchRequest(transport, credential).setBatchUrl(new GenericUrl(TEST_BATCH_URL));
HttpRequest request1 = jsonHttpRequest1.buildHttpRequest();
Expand Down Expand Up @@ -629,7 +629,7 @@ public void subTestExecuteWithVoidCallback(boolean testServerError) throws IOExc
new MockGoogleClientRequest<String>(client, METHOD1, URI_TEMPLATE1, null, String.class);
MockGoogleClientRequest<String> jsonHttpRequest2 =
new MockGoogleClientRequest<String>(client, METHOD2, URI_TEMPLATE2, null, String.class);
ObjectParser parser = new JsonObjectParser(new JacksonFactory());
ObjectParser parser = new JsonObjectParser(new GsonFactory());
BatchRequest batchRequest =
new BatchRequest(transport, null).setBatchUrl(new GenericUrl(TEST_BATCH_URL));
HttpRequest request1 = jsonHttpRequest1.buildHttpRequest();
Expand Down
Expand Up @@ -16,7 +16,7 @@

import com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.gson.GsonFactory;
import junit.framework.TestCase;

/**
Expand All @@ -32,7 +32,7 @@ public void testExecuteRefreshToken() throws Exception {

HttpTransport transport = new MockMetadataServerTransport(ACCESS_TOKEN);

ComputeCredential credential = new ComputeCredential(transport, new JacksonFactory());
ComputeCredential credential = new ComputeCredential(transport, new GsonFactory());

assertTrue(credential.refreshToken());
assertEquals(ACCESS_TOKEN, credential.getAccessToken());
Expand Down

0 comments on commit 63ed2ca

Please sign in to comment.