diff --git a/src/main/java/com/twilio/http/TwilioRestClient.java b/src/main/java/com/twilio/http/TwilioRestClient.java index d956d5b49..b76240d14 100644 --- a/src/main/java/com/twilio/http/TwilioRestClient.java +++ b/src/main/java/com/twilio/http/TwilioRestClient.java @@ -51,14 +51,8 @@ protected TwilioRestClient(Builder b) { this.region = b.region; this.edge = b.edge; this.httpClient = b.httpClient; - this.objectMapper = new ObjectMapper(); + this.objectMapper = b.objectMapper; this.userAgentExtensions = b.userAgentExtensions; - - // This module configures the ObjectMapper to use - // public API methods for manipulating java.time.* - // classes. The alternative is to use reflection which - // generates warnings from the module system on Java 9+ - objectMapper.registerModule(new JavaTimeModule()); } /** @@ -71,7 +65,7 @@ public Response request(final Request request) { if (username != null && password != null) { request.setAuth(username, password); } else if (authStrategy != null) { - request.setAuth(authStrategy); + request.setAuth(authStrategy); } if (region != null) @@ -106,6 +100,13 @@ public Response request(final Request request) { } public static class Builder { + // This module configures the ObjectMapper to use + // public API methods for manipulating java.time.* + // classes. The alternative is to use reflection which + // generates warnings from the module system on Java 9+ + private static final ObjectMapper DEFAULT_OBJECT_MAPPER = new ObjectMapper() + .registerModule(new JavaTimeModule()); + private String username; private String password; private AuthStrategy authStrategy; @@ -114,6 +115,7 @@ public static class Builder { private String edge; private HttpClient httpClient; private List userAgentExtensions; + private ObjectMapper objectMapper = DEFAULT_OBJECT_MAPPER; /** * Create a new Twilio Rest Client. @@ -163,6 +165,11 @@ public Builder userAgentExtensions(final List userAgentExtensions) { return this; } + public Builder objectMapper(final ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + return this; + } + /** * Build new TwilioRestClient. * diff --git a/src/main/java/com/twilio/http/bearertoken/BearerTokenTwilioRestClient.java b/src/main/java/com/twilio/http/bearertoken/BearerTokenTwilioRestClient.java index 0562c7253..f3ba9fac5 100644 --- a/src/main/java/com/twilio/http/bearertoken/BearerTokenTwilioRestClient.java +++ b/src/main/java/com/twilio/http/bearertoken/BearerTokenTwilioRestClient.java @@ -44,23 +44,25 @@ private BearerTokenTwilioRestClient(BearerTokenTwilioRestClient.Builder b) { this.region = b.region; this.edge = b.edge; this.httpClient = b.httpClient; - this.objectMapper = new ObjectMapper(); + this.objectMapper = b.objectMapper; this.userAgentExtensions = b.userAgentExtensions; this.tokenManager = b.tokenManager; + } + public static class Builder { // This module configures the ObjectMapper to use // public API methods for manipulating java.time.* // classes. The alternative is to use reflection which // generates warnings from the module system on Java 9+ - objectMapper.registerModule(new JavaTimeModule()); - } - - public static class Builder { + private static final ObjectMapper DEFAULT_OBJECT_MAPPER = new ObjectMapper() + .registerModule(new JavaTimeModule()); + private String region; private String edge; private BearerTokenHttpClient httpClient; private List userAgentExtensions; private TokenManager tokenManager; + private ObjectMapper objectMapper = DEFAULT_OBJECT_MAPPER; public Builder() { this.region = System.getenv("TWILIO_REGION"); @@ -95,6 +97,11 @@ public BearerTokenTwilioRestClient.Builder userAgentExtensions(final List userAgentExtensions; + private ObjectMapper objectMapper = DEFAULT_OBJECT_MAPPER; public Builder() { this.region = System.getenv("TWILIO_REGION"); @@ -82,6 +84,11 @@ public NoAuthTwilioRestClient.Builder userAgentExtensions(final List use return this; } + public NoAuthTwilioRestClient.Builder objectMapper(final ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + return this; + } + public NoAuthTwilioRestClient build() { if (this.httpClient == null) { this.httpClient = new NoAuthNetworkHttpClient(); diff --git a/src/test/java/com/twilio/http/TwilioRestClientTest.java b/src/test/java/com/twilio/http/TwilioRestClientTest.java index 862d72add..b78ee6d92 100644 --- a/src/test/java/com/twilio/http/TwilioRestClientTest.java +++ b/src/test/java/com/twilio/http/TwilioRestClientTest.java @@ -1,18 +1,19 @@ package com.twilio.http; -import com.twilio.rest.Domains; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.twilio.rest.Domains; import java.util.Arrays; import java.util.Collections; import java.util.List; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; import static org.mockito.Mockito.when; +import org.mockito.MockitoAnnotations; public class TwilioRestClientTest { private TwilioRestClient twilioRestClient; @@ -48,6 +49,20 @@ public void testRequest() { assertNotNull(resp); } + @Test + public void testRequestWithCustomObjectMapper() { + Request request = new Request( + HttpMethod.GET, + Domains.API.toString(), + URI + ); + twilioRestClientExtension = new TwilioRestClient.Builder(USER_NAME, TOKEN) + .objectMapper(new ObjectMapper().registerModule(new JavaTimeModule())) + .build(); + twilioRestClientExtension.request(request); + assertEquals(userAgentStringExtensions, request.getUserAgentExtensions()); + } + @Test public void testRequestWithExtension() { Request request = new Request(