Skip to content

Commit

Permalink
[java][okhttp-gson] minor improvements (OpenAPITools#13792)
Browse files Browse the repository at this point in the history
* use replace instead of replaceAll

* avoid using instance to accessd static methods

* use entrySet instead of keySet

* use statis class instead of instance for static method

* update samples
  • Loading branch information
wing328 authored Oct 22, 2022
1 parent 2e4a025 commit ca56242
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public class ApiClient {
* @return a {@link {{invokerPackage}}.ApiClient} object
*/
public ApiClient setDateFormat(DateFormat dateFormat) {
this.json.setDateFormat(dateFormat);
JSON.setDateFormat(dateFormat);
return this;
}

Expand All @@ -398,18 +398,18 @@ public class ApiClient {
* @return a {@link {{invokerPackage}}.ApiClient} object
*/
public ApiClient setSqlDateFormat(DateFormat dateFormat) {
this.json.setSqlDateFormat(dateFormat);
JSON.setSqlDateFormat(dateFormat);
return this;
}

{{#joda}}
public ApiClient setDateTimeFormat(DateTimeFormatter dateFormat) {
this.json.setDateTimeFormat(dateFormat);
JSON.setDateTimeFormat(dateFormat);
return this;
}

public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) {
this.json.setLocalDateFormat(dateFormat);
JSON.setLocalDateFormat(dateFormat);
return this;
}

Expand All @@ -422,7 +422,7 @@ public class ApiClient {
* @return a {@link {{invokerPackage}}.ApiClient} object
*/
public ApiClient setOffsetDateTimeFormat(DateTimeFormatter dateFormat) {
this.json.setOffsetDateTimeFormat(dateFormat);
JSON.setOffsetDateTimeFormat(dateFormat);
return this;
}

Expand All @@ -433,7 +433,7 @@ public class ApiClient {
* @return a {@link {{invokerPackage}}.ApiClient} object
*/
public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) {
this.json.setLocalDateFormat(dateFormat);
JSON.setLocalDateFormat(dateFormat);
return this;
}

Expand All @@ -445,7 +445,7 @@ public class ApiClient {
* @return a {@link {{invokerPackage}}.ApiClient} object
*/
public ApiClient setLenientOnJson(boolean lenientOnJson) {
this.json.setLenientOnJson(lenientOnJson);
JSON.setLenientOnJson(lenientOnJson);
return this;
}

Expand Down Expand Up @@ -745,7 +745,7 @@ public class ApiClient {
return "";
} else if (param instanceof Date {{#joda}}|| param instanceof DateTime || param instanceof LocalDate{{/joda}}{{#jsr310}}|| param instanceof OffsetDateTime || param instanceof LocalDate{{/jsr310}}) {
//Serialize to json string and remove the " enclosing characters
String jsonStr = json.serialize(param);
String jsonStr = JSON.serialize(param);
return jsonStr.substring(1, jsonStr.length() - 1);
} else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
Expand Down Expand Up @@ -1045,7 +1045,7 @@ public class ApiClient {
contentType = "application/json";
}
if (isJsonMime(contentType)) {
return json.deserialize(respBody, returnType);
return JSON.deserialize(respBody, returnType);
} else if (returnType.equals(String.class)) {
// Expecting string, return the raw response body.
return (T) respBody;
Expand Down Expand Up @@ -1079,7 +1079,7 @@ public class ApiClient {
} else if (isJsonMime(contentType)) {
String content;
if (obj != null) {
content = json.serialize(obj);
content = JSON.serialize(obj);
} else {
content = null;
}
Expand Down Expand Up @@ -1583,7 +1583,7 @@ public class ApiClient {
} else {
String content;
if (obj != null) {
content = json.serialize(obj);
content = JSON.serialize(obj);
} else {
content = null;
}
Expand Down Expand Up @@ -1736,7 +1736,7 @@ public class ApiClient {
if (entry.getKey().equals(param.getName())) {
switch (param.getIn()) {
case "path":
path = path.replaceAll("\\{" + param.getName() + "\\}", escapeString(value.toString()));
path = path.replace("{" + param.getName() + "}", escapeString(value.toString()));
break;
case "query":
if (value instanceof Collection<?>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public class {{classname}} {
// create path and map variables
{{^dynamicOperations}}
String localVarPath = "{{{path}}}"{{#pathParams}}
.replaceAll("\\{" + "{{baseName}}" + "\\}", localVarApiClient.escapeString({{#collectionFormat}}localVarApiClient.collectionPathParameterToString("{{{collectionFormat}}}", {{{paramName}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}.toString(){{/collectionFormat}})){{/pathParams}};
.replace("{" + "{{baseName}}" + "}", localVarApiClient.escapeString({{#collectionFormat}}localVarApiClient.collectionPathParameterToString("{{{collectionFormat}}}", {{{paramName}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}.toString(){{/collectionFormat}})){{/pathParams}};
{{/dynamicOperations}}
{{#dynamicOperations}}
ApiOperation apiOperation = localVarApiClient.getOperationLookupMap().get("{{{operationId}}}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public class RetryingOAuth extends OAuth implements Interceptor {
.setClientSecret(clientSecret));
setFlow(flow);
if (parameters != null) {
for (String paramName : parameters.keySet()) {
tokenRequestBuilder.setParameter(paramName, parameters.get(paramName));
for (Map.Entry<String, String> entry : parameters.entrySet()) {
tokenRequestBuilder.setParameter(entry.getKey(), entry.getValue());
}
}
}
Expand Down Expand Up @@ -130,8 +130,8 @@ public class RetryingOAuth extends OAuth implements Interceptor {
}

Map<String, String> headers = oAuthRequest.getHeaders();
for (String headerName : headers.keySet()) {
requestBuilder.addHeader(headerName, headers.get(headerName));
for (Map.Entry<String, String> entry : headers.entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
requestBuilder.url(oAuthRequest.getLocationUri());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public DateFormat getDateFormat() {
* @return a {@link org.openapitools.client.ApiClient} object
*/
public ApiClient setDateFormat(DateFormat dateFormat) {
this.json.setDateFormat(dateFormat);
JSON.setDateFormat(dateFormat);
return this;
}

Expand All @@ -289,7 +289,7 @@ public ApiClient setDateFormat(DateFormat dateFormat) {
* @return a {@link org.openapitools.client.ApiClient} object
*/
public ApiClient setSqlDateFormat(DateFormat dateFormat) {
this.json.setSqlDateFormat(dateFormat);
JSON.setSqlDateFormat(dateFormat);
return this;
}

Expand All @@ -300,7 +300,7 @@ public ApiClient setSqlDateFormat(DateFormat dateFormat) {
* @return a {@link org.openapitools.client.ApiClient} object
*/
public ApiClient setOffsetDateTimeFormat(DateTimeFormatter dateFormat) {
this.json.setOffsetDateTimeFormat(dateFormat);
JSON.setOffsetDateTimeFormat(dateFormat);
return this;
}

Expand All @@ -311,7 +311,7 @@ public ApiClient setOffsetDateTimeFormat(DateTimeFormatter dateFormat) {
* @return a {@link org.openapitools.client.ApiClient} object
*/
public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) {
this.json.setLocalDateFormat(dateFormat);
JSON.setLocalDateFormat(dateFormat);
return this;
}

Expand All @@ -322,7 +322,7 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) {
* @return a {@link org.openapitools.client.ApiClient} object
*/
public ApiClient setLenientOnJson(boolean lenientOnJson) {
this.json.setLenientOnJson(lenientOnJson);
JSON.setLenientOnJson(lenientOnJson);
return this;
}

Expand Down Expand Up @@ -583,7 +583,7 @@ public String parameterToString(Object param) {
return "";
} else if (param instanceof Date || param instanceof OffsetDateTime || param instanceof LocalDate) {
//Serialize to json string and remove the " enclosing characters
String jsonStr = json.serialize(param);
String jsonStr = JSON.serialize(param);
return jsonStr.substring(1, jsonStr.length() - 1);
} else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
Expand Down Expand Up @@ -842,7 +842,7 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
contentType = "application/json";
}
if (isJsonMime(contentType)) {
return json.deserialize(respBody, returnType);
return JSON.deserialize(respBody, returnType);
} else if (returnType.equals(String.class)) {
// Expecting string, return the raw response body.
return (T) respBody;
Expand Down Expand Up @@ -876,7 +876,7 @@ public RequestBody serialize(Object obj, String contentType) throws ApiException
} else if (isJsonMime(contentType)) {
String content;
if (obj != null) {
content = json.serialize(obj);
content = JSON.serialize(obj);
} else {
content = null;
}
Expand Down Expand Up @@ -1378,7 +1378,7 @@ private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String k
} else {
String content;
if (obj != null) {
content = json.serialize(obj);
content = JSON.serialize(obj);
} else {
content = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public DateFormat getDateFormat() {
* @return a {@link org.openapitools.client.ApiClient} object
*/
public ApiClient setDateFormat(DateFormat dateFormat) {
this.json.setDateFormat(dateFormat);
JSON.setDateFormat(dateFormat);
return this;
}

Expand All @@ -382,7 +382,7 @@ public ApiClient setDateFormat(DateFormat dateFormat) {
* @return a {@link org.openapitools.client.ApiClient} object
*/
public ApiClient setSqlDateFormat(DateFormat dateFormat) {
this.json.setSqlDateFormat(dateFormat);
JSON.setSqlDateFormat(dateFormat);
return this;
}

Expand All @@ -393,7 +393,7 @@ public ApiClient setSqlDateFormat(DateFormat dateFormat) {
* @return a {@link org.openapitools.client.ApiClient} object
*/
public ApiClient setOffsetDateTimeFormat(DateTimeFormatter dateFormat) {
this.json.setOffsetDateTimeFormat(dateFormat);
JSON.setOffsetDateTimeFormat(dateFormat);
return this;
}

Expand All @@ -404,7 +404,7 @@ public ApiClient setOffsetDateTimeFormat(DateTimeFormatter dateFormat) {
* @return a {@link org.openapitools.client.ApiClient} object
*/
public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) {
this.json.setLocalDateFormat(dateFormat);
JSON.setLocalDateFormat(dateFormat);
return this;
}

Expand All @@ -415,7 +415,7 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) {
* @return a {@link org.openapitools.client.ApiClient} object
*/
public ApiClient setLenientOnJson(boolean lenientOnJson) {
this.json.setLenientOnJson(lenientOnJson);
JSON.setLenientOnJson(lenientOnJson);
return this;
}

Expand Down Expand Up @@ -696,7 +696,7 @@ public String parameterToString(Object param) {
return "";
} else if (param instanceof Date || param instanceof OffsetDateTime || param instanceof LocalDate) {
//Serialize to json string and remove the " enclosing characters
String jsonStr = json.serialize(param);
String jsonStr = JSON.serialize(param);
return jsonStr.substring(1, jsonStr.length() - 1);
} else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
Expand Down Expand Up @@ -943,7 +943,7 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
contentType = "application/json";
}
if (isJsonMime(contentType)) {
return json.deserialize(respBody, returnType);
return JSON.deserialize(respBody, returnType);
} else if (returnType.equals(String.class)) {
// Expecting string, return the raw response body.
return (T) respBody;
Expand Down Expand Up @@ -977,7 +977,7 @@ public RequestBody serialize(Object obj, String contentType) throws ApiException
} else if (isJsonMime(contentType)) {
String content;
if (obj != null) {
content = json.serialize(obj);
content = JSON.serialize(obj);
} else {
content = null;
}
Expand Down Expand Up @@ -1456,7 +1456,7 @@ private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String k
} else {
String content;
if (obj != null) {
content = json.serialize(obj);
content = JSON.serialize(obj);
} else {
content = null;
}
Expand Down Expand Up @@ -1608,7 +1608,7 @@ public String fillParametersFromOperation(
if (entry.getKey().equals(param.getName())) {
switch (param.getIn()) {
case "path":
path = path.replaceAll("\\{" + param.getName() + "\\}", escapeString(value.toString()));
path = path.replace("{" + param.getName() + "}", escapeString(value.toString()));
break;
case "query":
if (value instanceof Collection<?>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public RetryingOAuth(
.setClientSecret(clientSecret));
setFlow(flow);
if (parameters != null) {
for (String paramName : parameters.keySet()) {
tokenRequestBuilder.setParameter(paramName, parameters.get(paramName));
for (Map.Entry<String, String> entry : parameters.entrySet()) {
tokenRequestBuilder.setParameter(entry.getKey(), entry.getValue());
}
}
}
Expand Down Expand Up @@ -129,8 +129,8 @@ private Response retryingIntercept(Chain chain, boolean updateTokenAndRetryOnAut
}

Map<String, String> headers = oAuthRequest.getHeaders();
for (String headerName : headers.keySet()) {
requestBuilder.addHeader(headerName, headers.get(headerName));
for (Map.Entry<String, String> entry : headers.entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
requestBuilder.url(oAuthRequest.getLocationUri());

Expand Down
Loading

0 comments on commit ca56242

Please sign in to comment.