Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;

import javax.annotation.Nullable;

{{#jsr310}}
{{#threetenbp}}
import org.threeten.bp.OffsetDateTime;
Expand Down Expand Up @@ -98,45 +101,58 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {

private final WebClient webClient;
private final DateFormat dateFormat;
private final ObjectMapper objectMapper;

private Map<String, Authentication> authentications;


public ApiClient() {
this.dateFormat = createDefaultDateFormat();
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(dateFormat);
mapper.registerModule(new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
{{#openApiNullable}}
JsonNullableModule jnm = new JsonNullableModule();
mapper.registerModule(jnm);
{{/openApiNullable}}

this.webClient = buildWebClient(mapper);
this.objectMapper = createDefaultObjectMapper(this.dateFormat);
this.webClient = buildWebClient(this.objectMapper);
this.init();
}

public ApiClient(WebClient webClient) {
this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient()), createDefaultDateFormat());
}

public ApiClient(ObjectMapper mapper, DateFormat format) {
this(buildWebClient(mapper.copy()), format);
}

public ApiClient(WebClient webClient, ObjectMapper mapper, DateFormat format) {
this(Optional.ofNullable(webClient).orElseGet(() ->buildWebClient(mapper.copy())), format);
this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient(mapper.copy())), format);
}

private ApiClient(WebClient webClient, DateFormat format) {
this.webClient = webClient;
this.dateFormat = format;
this.objectMapper = createDefaultObjectMapper(format);
this.init();
}

public DateFormat createDefaultDateFormat() {
public static DateFormat createDefaultDateFormat() {
DateFormat dateFormat = new RFC3339DateFormat();
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat;
}

public static ObjectMapper createDefaultObjectMapper(@Nullable DateFormat dateFormat) {
if (null == dateFormat) {
dateFormat = createDefaultDateFormat();
}
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(dateFormat);
mapper.registerModule(new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
{{#openApiNullable}}
JsonNullableModule jnm = new JsonNullableModule();
mapper.registerModule(jnm);
{{/openApiNullable}}
return mapper;
}

protected void init() {
// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
Expand All @@ -149,20 +165,45 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
}

/**
* Build the WebClient used to make HTTP requests.
* Build the WebClientBuilder used to make WebClient.
* @param mapper ObjectMapper used for serialize/deserialize
* @return WebClient
*/
public static WebClient buildWebClient(ObjectMapper mapper) {
public static WebClient.Builder buildWebClientBuilder(ObjectMapper mapper) {
ExchangeStrategies strategies = ExchangeStrategies
.builder()
.codecs(clientDefaultCodecsConfigurer -> {
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper, MediaType.APPLICATION_JSON));
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper, MediaType.APPLICATION_JSON));
}).build();
WebClient.Builder webClient = WebClient.builder().exchangeStrategies(strategies);
return webClient.build();
WebClient.Builder webClientBuilder = WebClient.builder().exchangeStrategies(strategies);
return webClientBuilder;
}

/**
* Build the WebClientBuilder used to make WebClient.
* @return WebClient
*/
public static WebClient.Builder buildWebClientBuilder() {
return buildWebClientBuilder(createDefaultObjectMapper(null));
}

/**
* Build the WebClient used to make HTTP requests.
* @param mapper ObjectMapper used for serialize/deserialize
* @return WebClient
*/
public static WebClient buildWebClient(ObjectMapper mapper) {
Comment thread
UnleashSpirit marked this conversation as resolved.
return buildWebClientBuilder(mapper).build();
}

/**
* Build the WebClient used to make HTTP requests.
* @return WebClient
*/
public static WebClient buildWebClient() {
return buildWebClientBuilder(createDefaultObjectMapper(null)).build();
}

/**
* Get the current base path
Expand Down Expand Up @@ -352,6 +393,22 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
return dateFormat.format(date);
}

/**
* Get the ObjectMapper used to make HTTP requests.
* @return ObjectMapper objectMapper
*/
public ObjectMapper getObjectMapper() {
return objectMapper;
}

/**
* Get the WebClient used to make HTTP requests.
* @return WebClient webClient
*/
public WebClient getWebClient() {
return webClient;
}

/**
* Format the given parameter object into string.
* @param param the object to convert
Expand Down Expand Up @@ -655,10 +712,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
}

// collectionFormat is assumed to be "csv" by default
if(collectionFormat == null) {
collectionFormat = CollectionFormat.CSV;
}
if(collectionFormat == null) {
collectionFormat = CollectionFormat.CSV;
}

return collectionFormat.collectionToString(values);
return collectionFormat.collectionToString(values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;

import javax.annotation.Nullable;

import java.time.OffsetDateTime;

import org.openapitools.client.auth.Authentication;
Expand Down Expand Up @@ -87,43 +90,56 @@ private String collectionToString(Collection<?> collection) {

private final WebClient webClient;
private final DateFormat dateFormat;
private final ObjectMapper objectMapper;

private Map<String, Authentication> authentications;


public ApiClient() {
this.dateFormat = createDefaultDateFormat();
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(dateFormat);
mapper.registerModule(new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNullableModule jnm = new JsonNullableModule();
mapper.registerModule(jnm);

this.webClient = buildWebClient(mapper);
this.objectMapper = createDefaultObjectMapper(this.dateFormat);
this.webClient = buildWebClient(this.objectMapper);
this.init();
}

public ApiClient(WebClient webClient) {
this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient()), createDefaultDateFormat());
}

public ApiClient(ObjectMapper mapper, DateFormat format) {
this(buildWebClient(mapper.copy()), format);
}

public ApiClient(WebClient webClient, ObjectMapper mapper, DateFormat format) {
this(Optional.ofNullable(webClient).orElseGet(() ->buildWebClient(mapper.copy())), format);
this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient(mapper.copy())), format);
}

private ApiClient(WebClient webClient, DateFormat format) {
this.webClient = webClient;
this.dateFormat = format;
this.objectMapper = createDefaultObjectMapper(format);
this.init();
}

public DateFormat createDefaultDateFormat() {
public static DateFormat createDefaultDateFormat() {
DateFormat dateFormat = new RFC3339DateFormat();
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat;
}

public static ObjectMapper createDefaultObjectMapper(@Nullable DateFormat dateFormat) {
if (null == dateFormat) {
dateFormat = createDefaultDateFormat();
}
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(dateFormat);
mapper.registerModule(new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNullableModule jnm = new JsonNullableModule();
mapper.registerModule(jnm);
return mapper;
}

protected void init() {
// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();
Expand All @@ -136,20 +152,43 @@ protected void init() {
}

/**
* Build the WebClient used to make HTTP requests.
* Build the WebClientBuilder used to make WebClient.
* @return WebClient
*/
public static WebClient buildWebClient(ObjectMapper mapper) {
public static WebClient.Builder buildWebClientBuilder(ObjectMapper mapper) {
ExchangeStrategies strategies = ExchangeStrategies
.builder()
.codecs(clientDefaultCodecsConfigurer -> {
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper, MediaType.APPLICATION_JSON));
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper, MediaType.APPLICATION_JSON));
}).build();
WebClient.Builder webClient = WebClient.builder().exchangeStrategies(strategies);
return webClient.build();
WebClient.Builder webClientBuilder = WebClient.builder().exchangeStrategies(strategies);
return webClientBuilder;
}

/**
* Build the WebClientBuilder used to make WebClient.
* @return WebClient
*/
public static WebClient.Builder buildWebClientBuilder() {
return buildWebClientBuilder(createDefaultObjectMapper(null));
}

/**
* Build the WebClient used to make HTTP requests.
* @return WebClient
*/
public static WebClient buildWebClient(ObjectMapper mapper) {
return buildWebClientBuilder(mapper).build();
}

/**
* Build the WebClient used to make HTTP requests.
* @return WebClient
*/
public static WebClient buildWebClient() {
return buildWebClientBuilder(createDefaultObjectMapper(null)).build();
}

/**
* Get the current base path
Expand Down Expand Up @@ -337,6 +376,22 @@ public String formatDate(Date date) {
return dateFormat.format(date);
}

/**
* Get the ObjectMapper used to make HTTP requests.
* @return ObjectMapper objectMapper
*/
public ObjectMapper getObjectMapper() {
return objectMapper;
}

/**
* Get the WebClient used to make HTTP requests.
* @return WebClient webClient
*/
public WebClient getWebClient() {
return webClient;
}

/**
* Format the given parameter object into string.
* @param param the object to convert
Expand Down Expand Up @@ -640,10 +695,10 @@ public String collectionPathParameterToString(CollectionFormat collectionFormat,
}

// collectionFormat is assumed to be "csv" by default
if(collectionFormat == null) {
collectionFormat = CollectionFormat.CSV;
}
if(collectionFormat == null) {
collectionFormat = CollectionFormat.CSV;
}

return collectionFormat.collectionToString(values);
return collectionFormat.collectionToString(values);
}
}