Java client codegen cleanup (#20691)

* made Configuration.defaultApiClient volatile so that if it's changed the new value is immediately visible to all other threads

* made no-arg api ctor use Configuration.getDefaultApiClient() instead of creating new ApiClient every time

* replaced all use of URLEncoder.encode with ApiClient.urlEncode; just code cleanup; no functional changes

* disabled AbstractJavaCodegenTest.testGeneratedExampleValues as it fails, possibly due to timezone (Locale.ROOT) of local system

* replaced thread-unsafe SimpleDateFormat with DateTimeFormatter and re-enabled unit-test

* removed unused import

* ran the generate-sample scripts and generated a HUGE number of changes!

* some code cleanup and adding booleans to processOpts to remove lots of duplicate FOO.equals(getLibrary()) checks; also using isLibrary() wherever possible

* added missing import of Configuration to api.mustache (and, thus, all native samples)

* added missing import for ApiClient to anyof and oneof model mustaches

* rolled back formatting of cliOptions block (too many changes for a simple PR)

* use toLowerCase with locale

* reverted supportedLibraries reordering and format changes to reduce diff noise

* updated comment

* rolled back Slf4j change due to failing arch-unit test; also rolled back serialization-lib setting code due to unit-test failure (still a mystery)

* moved lib* booleans AFTER super.processOpts() the library can be changed (as shown by unit-test)
This commit is contained in:
Ron Reynolds 2025-02-20 02:16:40 -08:00 committed by GitHub
parent 73079c2ef6
commit 13111077d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 78 additions and 64 deletions

View File

@ -22,7 +22,6 @@ import io.swagger.v3.oas.models.media.Schema;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
@ -57,7 +56,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
static final String MEDIA_TYPE = "mediaType";
private final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
public static final String USE_RX_JAVA2 = "useRxJava2";
public static final String USE_RX_JAVA3 = "useRxJava3";
public static final String DO_NOT_USE_RX = "doNotUseRx";
@ -267,7 +266,8 @@ public class JavaClientCodegen extends AbstractJavaCodegen
cliOptions.add(libraryOption);
setLibrary(OKHTTP_GSON);
CliOption serializationLibrary = new CliOption(CodegenConstants.SERIALIZATION_LIBRARY, "Serialization library, default depends on value of the option library");
CliOption serializationLibrary = new CliOption(CodegenConstants.SERIALIZATION_LIBRARY,
"Serialization library, default depends on value of the option library");
Map<String, String> serializationOptions = new HashMap<>();
serializationOptions.put(SERIALIZATION_LIBRARY_GSON, "Use Gson as serialization library");
serializationOptions.put(SERIALIZATION_LIBRARY_JACKSON, "Use Jackson as serialization library");
@ -307,21 +307,42 @@ public class JavaClientCodegen extends AbstractJavaCodegen
@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
if (MICROPROFILE.equals(getLibrary())) {
if (isLibrary(MICROPROFILE)) {
co.subresourceOperation = !co.path.isEmpty();
}
}
@Override
public void processOpts() {
if (WEBCLIENT.equals(getLibrary()) || NATIVE.equals(getLibrary()) || RESTCLIENT.equals(getLibrary())) {
// this is before super.processOpts() because that method uses dateLibrary to select imports
if (isLibrary(WEBCLIENT) || isLibrary(NATIVE) || isLibrary(RESTCLIENT)) {
dateLibrary = "java8";
} else if (MICROPROFILE.equals(getLibrary())) {
} else if (isLibrary(MICROPROFILE)) {
dateLibrary = "legacy";
}
super.processOpts();
super.processOpts(); // can actually change the library (possibly only in unit-tests but still...)
// determine and cache client library type once
final boolean libApache = isLibrary(APACHE);
final boolean libFeign = isLibrary(FEIGN);
final boolean libGoogleApiClient = isLibrary(GOOGLE_API_CLIENT);
final boolean libJersey2 = isLibrary(JERSEY2);
final boolean libJersey3 = isLibrary(JERSEY3);
final boolean libMicroprofile = isLibrary(MICROPROFILE);
final boolean libNative = isLibrary(NATIVE);
final boolean libOkHttpGson = isLibrary(OKHTTP_GSON) || StringUtils.isBlank(getLibrary());
final boolean libRestAssured = isLibrary(REST_ASSURED);
final boolean libRestClient = isLibrary(RESTCLIENT);
final boolean libRestEasy = isLibrary(RESTEASY);
final boolean libRestTemplate = isLibrary(RESTTEMPLATE);
final boolean libRetrofit2 = isLibrary(RETROFIT_2);
final boolean libVertx = isLibrary(VERTX);
final boolean libWebClient = isLibrary(WEBCLIENT);
// default jackson unless overridden by setSerializationLibrary
this.jackson = !additionalProperties.containsKey(CodegenConstants.SERIALIZATION_LIBRARY) || SERIALIZATION_LIBRARY_JACKSON.equals(additionalProperties.get(CodegenConstants.SERIALIZATION_LIBRARY));
this.jackson = !additionalProperties.containsKey(CodegenConstants.SERIALIZATION_LIBRARY) ||
SERIALIZATION_LIBRARY_JACKSON.equals(additionalProperties.get(CodegenConstants.SERIALIZATION_LIBRARY));
convertPropertyToBooleanAndWriteBack(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, this::setUseOneOfDiscriminatorLookup);
@ -399,7 +420,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
// add URL query deepObject support to native, apache-httpclient by default
if (!additionalProperties.containsKey(SUPPORT_URL_QUERY)) {
if (isLibrary(NATIVE) || isLibrary(APACHE)) {
if (libNative || libApache) {
// default to true for native and apache-httpclient
additionalProperties.put(SUPPORT_URL_QUERY, true);
}
@ -438,20 +459,21 @@ public class JavaClientCodegen extends AbstractJavaCodegen
}
// helper for client library that allow to parse/format java.time.OffsetDateTime or org.threeten.bp.OffsetDateTime
if (additionalProperties.containsKey("jsr310") && (isLibrary(WEBCLIENT) || isLibrary(VERTX) || isLibrary(RESTTEMPLATE) || isLibrary(RESTEASY) || isLibrary(MICROPROFILE) || isLibrary(JERSEY2) || isLibrary(JERSEY3) || isLibrary(APACHE) || isLibrary(RESTCLIENT))) {
if (additionalProperties.containsKey("jsr310") && (libWebClient || libVertx || libRestTemplate || libRestEasy
|| libMicroprofile || libJersey2 || libJersey3 || libApache || libRestClient)) {
supportingFiles.add(new SupportingFile("JavaTimeFormatter.mustache", invokerFolder, "JavaTimeFormatter.java"));
}
if (!(RESTTEMPLATE.equals(getLibrary()) || isLibrary(REST_ASSURED) || isLibrary(NATIVE) || isLibrary(MICROPROFILE))) {
if (!(libRestTemplate || libRestAssured || libNative || libMicroprofile)) {
supportingFiles.add(new SupportingFile("StringUtil.mustache", invokerFolder, "StringUtil.java"));
}
// google-api-client doesn't use the OpenAPI auth, because it uses Google Credential directly (HttpRequestInitializer)
if (!(isLibrary(GOOGLE_API_CLIENT) || isLibrary(REST_ASSURED) || isLibrary(NATIVE) || isLibrary(MICROPROFILE))) {
if (!(libGoogleApiClient || libRestAssured || libNative || libMicroprofile)) {
supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java"));
supportingFiles.add(new SupportingFile("auth/HttpBearerAuth.mustache", authFolder, "HttpBearerAuth.java"));
supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java"));
if (OKHTTP_GSON.equals(getLibrary()) && withAWSV4Signature) {
if (libOkHttpGson && withAWSV4Signature) {
supportingFiles.add(new SupportingFile("auth/AWS4Auth.mustache", authFolder, "AWS4Auth.java"));
}
}
@ -473,7 +495,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
convertPropertyToStringAndWriteBack(CodegenConstants.SERIALIZATION_LIBRARY, this::setSerializationLibrary);
//TODO: add auto-generated doc to feign
if (FEIGN.equals(getLibrary())) {
if (libFeign) {
modelDocTemplateFiles.remove("model_doc.mustache");
apiDocTemplateFiles.remove("api_doc.mustache");
//Templates to decode response headers
@ -484,21 +506,22 @@ public class JavaClientCodegen extends AbstractJavaCodegen
reservedWords.remove("file");
}
if (!(FEIGN.equals(getLibrary()) || RESTTEMPLATE.equals(getLibrary()) || RETROFIT_2.equals(getLibrary()) || GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()) || WEBCLIENT.equals(getLibrary()) || MICROPROFILE.equals(getLibrary()) || RESTCLIENT.equals(getLibrary()))) {
if (!(libFeign || libRestTemplate || libRetrofit2 || libGoogleApiClient || libRestAssured || libWebClient
|| libMicroprofile || libRestClient)) {
supportingFiles.add(new SupportingFile("apiException.mustache", invokerFolder, "ApiException.java"));
supportingFiles.add(new SupportingFile("Configuration.mustache", invokerFolder, "Configuration.java"));
supportingFiles.add(new SupportingFile("Pair.mustache", invokerFolder, "Pair.java"));
}
if (!(FEIGN.equals(getLibrary()) || RESTTEMPLATE.equals(getLibrary()) || RETROFIT_2.equals(getLibrary()) || GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()) || NATIVE.equals(getLibrary()) || MICROPROFILE.equals(getLibrary()))) {
if (!(libFeign || libRestTemplate || libRetrofit2 || libGoogleApiClient || libRestAssured || libNative || libMicroprofile)) {
supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java"));
}
if (APACHE.equals(getLibrary()) || RESTTEMPLATE.equals(getLibrary())) {
if (libApache || libRestTemplate) {
supportingFiles.add(new SupportingFile("BaseApi.mustache", invokerFolder, "BaseApi.java"));
}
if (FEIGN.equals(getLibrary())) {
if (libFeign) {
if (getSerializationLibrary() == null) {
LOGGER.info("No serializationLibrary configured, using '{}' as fallback", SERIALIZATION_LIBRARY_JACKSON);
setSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
@ -515,8 +538,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
// The flag below should be set for all Java libraries, but the templates need to be ported
// one by one for each library.
supportsAdditionalPropertiesWithComposedSchema = true;
} else if (OKHTTP_GSON.equals(getLibrary()) || StringUtils.isEmpty(getLibrary())) {
} else if (libOkHttpGson) {
// the "okhttp-gson" library template requires "ApiCallback.mustache" for async call
supportingFiles.add(new SupportingFile("ApiCallback.mustache", invokerFolder, "ApiCallback.java"));
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
@ -524,9 +546,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
supportingFiles.add(new SupportingFile("ProgressRequestBody.mustache", invokerFolder, "ProgressRequestBody.java"));
supportingFiles.add(new SupportingFile("ProgressResponseBody.mustache", invokerFolder, "ProgressResponseBody.java"));
supportingFiles.add(new SupportingFile("GzipRequestInterceptor.mustache", invokerFolder, "GzipRequestInterceptor.java"));
if (OKHTTP_GSON.equals(getLibrary())) {
supportingFiles.add(new SupportingFile("AbstractOpenApiSchema.mustache", modelsFolder, "AbstractOpenApiSchema.java"));
}
supportingFiles.add(new SupportingFile("AbstractOpenApiSchema.mustache", modelsFolder, "AbstractOpenApiSchema.java"));
// NOTE: below moved to postProcessOperationsWithModels
//supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
@ -539,8 +559,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
// The flag below should be set for all Java libraries, but the templates need to be ported
// one by one for each library.
supportsAdditionalPropertiesWithComposedSchema = true;
} else if (RETROFIT_2.equals(getLibrary())) {
} else if (libRetrofit2) {
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
supportingFiles.add(new SupportingFile("CollectionFormats.mustache", invokerFolder, "CollectionFormats.java"));
if (SERIALIZATION_LIBRARY_JACKSON.equals(getSerializationLibrary())) {
@ -548,7 +567,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
} else if (!usePlayWS) {
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
}
} else if (JERSEY2.equals(getLibrary())) {
} else if (libJersey2) {
additionalProperties.put("jersey2", true);
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
@ -564,8 +583,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
// The flag below should be set for all Java libraries, but the templates need to be ported
// one by one for each library.
supportsAdditionalPropertiesWithComposedSchema = true;
} else if (JERSEY3.equals(getLibrary())) {
} else if (libJersey3) {
additionalProperties.put("jersey3", true);
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
@ -582,15 +600,15 @@ public class JavaClientCodegen extends AbstractJavaCodegen
// one by one for each library.
supportsAdditionalPropertiesWithComposedSchema = true;
applyJakartaPackage();
} else if (NATIVE.equals(getLibrary())) {
} else if (libNative) {
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
supportingFiles.add(new SupportingFile("AbstractOpenApiSchema.mustache", modelsFolder, "AbstractOpenApiSchema.java"));
forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
} else if (RESTEASY.equals(getLibrary())) {
} else if (libRestEasy) {
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
} else if (RESTTEMPLATE.equals(getLibrary())) {
} else if (libRestTemplate) {
forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java"));
@ -600,8 +618,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
// The flag below should be set for all Java libraries, but the templates need to be ported
// one by one for each library.
supportsAdditionalPropertiesWithComposedSchema = true;
} else if (WEBCLIENT.equals(getLibrary())) {
} else if (libWebClient) {
forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
// Composed schemas can have the 'additionalProperties' keyword, as specified in JSON schema.
@ -610,21 +627,19 @@ public class JavaClientCodegen extends AbstractJavaCodegen
// The flag below should be set for all Java libraries, but the templates need to be ported
// one by one for each library.
supportsAdditionalPropertiesWithComposedSchema = true;
} else if (RESTCLIENT.equals(getLibrary())) {
} else if (libRestClient) {
forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
applyJakartaPackage();
} else if (VERTX.equals(getLibrary())) {
} else if (libVertx) {
typeMapping.put("file", "AsyncFile");
importMapping.put("AsyncFile", "io.vertx.core.file.AsyncFile");
forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
apiTemplateFiles.put("apiImpl.mustache", "Impl.java");
apiTemplateFiles.put("rxApiImpl.mustache", ".java");
supportingFiles.remove(new SupportingFile("manifest.mustache", projectFolder, "AndroidManifest.xml"));
} else if (GOOGLE_API_CLIENT.equals(getLibrary())) {
} else if (libGoogleApiClient) {
forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
} else if (REST_ASSURED.equals(getLibrary())) {
} else if (libRestAssured) {
if (getSerializationLibrary() == null) {
LOGGER.info("No serializationLibrary configured, using '{}' as fallback", SERIALIZATION_LIBRARY_GSON);
setSerializationLibrary(SERIALIZATION_LIBRARY_GSON);
@ -639,7 +654,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
additionalProperties.put("convert", new CaseFormatLambda(LOWER_CAMEL, UPPER_UNDERSCORE));
apiTemplateFiles.put("api.mustache", ".java");
supportingFiles.add(new SupportingFile("ResponseSpecBuilders.mustache", invokerFolder, "ResponseSpecBuilders.java"));
} else if (MICROPROFILE.equals(getLibrary())) {
} else if (libMicroprofile) {
supportingFiles.clear(); // Don't need extra files provided by Java Codegen
String apiExceptionFolder = (sourceFolder + File.separator + apiPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar);
String pomTemplate = mpRestClientVersions.get(microprofileRestClientVersion).pomTemplate;
@ -670,7 +685,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
additionalProperties.put("jsonbPolymorphism", true);
}
}
} else if (APACHE.equals(getLibrary())) {
} else if (libApache) {
forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
} else {
LOGGER.error("Unknown library option (-l/--library): {}", getLibrary());
@ -733,20 +748,19 @@ public class JavaClientCodegen extends AbstractJavaCodegen
// has OAuth defined
if (ProcessUtils.hasOAuthMethods(openAPI)) {
// for okhttp-gson (default), check to see if OAuth is defined and included OAuth-related files accordingly
if ((OKHTTP_GSON.equals(getLibrary()) || StringUtils.isEmpty(getLibrary()))) {
if (libOkHttpGson) {
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java"));
}
// google-api-client doesn't use the OpenAPI auth, because it uses Google Credential directly (HttpRequestInitializer)
if (!(GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()) || usePlayWS
|| NATIVE.equals(getLibrary()) || MICROPROFILE.equals(getLibrary()))) {
if (!(libGoogleApiClient || libRestAssured || usePlayWS || libNative || libMicroprofile)) {
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java"));
}
// Add OauthPasswordGrant.java and OauthClientCredentialsGrant.java for feign library
if (FEIGN.equals(getLibrary())) {
if (libFeign) {
supportingFiles.add(new SupportingFile("auth/DefaultApi20Impl.mustache", authFolder, "DefaultApi20Impl.java"));
supportingFiles.add(new SupportingFile("auth/OauthPasswordGrant.mustache", authFolder, "OauthPasswordGrant.java"));
supportingFiles.add(new SupportingFile("auth/OauthClientCredentialsGrant.mustache", authFolder, "OauthClientCredentialsGrant.java"));
@ -759,7 +773,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
super.postProcessOperationsWithModels(objs, allModels);
if (this.getSingleRequestParameter() && (JERSEY2.equals(getLibrary()) || JERSEY3.equals(getLibrary()) || OKHTTP_GSON.equals(getLibrary()))) {
if (this.getSingleRequestParameter() && (isLibrary(JERSEY2) || isLibrary(JERSEY3) || isLibrary(OKHTTP_GSON))) {
// loop through operations to set x-group-parameters extension to true if useSingleRequestParameter option is enabled
OperationMap operations = objs.getOperations();
if (operations != null) {
@ -772,7 +786,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
}
}
if (RETROFIT_2.equals(getLibrary())) {
if (isLibrary(RETROFIT_2)) {
OperationMap operations = objs.getOperations();
if (operations != null) {
List<CodegenOperation> ops = operations.getOperation();
@ -809,7 +823,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
}
// camelize path variables for Feign client
if (FEIGN.equals(getLibrary())) {
if (isLibrary(FEIGN)) {
OperationMap operations = objs.getOperations();
List<CodegenOperation> operationList = operations.getOperation();
Pattern methodPattern = Pattern.compile("^(.*):([^:]*)$");
@ -840,7 +854,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
}
}
if (NATIVE.equals(getLibrary()) || APACHE.equals(getLibrary())) {
if (isLibrary(NATIVE) || isLibrary(APACHE)) {
OperationMap operations = objs.getOperations();
List<CodegenOperation> operationList = operations.getOperation();
for (CodegenOperation op : operationList) {
@ -851,7 +865,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
}
}
if (MICROPROFILE.equals(getLibrary())) {
if (isLibrary(MICROPROFILE)) {
objs = AbstractJavaJAXRSServerCodegen.jaxrsPostProcessOperations(objs);
if (configKeyFromClassName) {
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
@ -863,7 +877,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
}
}
if (WEBCLIENT.equals(getLibrary())) {
if (isLibrary(WEBCLIENT)) {
OperationMap operations = objs.getOperations();
if (operations != null) {
List<CodegenOperation> ops = operations.getOperation();
@ -884,7 +898,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
@Override
public String apiFilename(String templateName, String tag) {
if (VERTX.equals(getLibrary())) {
if (isLibrary(VERTX)) {
String suffix = apiTemplateFiles().get(templateName);
String subFolder = "";
if (templateName.startsWith("rx")) {
@ -940,8 +954,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);
if (!BooleanUtils.toBoolean(model.isEnum)) {
//final String lib = getLibrary();
if (!model.isEnum) {
//Needed imports for Jackson based libraries
if (additionalProperties.containsKey(SERIALIZATION_LIBRARY_JACKSON)) {
model.imports.add("JsonProperty");
@ -964,12 +977,13 @@ public class JavaClientCodegen extends AbstractJavaCodegen
model.imports.add("JsonCreator");
}
}
if (MICROPROFILE.equals(getLibrary())) {
if (isLibrary(MICROPROFILE)) {
model.imports.remove("ApiModelProperty");
model.imports.remove("ApiModel");
}
if (!BooleanUtils.toBoolean(model.isEnum)) {
if (!model.isEnum) {
// needed by all pojos, but not enums
if (AnnotationLibrary.SWAGGER2.equals(getAnnotationLibrary())) {
model.imports.add("Schema");
@ -986,7 +1000,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
@Override
public CodegenModel fromModel(String name, Schema model) {
CodegenModel codegenModel = super.fromModel(name, model);
if (MICROPROFILE.equals(getLibrary())) {
if (isLibrary(MICROPROFILE)) {
if (codegenModel.imports.contains("ApiModel")) {
// Remove io.swagger.annotations.ApiModel import
codegenModel.imports.remove("ApiModel");
@ -1084,7 +1098,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
CodegenModel cm = mo.getModel();
cm.getVendorExtensions().putIfAbsent("x-implements", new ArrayList<String>());
if (JERSEY2.equals(getLibrary()) || JERSEY3.equals(getLibrary()) || NATIVE.equals(getLibrary()) || OKHTTP_GSON.equals(getLibrary())) {
if (isLibrary(JERSEY2) || isLibrary(JERSEY3) || isLibrary(NATIVE) || isLibrary(OKHTTP_GSON)) {
if (cm.oneOf != null && !cm.oneOf.isEmpty() && cm.oneOf.contains("ModelNull")) {
// if oneOf contains "null" type
cm.isNullable = true;
@ -1165,9 +1179,8 @@ public class JavaClientCodegen extends AbstractJavaCodegen
}
public void forceSerializationLibrary(String serializationLibrary) {
if ((this.serializationLibrary != null) && !this.serializationLibrary.equalsIgnoreCase(serializationLibrary)) {
LOGGER.warn(
"The configured serializationLibrary '{}', is not supported by the library: '{}', switching back to: {}",
if (this.serializationLibrary != null && !this.serializationLibrary.equalsIgnoreCase(serializationLibrary)) {
LOGGER.warn("The configured serializationLibrary '{}', is not supported by the library: '{}', switching back to: {}",
this.serializationLibrary, getLibrary(), serializationLibrary);
}
setSerializationLibrary(serializationLibrary);

View File

@ -229,7 +229,7 @@ public class JavaClientCodegenTest {
configAssert.assertValue(CodegenConstants.MODEL_PACKAGE, codegen::modelPackage, "org.openapitools.client.model");
configAssert.assertValue(CodegenConstants.API_PACKAGE, codegen::apiPackage, "org.openapitools.client.api");
configAssert.assertValue(CodegenConstants.INVOKER_PACKAGE, codegen::getInvokerPackage, "org.openapitools.client");
Assertions.assertEquals(codegen.getSerializationLibrary(), JavaClientCodegen.SERIALIZATION_LIBRARY_GSON);
assertEquals(codegen.getSerializationLibrary(), JavaClientCodegen.SERIALIZATION_LIBRARY_GSON);
configAssert.assertValue(JavaClientCodegen.SERIALIZATION_LIBRARY_GSON, "true");
}
@ -248,7 +248,7 @@ public class JavaClientCodegenTest {
configAssert.assertValue(CodegenConstants.MODEL_PACKAGE, codegen::modelPackage, "xyz.yyyyy.zzzzzzz.model");
configAssert.assertValue(CodegenConstants.API_PACKAGE, codegen::apiPackage, "xyz.yyyyy.zzzzzzz.api");
configAssert.assertValue(CodegenConstants.INVOKER_PACKAGE, codegen::getInvokerPackage, "xyz.yyyyy.zzzzzzz.invoker");
Assertions.assertEquals(codegen.getSerializationLibrary(), JavaClientCodegen.SERIALIZATION_LIBRARY_GSON); // the library JavaClientCodegen.OKHTTP_GSON only supports GSON
assertEquals(codegen.getSerializationLibrary(), JavaClientCodegen.SERIALIZATION_LIBRARY_GSON); // the library JavaClientCodegen.OKHTTP_GSON only supports GSON
}
@Test
@ -277,6 +277,7 @@ public class JavaClientCodegenTest {
codegen
.additionalProperties()
.put(CodegenConstants.INVOKER_PACKAGE, "xyz.yyyyy.zzzzzzz.iiii.invoker");
// this REQUIRES that serialization-library is handled case-insensitively (not sure if that's intentional)
codegen.additionalProperties().put(CodegenConstants.SERIALIZATION_LIBRARY, "JACKSON");
codegen.additionalProperties().put(CodegenConstants.LIBRARY, JavaClientCodegen.JERSEY2);
codegen.processOpts();
@ -286,7 +287,7 @@ public class JavaClientCodegenTest {
configAssert.assertValue(CodegenConstants.MODEL_PACKAGE, "xyz.yyyyy.zzzzzzz.mmmmm.model");
configAssert.assertValue(CodegenConstants.API_PACKAGE, "xyz.yyyyy.zzzzzzz.aaaaa.api");
configAssert.assertValue(CodegenConstants.INVOKER_PACKAGE, "xyz.yyyyy.zzzzzzz.iiii.invoker");
Assertions.assertEquals(codegen.getSerializationLibrary(), JavaClientCodegen.SERIALIZATION_LIBRARY_JACKSON);
assertEquals(codegen.getSerializationLibrary(), JavaClientCodegen.SERIALIZATION_LIBRARY_JACKSON);
}
@Test