Skip to content

Commit 1a4e5a4

Browse files
authored
Java client: Add constants for libraries (#163)
1 parent 37df59d commit 1a4e5a4

1 file changed

Lines changed: 35 additions & 27 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
public class JavaClientCodegen extends AbstractJavaCodegen
5353
implements BeanValidationFeatures, PerformBeanValidationFeatures,
5454
GzipFeatures {
55+
5556
static final String MEDIA_TYPE = "mediaType";
5657

57-
@SuppressWarnings("hiding")
5858
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
5959

6060
public static final String USE_RX_JAVA = "useRxJava";
@@ -68,9 +68,17 @@ public class JavaClientCodegen extends AbstractJavaCodegen
6868
public static final String PLAY_24 = "play24";
6969
public static final String PLAY_25 = "play25";
7070

71+
public static final String FEIGN = "feign";
72+
public static final String GOOGLE_API_CLIENT = "google-api-client";
73+
public static final String JERSEY1 = "jersey1";
74+
public static final String JERSEY2 = "jersey2";
75+
public static final String OKHTTP_GSON = "okhttp-gson";
76+
public static final String RESTEASY = "resteasy";
77+
public static final String RESTTEMPLATE = "resttemplate";
78+
public static final String REST_ASSURED = "rest-assured";
7179
public static final String RETROFIT_1 = "retrofit";
7280
public static final String RETROFIT_2 = "retrofit2";
73-
public static final String REST_ASSURED = "rest-assured";
81+
public static final String VERTX = "vertx";
7482

7583
protected String gradleWrapperPackage = "gradle.wrapper";
7684
protected boolean useRxJava = false;
@@ -105,24 +113,24 @@ public JavaClientCodegen() {
105113
cliOptions.add(CliOption.newBoolean(USE_GZIP_FEATURE, "Send gzip-encoded requests"));
106114
cliOptions.add(CliOption.newBoolean(USE_RUNTIME_EXCEPTION, "Use RuntimeException instead of Exception"));
107115

108-
supportedLibraries.put("jersey1", "HTTP client: Jersey client 1.19.4. JSON processing: Jackson 2.8.9. Enable Java6 support using '-DsupportJava6=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
109-
supportedLibraries.put("feign", "HTTP client: OpenFeign 9.4.0. JSON processing: Jackson 2.8.9");
110-
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.8.9");
111-
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.8.1. Enable Parcelable models on Android using '-DparcelableModel=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
116+
supportedLibraries.put(JERSEY1, "HTTP client: Jersey client 1.19.4. JSON processing: Jackson 2.8.9. Enable Java6 support using '-DsupportJava6=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
117+
supportedLibraries.put(FEIGN, "HTTP client: OpenFeign 9.4.0. JSON processing: Jackson 2.8.9");
118+
supportedLibraries.put(JERSEY2, "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.8.9");
119+
supportedLibraries.put(OKHTTP_GSON, "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.8.1. Enable Parcelable models on Android using '-DparcelableModel=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
112120
supportedLibraries.put(RETROFIT_1, "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.3.1 (Retrofit 1.9.0). IMPORTANT NOTE: retrofit1.x is no longer actively maintained so please upgrade to 'retrofit2' instead.");
113121
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 3.8.0. JSON processing: Gson 2.6.1 (Retrofit 2.3.0). Enable the RxJava adapter using '-DuseRxJava[2]=true'. (RxJava 1.x or 2.x)");
114-
supportedLibraries.put("resttemplate", "HTTP client: Spring RestTemplate 4.3.9-RELEASE. JSON processing: Jackson 2.8.9");
115-
supportedLibraries.put("resteasy", "HTTP client: Resteasy client 3.1.3.Final. JSON processing: Jackson 2.8.9");
116-
supportedLibraries.put("vertx", "HTTP client: VertX client 3.2.4. JSON processing: Jackson 2.8.9");
117-
supportedLibraries.put("google-api-client", "HTTP client: Google API client 1.23.0. JSON processing: Jackson 2.8.9");
118-
supportedLibraries.put("rest-assured", "HTTP client: rest-assured : 3.1.0. JSON processing: Gson 2.6.1. Only for Java8");
122+
supportedLibraries.put(RESTTEMPLATE, "HTTP client: Spring RestTemplate 4.3.9-RELEASE. JSON processing: Jackson 2.8.9");
123+
supportedLibraries.put(RESTEASY, "HTTP client: Resteasy client 3.1.3.Final. JSON processing: Jackson 2.8.9");
124+
supportedLibraries.put(VERTX, "HTTP client: VertX client 3.2.4. JSON processing: Jackson 2.8.9");
125+
supportedLibraries.put(GOOGLE_API_CLIENT, "HTTP client: Google API client 1.23.0. JSON processing: Jackson 2.8.9");
126+
supportedLibraries.put(REST_ASSURED, "HTTP client: rest-assured : 3.1.0. JSON processing: Gson 2.6.1. Only for Java8");
119127

120128
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
121129
libraryOption.setEnum(supportedLibraries);
122130
// set okhttp-gson as the default
123-
libraryOption.setDefault("okhttp-gson");
131+
libraryOption.setDefault(OKHTTP_GSON);
124132
cliOptions.add(libraryOption);
125-
setLibrary("okhttp-gson");
133+
setLibrary(OKHTTP_GSON);
126134

127135
}
128136

@@ -202,12 +210,12 @@ public void processOpts() {
202210
writeOptional(outputFolder, new SupportingFile("manifest.mustache", projectFolder, "AndroidManifest.xml"));
203211
supportingFiles.add(new SupportingFile("travis.mustache", "", ".travis.yml"));
204212
supportingFiles.add(new SupportingFile("ApiClient.mustache", invokerFolder, "ApiClient.java"));
205-
if (!("resttemplate".equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) {
213+
if (!(RESTTEMPLATE.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) {
206214
supportingFiles.add(new SupportingFile("StringUtil.mustache", invokerFolder, "StringUtil.java"));
207215
}
208216

209217
// google-api-client doesn't use the Swagger auth, because it uses Google Credential directly (HttpRequestInitializer)
210-
if (!("google-api-client".equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) {
218+
if (!(GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) {
211219
supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java"));
212220
supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java"));
213221
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
@@ -228,23 +236,23 @@ public void processOpts() {
228236
}
229237

230238
//TODO: add doc to retrofit1 and feign
231-
if ("feign".equals(getLibrary()) || "retrofit".equals(getLibrary())) {
239+
if (FEIGN.equals(getLibrary()) || RETROFIT_1.equals(getLibrary())) {
232240
modelDocTemplateFiles.remove("model_doc.mustache");
233241
apiDocTemplateFiles.remove("api_doc.mustache");
234242
}
235243

236-
if (!("feign".equals(getLibrary()) || "resttemplate".equals(getLibrary()) || usesAnyRetrofitLibrary() || "google-api-client".equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) {
244+
if (!(FEIGN.equals(getLibrary()) || RESTTEMPLATE.equals(getLibrary()) || usesAnyRetrofitLibrary() || GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) {
237245
supportingFiles.add(new SupportingFile("apiException.mustache", invokerFolder, "ApiException.java"));
238246
supportingFiles.add(new SupportingFile("Configuration.mustache", invokerFolder, "Configuration.java"));
239247
supportingFiles.add(new SupportingFile("Pair.mustache", invokerFolder, "Pair.java"));
240248
supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java"));
241249
}
242250

243-
if ("feign".equals(getLibrary())) {
251+
if (FEIGN.equals(getLibrary())) {
244252
additionalProperties.put("jackson", "true");
245253
supportingFiles.add(new SupportingFile("ParamExpander.mustache", invokerFolder, "ParamExpander.java"));
246254
supportingFiles.add(new SupportingFile("EncodingUtils.mustache", invokerFolder, "EncodingUtils.java"));
247-
} else if ("okhttp-gson".equals(getLibrary()) || StringUtils.isEmpty(getLibrary())) {
255+
} else if (OKHTTP_GSON.equals(getLibrary()) || StringUtils.isEmpty(getLibrary())) {
248256
// the "okhttp-gson" library template requires "ApiCallback.mustache" for async call
249257
supportingFiles.add(new SupportingFile("ApiCallback.mustache", invokerFolder, "ApiCallback.java"));
250258
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
@@ -260,19 +268,19 @@ public void processOpts() {
260268
if ("retrofit2".equals(getLibrary()) && !usePlayWS) {
261269
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
262270
}
263-
} else if ("jersey2".equals(getLibrary())) {
271+
} else if (JERSEY2.equals(getLibrary())) {
264272
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
265273
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
266274
additionalProperties.put("jackson", "true");
267-
} else if ("resteasy".equals(getLibrary())) {
275+
} else if (RESTEASY.equals(getLibrary())) {
268276
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
269277
additionalProperties.put("jackson", "true");
270-
} else if ("jersey1".equals(getLibrary())) {
278+
} else if (JERSEY1.equals(getLibrary())) {
271279
additionalProperties.put("jackson", "true");
272-
} else if ("resttemplate".equals(getLibrary())) {
280+
} else if (RESTTEMPLATE.equals(getLibrary())) {
273281
additionalProperties.put("jackson", "true");
274282
supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java"));
275-
} else if ("vertx".equals(getLibrary())) {
283+
} else if (VERTX.equals(getLibrary())) {
276284
typeMapping.put("file", "AsyncFile");
277285
importMapping.put("AsyncFile", "io.vertx.core.file.AsyncFile");
278286
setJava8Mode(true);
@@ -281,7 +289,7 @@ public void processOpts() {
281289
apiTemplateFiles.put("apiImpl.mustache", "Impl.java");
282290
apiTemplateFiles.put("rxApiImpl.mustache", ".java");
283291
supportingFiles.remove(new SupportingFile("manifest.mustache", projectFolder, "AndroidManifest.xml"));
284-
} else if ("google-api-client".equals(getLibrary())) {
292+
} else if (GOOGLE_API_CLIENT.equals(getLibrary())) {
285293
additionalProperties.put("jackson", "true");
286294

287295
} else if (REST_ASSURED.equals(getLibrary())) {
@@ -398,7 +406,7 @@ public int compare(CodegenParameter one, CodegenParameter another) {
398406
}
399407

400408
// camelize path variables for Feign client
401-
if ("feign".equals(getLibrary())) {
409+
if (FEIGN.equals(getLibrary())) {
402410
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
403411
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
404412
for (CodegenOperation op : operationList) {
@@ -420,7 +428,7 @@ public int compare(CodegenParameter one, CodegenParameter another) {
420428

421429
@Override
422430
public String apiFilename(String templateName, String tag) {
423-
if ("vertx".equals(getLibrary())) {
431+
if (VERTX.equals(getLibrary())) {
424432
String suffix = apiTemplateFiles().get(templateName);
425433
String subFolder = "";
426434
if (templateName.startsWith("rx")) {

0 commit comments

Comments
 (0)