Skip to content

Commit f02838c

Browse files
authored
Merge pull request #1160 from swagger-api/okhttp4-generator-tweak
Okhttp4 generator tweak
2 parents ae3b978 + 70a7034 commit f02838c

File tree

4 files changed

+51
-50
lines changed

4 files changed

+51
-50
lines changed

src/main/java/io/swagger/codegen/v3/generators/handlebars/StringUtilHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ public String toLowerCase(String string) {
2424
return string.toLowerCase();
2525
}
2626

27+
public String toQuotedWord(Object object) {
28+
String string = object.toString();
29+
if (string.contains("\"")) {
30+
return string;
31+
} else {
32+
return "\"" + string + "\"";
33+
}
34+
}
35+
2736
public String backSlash() {
2837
return "\\";
2938
}

src/main/resources/handlebars/Java/libraries/okhttp4-gson/api.mustache

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,21 @@ public class {{classname}} {
6363
}
6464

6565
{{#operation}}
66+
{{#contents}}
6667
/**
67-
* Build call for {{operationId}}{{#allParams}}
68-
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}}
68+
* Build call for {{operationId}}{{#parameters}}
69+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/parameters}}
6970
* @param progressListener Progress listener
7071
* @param progressRequestListener Progress request listener
7172
* @return Call to execute
7273
* @throws ApiException If fail to serialize the request body object
73-
{{#isDeprecated}}
74-
* @deprecated
75-
{{/isDeprecated}}
7674
{{#externalDocs}}
7775
* {{description}}
7876
* @see <a href="{{url}}">{{summary}} Documentation</a>
7977
{{/externalDocs}}
8078
*/
81-
{{#isDeprecated}}
82-
@Deprecated
83-
{{/isDeprecated}}
84-
public okhttp3.Call {{operationId}}Call({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
85-
Object {{localVariablePrefix}}localVarPostBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
79+
public okhttp3.Call {{operationId}}Call({{#parameters}}{{{dataType}}} {{paramName}}, {{/parameters}}final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
80+
Object {{localVariablePrefix}}localVarPostBody = {{^isForm}}{{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}{{/isForm}}{{#isForm}}null{{/isForm}};
8681

8782
// create path and map variables
8883
String {{localVariablePrefix}}localVarPath = "{{{path}}}"{{#pathParams}}
@@ -97,9 +92,13 @@ public class {{classname}} {
9792
if ({{paramName}} != null)
9893
{{localVariablePrefix}}localVarHeaderParams.put("{{baseName}}", {{localVariablePrefix}}apiClient.parameterToString({{paramName}}));{{/headerParams}}
9994

100-
{{javaUtilPrefix}}Map<String, Object> {{localVariablePrefix}}localVarFormParams = new {{javaUtilPrefix}}HashMap<String, Object>();{{#formParams}}
95+
{{javaUtilPrefix}}Map<String, Object> {{localVariablePrefix}}localVarFormParams = new {{javaUtilPrefix}}HashMap<String, Object>();
96+
{{#isForm}}
97+
{{#formParams}}
10198
if ({{paramName}} != null)
102-
{{localVariablePrefix}}localVarFormParams.put("{{baseName}}", {{paramName}});{{/formParams}}
99+
{{localVariablePrefix}}localVarFormParams.put("{{baseName}}", {{paramName}});
100+
{{/formParams}}
101+
{{/isForm}}
103102

104103
final String[] {{localVariablePrefix}}localVarAccepts = {
105104
{{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
@@ -133,16 +132,16 @@ public class {{classname}} {
133132
@Deprecated
134133
{{/isDeprecated}}
135134
@SuppressWarnings("rawtypes")
136-
private okhttp3.Call {{operationId}}ValidateBeforeCall({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
135+
private okhttp3.Call {{operationId}}ValidateBeforeCall({{#parameters}}{{{dataType}}} {{paramName}}, {{/parameters}}final ProgressResponseBody.ProgressListener progressListener, final ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException {
137136
{{^performBeanValidation}}
138-
{{#allParams}}{{#required}}
137+
{{#parameters}}{{#required}}
139138
// verify the required parameter '{{paramName}}' is set
140139
if ({{paramName}} == null) {
141140
throw new ApiException("Missing the required parameter '{{paramName}}' when calling {{operationId}}(Async)");
142141
}
143-
{{/required}}{{/allParams}}
142+
{{/required}}{{/parameters}}
144143

145-
okhttp3.Call {{localVariablePrefix}}call = {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}progressListener, progressRequestListener);
144+
okhttp3.Call {{localVariablePrefix}}call = {{operationId}}Call({{#parameters}}{{paramName}}, {{/parameters}}progressListener, progressRequestListener);
146145
return {{localVariablePrefix}}call;
147146

148147
{{/performBeanValidation}}
@@ -151,13 +150,13 @@ public class {{classname}} {
151150
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
152151
ExecutableValidator executableValidator = factory.getValidator().forExecutables();
153152
154-
Object[] parameterValues = { {{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}} };
155-
Method method = this.getClass().getMethod("{{operationId}}WithHttpInfo"{{#allParams}}, {{#isListContainer}}java.util.List{{/isListContainer}}{{#isMapContainer}}java.util.Map{{/isMapContainer}}{{^isListContainer}}{{^isMapContainer}}{{{dataType}}}{{/isMapContainer}}{{/isListContainer}}.class{{/allParams}});
153+
Object[] parameterValues = { {{#parameters}}{{paramName}}{{#has this 'more'}}, {{/has}}{{/parameters}} };
154+
Method method = this.getClass().getMethod("{{operationId}}WithHttpInfo"{{#parameters}}, {{#is this 'list-container'}}java.util.List{{/is}}{{#is this 'map-container'}}java.util.Map{{/is}}{{#isNot this 'list-container'}}{{#isNot this 'map-container'}}{{{dataType}}}{{/isNot}}{{/isNot}}.class{{/parameters}});
156155
Set<ConstraintViolation<{{classname}}>> violations = executableValidator.validateParameters(this, method,
157156
parameterValues);
158157

159158
if (violations.size() == 0) {
160-
okhttp3.Call {{localVariablePrefix}}call = {{operationId}}Call({{#allParams}}{{paramName}}, {{/allParams}}progressListener, progressRequestListener);
159+
okhttp3.Call {{localVariablePrefix}}call = {{operationId}}Call({{#parameters}}{{paramName}}, {{/parameters}}progressListener, progressRequestListener);
161160
return {{localVariablePrefix}}call;
162161

163162
} else {
@@ -176,68 +175,50 @@ public class {{classname}} {
176175

177176
/**
178177
* {{summary}}
179-
* {{notes}}{{#allParams}}
180-
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}}{{#returnType}}
178+
* {{notes}}{{#parameters}}
179+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/parameters}}{{#returnType}}
181180
* @return {{returnType}}{{/returnType}}
182181
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
183-
{{#isDeprecated}}
184-
* @deprecated
185-
{{/isDeprecated}}
186182
{{#externalDocs}}
187183
* {{description}}
188184
* @see <a href="{{url}}">{{summary}} Documentation</a>
189185
{{/externalDocs}}
190186
*/
191-
{{#isDeprecated}}
192-
@Deprecated
193-
{{/isDeprecated}}
194-
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException {
195-
{{#returnType}}ApiResponse<{{{returnType}}}> {{localVariablePrefix}}resp = {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}}
187+
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#parameters}}{{{dataType}}} {{paramName}}{{#has this 'more'}}, {{/has}}{{/parameters}}) throws ApiException {
188+
{{#returnType}}ApiResponse<{{{returnType}}}> {{localVariablePrefix}}resp = {{/returnType}}{{operationId}}WithHttpInfo({{#parameters}}{{paramName}}{{#has this 'more'}}, {{/has}}{{/parameters}});{{#returnType}}
196189
return {{localVariablePrefix}}resp.getData();{{/returnType}}
197190
}
198191

199192
/**
200193
* {{summary}}
201-
* {{notes}}{{#allParams}}
202-
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}}
194+
* {{notes}}{{#parameters}}
195+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/parameters}}
203196
* @return ApiResponse&lt;{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Void{{/returnType}}&gt;
204197
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
205-
{{#isDeprecated}}
206-
* @deprecated
207-
{{/isDeprecated}}
208198
{{#externalDocs}}
209199
* {{description}}
210200
* @see <a href="{{url}}">{{summary}} Documentation</a>
211201
{{/externalDocs}}
212202
*/
213-
{{#isDeprecated}}
214-
@Deprecated
215-
{{/isDeprecated}}
216-
public ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{operationId}}WithHttpInfo({{#allParams}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException {
217-
okhttp3.Call {{localVariablePrefix}}call = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}null, null);
203+
public ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{operationId}}WithHttpInfo({{#parameters}}{{#if useBeanValidation}}{{>beanValidationQueryParams}}{{/if}}{{{dataType}}} {{paramName}}{{#has this 'more'}}, {{/has}}{{/parameters}}) throws ApiException {
204+
okhttp3.Call {{localVariablePrefix}}call = {{operationId}}ValidateBeforeCall({{#parameters}}{{paramName}}, {{/parameters}}null, null);
218205
{{#returnType}}Type {{localVariablePrefix}}localVarReturnType = new TypeToken<{{{returnType}}}>(){}.getType();
219206
return {{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call, {{localVariablePrefix}}localVarReturnType);{{/returnType}}{{^returnType}}return {{localVariablePrefix}}apiClient.execute({{localVariablePrefix}}call);{{/returnType}}
220207
}
221208

222209
/**
223210
* {{summary}} (asynchronously)
224-
* {{notes}}{{#allParams}}
225-
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}}
211+
* {{notes}}{{#parameters}}
212+
* @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/parameters}}
226213
* @param callback The callback to be executed when the API call finishes
227214
* @return The request call
228215
* @throws ApiException If fail to process the API call, e.g. serializing the request body object
229-
{{#isDeprecated}}
230-
* @deprecated
231-
{{/isDeprecated}}
232216
{{#externalDocs}}
233217
* {{description}}
234218
* @see <a href="{{url}}">{{summary}} Documentation</a>
235219
{{/externalDocs}}
236220
*/
237-
{{#isDeprecated}}
238-
@Deprecated
239-
{{/isDeprecated}}
240-
public okhttp3.Call {{operationId}}Async({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}final ApiCallback<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> callback) throws ApiException {
221+
public okhttp3.Call {{operationId}}Async({{#parameters}}{{{dataType}}} {{paramName}}, {{/parameters}}final ApiCallback<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{localVariablePrefix}}callback) throws ApiException {
241222
242223
ProgressResponseBody.ProgressListener progressListener = null;
243224
ProgressRequestBody.ProgressRequestListener progressRequestListener = null;
@@ -258,11 +239,12 @@ public class {{classname}} {
258239
};
259240
}
260241

261-
okhttp3.Call {{localVariablePrefix}}call = {{operationId}}ValidateBeforeCall({{#allParams}}{{paramName}}, {{/allParams}}progressListener, progressRequestListener);
242+
okhttp3.Call {{localVariablePrefix}}call = {{operationId}}ValidateBeforeCall({{#parameters}}{{paramName}}, {{/parameters}}progressListener, progressRequestListener);
262243
{{#returnType}}Type {{localVariablePrefix}}localVarReturnType = new TypeToken<{{{returnType}}}>(){}.getType();
263-
{{localVariablePrefix}}apiClient.executeAsync({{localVariablePrefix}}call, {{localVariablePrefix}}localVarReturnType, callback);{{/returnType}}{{^returnType}}{{localVariablePrefix}}apiClient.executeAsync({{localVariablePrefix}}call, callback);{{/returnType}}
244+
{{localVariablePrefix}}apiClient.executeAsync({{localVariablePrefix}}call, {{localVariablePrefix}}localVarReturnType, {{localVariablePrefix}}callback);{{/returnType}}{{^returnType}}{{localVariablePrefix}}apiClient.executeAsync({{localVariablePrefix}}call, {{localVariablePrefix}}callback);{{/returnType}}
264245
return {{localVariablePrefix}}call;
265246
}
247+
{{/contents}}
266248
{{/operation}}
267249
}
268250
{{/operations}}

src/main/resources/handlebars/Java/modelEnum.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import com.google.gson.stream.JsonWriter;
1919
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
2020
{{#allowableValues}}
2121
{{#enumVars}}
22+
{{#gson}}
23+
{{#value}}
24+
@SerializedName({{{toQuotedWord value}}})
25+
{{/value}}
26+
{{/gson}}
2227
{{{name}}}({{#value}}{{{value}}}{{/value}}{{^value}}null{{/value}}){{^@last}},{{/@last}}{{#@last}};{{/@last}}
2328
{{/enumVars}}
2429
{{/allowableValues}}

src/main/resources/handlebars/Java/modelInnerEnum.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} {
66
{{#allowableValues}}
77
{{#enumVars}}
8+
{{#gson}}
9+
{{#value}}
10+
@SerializedName({{{toQuotedWord value}}})
11+
{{/value}}
12+
{{/gson}}
813
{{{name}}}({{#value}}{{{value}}}{{/value}}{{^value}}null{{/value}}){{^@last}},{{/@last}}{{#@last}};{{/@last}}
914
{{/enumVars}}
1015
{{/allowableValues}}

0 commit comments

Comments
 (0)