4040import org .asynchttpclient .Response ;
4141import org .asynchttpclient .request .body .multipart .FilePart ;
4242import org .asynchttpclient .request .body .multipart .StringPart ;
43+ import org .jetbrains .annotations .VisibleForTesting ;
4344import org .slf4j .Logger ;
4445import org .slf4j .LoggerFactory ;
4546import org .zendesk .client .v2 .model .AgentRole ;
@@ -165,7 +166,8 @@ private Zendesk(
165166 String username ,
166167 String password ,
167168 Map <String , String > headers ,
168- int cbpPageSize ) {
169+ int cbpPageSize ,
170+ Function <ObjectMapper , ObjectMapper > objectMapperCustomizer ) {
169171 this .logger = LoggerFactory .getLogger (Zendesk .class );
170172 this .closeClient = client == null ;
171173 this .oauthToken = null ;
@@ -188,15 +190,16 @@ private Zendesk(
188190 headers .putIfAbsent (USER_AGENT_HEADER , new DefaultUserAgent ().toString ());
189191 this .headers = Collections .unmodifiableMap (headers );
190192 this .cbpPageSize = cbpPageSize ;
191- this .mapper = createMapper ();
193+ this .mapper = createMapper (objectMapperCustomizer );
192194 }
193195
194196 private Zendesk (
195197 AsyncHttpClient client ,
196198 String url ,
197199 String oauthToken ,
198200 Map <String , String > headers ,
199- int cbpPageSize ) {
201+ int cbpPageSize ,
202+ Function <ObjectMapper , ObjectMapper > objectMapperCustomizer ) {
200203 this .logger = LoggerFactory .getLogger (Zendesk .class );
201204 this .closeClient = client == null ;
202205 this .realm = null ;
@@ -212,7 +215,7 @@ private Zendesk(
212215 headers .putIfAbsent (USER_AGENT_HEADER , new DefaultUserAgent ().toString ());
213216 this .headers = Collections .unmodifiableMap (headers );
214217 this .cbpPageSize = cbpPageSize ;
215- this .mapper = createMapper ();
218+ this .mapper = createMapper (objectMapperCustomizer );
216219 }
217220
218221 //////////////////////////////////////////////////////////////////////
@@ -4206,7 +4209,19 @@ private TemplateUri getSearchUri(Map<String, Object> params, String query, Strin
42064209 return templateUri ;
42074210 }
42084211
4212+ /**
4213+ * This method did not allow for any customization and its public visibility was likely only for
4214+ * tests. If you rely on it for some reason, please report a new issue to discuss options. To
4215+ * customize the ObjectMapper, use the {@link Builder#customizeObjectMapper(Function)} method.
4216+ */
4217+ @ Deprecated (since = "1.3.0" , forRemoval = true )
42094218 public static ObjectMapper createMapper () {
4219+ return createMapper (Function .identity ());
4220+ }
4221+
4222+ @ VisibleForTesting
4223+ public static ObjectMapper createMapper (
4224+ Function <ObjectMapper , ObjectMapper > objectMapperCustomizer ) {
42104225 ObjectMapper mapper = new ObjectMapper ();
42114226 mapper .enable (SerializationFeature .WRITE_ENUMS_USING_TO_STRING );
42124227 mapper .enable (DeserializationFeature .READ_ENUMS_USING_TO_STRING );
@@ -4215,6 +4230,11 @@ public static ObjectMapper createMapper() {
42154230 mapper .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS );
42164231 mapper .setDateFormat (new StdDateFormat ());
42174232 mapper .enable (DeserializationFeature .USE_LONG_FOR_INTS );
4233+ return objectMapperCustomizer .apply (mapper );
4234+ }
4235+
4236+ @ VisibleForTesting
4237+ ObjectMapper getMapper () {
42184238 return mapper ;
42194239 }
42204240
@@ -4279,11 +4299,13 @@ public static class Builder {
42794299 private String token = null ;
42804300 private String oauthToken = null ;
42814301 private int cbpPageSize = DEFAULT_CBP_PAGE_SIZE ;
4302+ private Function <ObjectMapper , ObjectMapper > objectMapperCustomizer ;
42824303 private final Map <String , String > headers ;
42834304
42844305 public Builder (String url ) {
42854306 this .url = url ;
42864307 this .headers = new HashMap <>();
4308+ objectMapperCustomizer = Function .identity ();
42874309 }
42884310
42894311 public Builder setClient (AsyncHttpClient client ) {
@@ -4339,13 +4361,30 @@ public Builder setCbpPageSize(int cbpPageSize) {
43394361 return this ;
43404362 }
43414363
4364+ /**
4365+ * Customize the ObjectMapper used by this Zendesk client. Careful, the customizer function will
4366+ * be applied after the default configuration for this library.
4367+ *
4368+ * @param customizer a function that takes an ObjectMapper and returns a customized ObjectMapper
4369+ * @return this builder instance
4370+ * @since 1.3.0
4371+ */
4372+ public Builder customizeObjectMapper (Function <ObjectMapper , ObjectMapper > customizer ) {
4373+ if (customizer != null ) {
4374+ this .objectMapperCustomizer = customizer ;
4375+ }
4376+ return this ;
4377+ }
4378+
43424379 public Zendesk build () {
43434380 if (token != null ) {
4344- return new Zendesk (client , url , username + "/token" , token , headers , cbpPageSize );
4381+ return new Zendesk (
4382+ client , url , username + "/token" , token , headers , cbpPageSize , objectMapperCustomizer );
43454383 } else if (oauthToken != null ) {
4346- return new Zendesk (client , url , oauthToken , headers , cbpPageSize );
4384+ return new Zendesk (client , url , oauthToken , headers , cbpPageSize , objectMapperCustomizer );
43474385 }
4348- return new Zendesk (client , url , username , password , headers , cbpPageSize );
4386+ return new Zendesk (
4387+ client , url , username , password , headers , cbpPageSize , objectMapperCustomizer );
43494388 }
43504389 }
43514390}
0 commit comments