[java][jersey] Remove double brace initialization and some more jersey cleanup (#14783)

* Remove double brace initialization

* Use diamond operator

* Less clutter in generated api classes

* Optimize isJsonMime

* Revert change in escapeString

Skip toString() on string parameters.

* Fix edge-cases

A ServerVariable without enumValues and/or operationServers
with any ServerVariables would lead to invalid code.
This commit is contained in:
Robin Karlsson
2023-03-13 09:42:37 +01:00
committed by GitHub
parent 9a53625fcc
commit 564939a4a8
131 changed files with 2746 additions and 6705 deletions

View File

@@ -35,6 +35,7 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.glassfish.jersey.logging.LoggingFeature;
import java.util.AbstractMap.SimpleEntry;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Collection;
@@ -42,11 +43,14 @@ import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.time.OffsetDateTime;
import java.net.URLEncoder;
@@ -68,63 +72,58 @@ import org.openapitools.client.auth.ApiKeyAuth;
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ApiClient extends JavaTimeFormatter {
protected Map<String, String> defaultHeaderMap = new HashMap<String, String>();
protected Map<String, String> defaultCookieMap = new HashMap<String, String>();
private static final Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$");
protected Map<String, String> defaultHeaderMap = new HashMap<>();
protected Map<String, String> defaultCookieMap = new HashMap<>();
protected String basePath = "http://petstore.swagger.io:80/v2";
protected String userAgent;
private static final Logger log = Logger.getLogger(ApiClient.class.getName());
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(Arrays.asList(
new ServerConfiguration(
"http://{server}.swagger.io:{port}/v2",
"petstore server",
new HashMap<String, ServerVariable>() {{
put("server", new ServerVariable(
"No description provided",
"petstore",
new HashSet<String>(
Arrays.asList(
"petstore",
"qa-petstore",
"dev-petstore"
)
protected List<ServerConfiguration> servers = new ArrayList<>(Arrays.asList(
new ServerConfiguration(
"http://{server}.swagger.io:{port}/v2",
"petstore server",
Stream.<Entry<String, ServerVariable>>of(
new SimpleEntry<>("server", new ServerVariable(
"No description provided",
"petstore",
new LinkedHashSet<>(Arrays.asList(
"petstore",
"qa-petstore",
"dev-petstore"
))
)),
new SimpleEntry<>("port", new ServerVariable(
"No description provided",
"80",
new LinkedHashSet<>(Arrays.asList(
"80",
"8080"
))
))
).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (a, b) -> a, LinkedHashMap::new))
),
new ServerConfiguration(
"https://localhost:8080/{version}",
"The local server",
Stream.<Entry<String, ServerVariable>>of(
new SimpleEntry<>("version", new ServerVariable(
"No description provided",
"v2",
new LinkedHashSet<>(Arrays.asList(
"v1",
"v2"
))
))
).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (a, b) -> a, LinkedHashMap::new))
)
));
put("port", new ServerVariable(
"No description provided",
"80",
new HashSet<String>(
Arrays.asList(
"80",
"8080"
)
)
));
}}
),
new ServerConfiguration(
"https://localhost:8080/{version}",
"The local server",
new HashMap<String, ServerVariable>() {{
put("version", new ServerVariable(
"No description provided",
"v2",
new HashSet<String>(
Arrays.asList(
"v1",
"v2"
)
)
));
}}
)
));
protected Integer serverIndex = 0;
protected Map<String, String> serverVariables = null;
protected Map<String, List<ServerConfiguration>> operationServers = new HashMap<String, List<ServerConfiguration>>() {{
}};
protected Map<String, Integer> operationServerIndex = new HashMap<String, Integer>();
protected Map<String, Map<String, String>> operationServerVariables = new HashMap<String, Map<String, String>>();
protected Map<String, List<ServerConfiguration>> operationServers = new HashMap<>();
protected Map<String, Integer> operationServerIndex = new HashMap<>();
protected Map<String, Map<String, String>> operationServerVariables = new HashMap<>();
protected boolean debugging = false;
protected ClientConfig clientConfig;
protected int connectionTimeout = 0;
@@ -161,7 +160,7 @@ public class ApiClient extends JavaTimeFormatter {
setUserAgent("OpenAPI-Generator/1.0.0/java");
// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();
authentications = new HashMap<>();
Authentication auth = null;
if (authMap != null) {
auth = authMap.get("api_key");
@@ -183,7 +182,7 @@ public class ApiClient extends JavaTimeFormatter {
authentications = Collections.unmodifiableMap(authentications);
// Setup authentication lookup (key: authentication alias, value: authentication name)
authenticationLookup = new HashMap<String, String>();
authenticationLookup = new HashMap<>();
authenticationLookup.put("api_key_query", "api_key");
}
@@ -665,7 +664,7 @@ public class ApiClient extends JavaTimeFormatter {
* @return List of pairs
*/
public List<Pair> parameterToPairs(String collectionFormat, String name, Object value){
List<Pair> params = new ArrayList<Pair>();
List<Pair> params = new ArrayList<>();
// preconditions
if (name == null || name.isEmpty() || value == null) return params;
@@ -724,14 +723,13 @@ public class ApiClient extends JavaTimeFormatter {
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* "* / *" is also default to JSON
* "*{@literal /}*" is also considered JSON by this method.
*
* @param mime MIME
* @return True if the MIME type is JSON
*/
public boolean isJsonMime(String mime) {
String jsonMime = "(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$";
return mime != null && (mime.matches(jsonMime) || mime.equals("*/*"));
return mime != null && (mime.equals("*/*") || JSON_MIME_PATTERN.matcher(mime).matches());
}
/**
@@ -743,8 +741,8 @@ public class ApiClient extends JavaTimeFormatter {
* @return The Accept header to use. If the given array is empty,
* null will be returned (not to set the Accept header explicitly).
*/
public String selectHeaderAccept(String[] accepts) {
if (accepts.length == 0) {
public String selectHeaderAccept(String... accepts) {
if (accepts == null || accepts.length == 0) {
return null;
}
for (String accept : accepts) {
@@ -764,8 +762,8 @@ public class ApiClient extends JavaTimeFormatter {
* @return The Content-Type header to use. If the given array is empty,
* JSON will be used.
*/
public String selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0) {
public String selectHeaderContentType(String... contentTypes) {
if (contentTypes == null || contentTypes.length == 0) {
return "application/json";
}
for (String contentType : contentTypes) {
@@ -1057,15 +1055,17 @@ public class ApiClient extends JavaTimeFormatter {
Map<String, String> allHeaderParams = new HashMap<>(defaultHeaderMap);
allHeaderParams.putAll(headerParams);
// update different parameters (e.g. headers) for authentication
updateParamsForAuth(
authNames,
queryParams,
allHeaderParams,
cookieParams,
null,
method,
target.getUri());
if (authNames != null) {
// update different parameters (e.g. headers) for authentication
updateParamsForAuth(
authNames,
queryParams,
allHeaderParams,
cookieParams,
null,
method,
target.getUri());
}
for (Entry<String, String> entry : allHeaderParams.entrySet()) {
String value = entry.getValue();
@@ -1237,10 +1237,10 @@ public class ApiClient extends JavaTimeFormatter {
* @return a {@link java.util.Map} of response headers.
*/
protected Map<String, List<String>> buildResponseHeaders(Response response) {
Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
Map<String, List<String>> responseHeaders = new HashMap<>();
for (Entry<String, List<Object>> entry: response.getHeaders().entrySet()) {
List<Object> values = entry.getValue();
List<String> headers = new ArrayList<String>();
List<String> headers = new ArrayList<>();
for (Object o : values) {
headers.add(String.valueOf(o));
}

View File

@@ -63,7 +63,7 @@ public class JSON implements ContextResolver<ObjectMapper> {
public static Class<?> getClassForElement(JsonNode node, Class<?> modelClass) {
ClassDiscriminatorMapping cdm = modelDiscriminators.get(modelClass);
if (cdm != null) {
return cdm.getClassForElement(node, new HashSet<Class<?>>());
return cdm.getClassForElement(node, new HashSet<>());
}
return null;
}
@@ -83,7 +83,7 @@ public class JSON implements ContextResolver<ObjectMapper> {
ClassDiscriminatorMapping(Class<?> cls, String propertyName, Map<String, Class<?>> mappings) {
modelClass = cls;
discriminatorName = propertyName;
discriminatorMappings = new HashMap<String, Class<?>>();
discriminatorMappings = new HashMap<>();
if (mappings != null) {
discriminatorMappings.putAll(mappings);
}
@@ -192,12 +192,12 @@ public class JSON implements ContextResolver<ObjectMapper> {
/**
* A map of discriminators for all model classes.
*/
private static Map<Class<?>, ClassDiscriminatorMapping> modelDiscriminators = new HashMap<Class<?>, ClassDiscriminatorMapping>();
private static Map<Class<?>, ClassDiscriminatorMapping> modelDiscriminators = new HashMap<>();
/**
* A map of oneOf/anyOf descendants for each model class.
*/
private static Map<Class<?>, Map<String, GenericType>> modelDescendants = new HashMap<Class<?>, Map<String, GenericType>>();
private static Map<Class<?>, Map<String, GenericType>> modelDescendants = new HashMap<>();
/**
* Register a model class discriminator.

View File

@@ -11,6 +11,7 @@ import javax.ws.rs.core.GenericType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -71,37 +72,12 @@ public class UsageApi {
</table>
*/
public ApiResponse<Object> anyKeyWithHttpInfo() throws ApiException {
Object localVarPostBody = null;
// create path and map variables
String localVarPath = "/any";
// query params
List<Pair> localVarQueryParams = new ArrayList<Pair>();
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
final String[] localVarAccepts = {
"application/json"
};
final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = {
};
final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
String[] localVarAuthNames = new String[] { "api_key", "api_key_query" };
String localVarAccept = apiClient.selectHeaderAccept("application/json");
String localVarContentType = apiClient.selectHeaderContentType();
String[] localVarAuthNames = new String[] {"api_key", "api_key_query"};
GenericType<Object> localVarReturnType = new GenericType<Object>() {};
return apiClient.invokeAPI("UsageApi.anyKey", localVarPath, "GET", localVarQueryParams, localVarPostBody,
localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType,
return apiClient.invokeAPI("UsageApi.anyKey", "/any", "GET", new ArrayList<>(), null,
new LinkedHashMap<>(), new LinkedHashMap<>(), new LinkedHashMap<>(), localVarAccept, localVarContentType,
localVarAuthNames, localVarReturnType, false);
}
/**
@@ -131,37 +107,12 @@ public class UsageApi {
</table>
*/
public ApiResponse<Object> bothKeysWithHttpInfo() throws ApiException {
Object localVarPostBody = null;
// create path and map variables
String localVarPath = "/both";
// query params
List<Pair> localVarQueryParams = new ArrayList<Pair>();
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
final String[] localVarAccepts = {
"application/json"
};
final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = {
};
final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
String[] localVarAuthNames = new String[] { "api_key", "api_key_query" };
String localVarAccept = apiClient.selectHeaderAccept("application/json");
String localVarContentType = apiClient.selectHeaderContentType();
String[] localVarAuthNames = new String[] {"api_key", "api_key_query"};
GenericType<Object> localVarReturnType = new GenericType<Object>() {};
return apiClient.invokeAPI("UsageApi.bothKeys", localVarPath, "GET", localVarQueryParams, localVarPostBody,
localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType,
return apiClient.invokeAPI("UsageApi.bothKeys", "/both", "GET", new ArrayList<>(), null,
new LinkedHashMap<>(), new LinkedHashMap<>(), new LinkedHashMap<>(), localVarAccept, localVarContentType,
localVarAuthNames, localVarReturnType, false);
}
/**
@@ -191,37 +142,12 @@ public class UsageApi {
</table>
*/
public ApiResponse<Object> keyInHeaderWithHttpInfo() throws ApiException {
Object localVarPostBody = null;
// create path and map variables
String localVarPath = "/header";
// query params
List<Pair> localVarQueryParams = new ArrayList<Pair>();
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
final String[] localVarAccepts = {
"application/json"
};
final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = {
};
final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
String[] localVarAuthNames = new String[] { "api_key" };
String localVarAccept = apiClient.selectHeaderAccept("application/json");
String localVarContentType = apiClient.selectHeaderContentType();
String[] localVarAuthNames = new String[] {"api_key"};
GenericType<Object> localVarReturnType = new GenericType<Object>() {};
return apiClient.invokeAPI("UsageApi.keyInHeader", localVarPath, "GET", localVarQueryParams, localVarPostBody,
localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType,
return apiClient.invokeAPI("UsageApi.keyInHeader", "/header", "GET", new ArrayList<>(), null,
new LinkedHashMap<>(), new LinkedHashMap<>(), new LinkedHashMap<>(), localVarAccept, localVarContentType,
localVarAuthNames, localVarReturnType, false);
}
/**
@@ -251,37 +177,12 @@ public class UsageApi {
</table>
*/
public ApiResponse<Object> keyInQueryWithHttpInfo() throws ApiException {
Object localVarPostBody = null;
// create path and map variables
String localVarPath = "/query";
// query params
List<Pair> localVarQueryParams = new ArrayList<Pair>();
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
final String[] localVarAccepts = {
"application/json"
};
final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = {
};
final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
String[] localVarAuthNames = new String[] { "api_key_query" };
String localVarAccept = apiClient.selectHeaderAccept("application/json");
String localVarContentType = apiClient.selectHeaderContentType();
String[] localVarAuthNames = new String[] {"api_key_query"};
GenericType<Object> localVarReturnType = new GenericType<Object>() {};
return apiClient.invokeAPI("UsageApi.keyInQuery", localVarPath, "GET", localVarQueryParams, localVarPostBody,
localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType,
return apiClient.invokeAPI("UsageApi.keyInQuery", "/query", "GET", new ArrayList<>(), null,
new LinkedHashMap<>(), new LinkedHashMap<>(), new LinkedHashMap<>(), localVarAccept, localVarContentType,
localVarAuthNames, localVarReturnType, false);
}
}