Skip to content

feat: Support custom ObjectMappers, with singleton defaults #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/main/java/com/twilio/http/TwilioRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -114,6 +115,7 @@ public static class Builder {
private String edge;
private HttpClient httpClient;
private List<String> userAgentExtensions;
private ObjectMapper objectMapper = DEFAULT_OBJECT_MAPPER;

/**
* Create a new Twilio Rest Client.
Expand Down Expand Up @@ -163,6 +165,11 @@ public Builder userAgentExtensions(final List<String> userAgentExtensions) {
return this;
}

public Builder objectMapper(final ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return this;
}

/**
* Build new TwilioRestClient.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> userAgentExtensions;
private TokenManager tokenManager;
private ObjectMapper objectMapper = DEFAULT_OBJECT_MAPPER;

public Builder() {
this.region = System.getenv("TWILIO_REGION");
Expand Down Expand Up @@ -95,6 +97,11 @@ public BearerTokenTwilioRestClient.Builder userAgentExtensions(final List<String
return this;
}

public BearerTokenTwilioRestClient.Builder objectMapper(final ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return this;
}

public BearerTokenTwilioRestClient build() {
if (this.httpClient == null) {
this.httpClient = new BearerTokenNetworkHttpClient();
Expand All @@ -111,7 +118,7 @@ public Response request(BearerTokenRequest request) {
}
}
}

request.setAuth(accessToken);
if (region != null)
request.setRegion(region);
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/com/twilio/http/noauth/NoAuthTwilioRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@ private NoAuthTwilioRestClient(NoAuthTwilioRestClient.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;
}

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 NoAuthNetworkHttpClient httpClient;
private List<String> userAgentExtensions;
private ObjectMapper objectMapper = DEFAULT_OBJECT_MAPPER;

public Builder() {
this.region = System.getenv("TWILIO_REGION");
Expand Down Expand Up @@ -82,6 +84,11 @@ public NoAuthTwilioRestClient.Builder userAgentExtensions(final List<String> 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();
Expand Down
27 changes: 21 additions & 6 deletions src/test/java/com/twilio/http/TwilioRestClientTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Loading