Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced GeneratorProperties as a thread-safe clone of System Properties #1598

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.gradle.kotlin.dsl.property
import org.openapitools.codegen.CodegenConstants
import org.openapitools.codegen.DefaultGenerator
import org.openapitools.codegen.config.CodegenConfigurator
import org.openapitools.codegen.config.GeneratorProperties


/**
Expand Down Expand Up @@ -352,36 +353,36 @@ open class GenerateTask : DefaultTask() {
try {
if (systemProperties.isPresent) {
systemProperties.get().forEach { (key, value) ->
// System.setProperty returns the original value for a key, or null.
// GeneratorProperties.setProperty returns the original value for a key, or null.
// Cache the original value or null…we will late put the properties back in their original state.
originalEnvironmentVariables[key] = System.setProperty(key, value)
originalEnvironmentVariables[key] = GeneratorProperties.setProperty(key, value)
configurator.addSystemProperty(key, value)
}
}

if (supportingFilesConstrainedTo.isPresent && supportingFilesConstrainedTo.get().isNotEmpty()) {
System.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesConstrainedTo.get().joinToString(","))
GeneratorProperties.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesConstrainedTo.get().joinToString(","))
} else {
System.clearProperty(CodegenConstants.SUPPORTING_FILES)
GeneratorProperties.clearProperty(CodegenConstants.SUPPORTING_FILES)
}

if (modelFilesConstrainedTo.isPresent && modelFilesConstrainedTo.get().isNotEmpty()) {
System.setProperty(CodegenConstants.MODELS, modelFilesConstrainedTo.get().joinToString(","))
GeneratorProperties.setProperty(CodegenConstants.MODELS, modelFilesConstrainedTo.get().joinToString(","))
} else {
System.clearProperty(CodegenConstants.MODELS)
GeneratorProperties.clearProperty(CodegenConstants.MODELS)
}

if (apiFilesConstrainedTo.isPresent && apiFilesConstrainedTo.get().isNotEmpty()) {
System.setProperty(CodegenConstants.APIS, apiFilesConstrainedTo.get().joinToString(","))
GeneratorProperties.setProperty(CodegenConstants.APIS, apiFilesConstrainedTo.get().joinToString(","))
} else {
System.clearProperty(CodegenConstants.APIS)
GeneratorProperties.clearProperty(CodegenConstants.APIS)
}

System.setProperty(CodegenConstants.API_DOCS, generateApiDocumentation.get().toString())
System.setProperty(CodegenConstants.MODEL_DOCS, generateModelDocumentation.get().toString())
System.setProperty(CodegenConstants.MODEL_TESTS, generateModelTests.get().toString())
System.setProperty(CodegenConstants.API_TESTS, generateApiTests.get().toString())
System.setProperty(CodegenConstants.WITH_XML, withXml.get().toString())
GeneratorProperties.setProperty(CodegenConstants.API_DOCS, generateApiDocumentation.get().toString())
GeneratorProperties.setProperty(CodegenConstants.MODEL_DOCS, generateModelDocumentation.get().toString())
GeneratorProperties.setProperty(CodegenConstants.MODEL_TESTS, generateModelTests.get().toString())
GeneratorProperties.setProperty(CodegenConstants.API_TESTS, generateApiTests.get().toString())
GeneratorProperties.setProperty(CodegenConstants.WITH_XML, withXml.get().toString())

// now override with any specified parameters
verbose.ifNotEmpty { value ->
Expand Down Expand Up @@ -542,13 +543,7 @@ open class GenerateTask : DefaultTask() {
}
} finally {
// Reset all modified system properties back to their original state
originalEnvironmentVariables.forEach {
when {
it.value == null -> System.clearProperty(it.key)
else -> System.setProperty(it.key, it.value)
}
}
originalEnvironmentVariables.clear()
GeneratorProperties.reset()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.gradle.kotlin.dsl.property
import org.openapitools.codegen.CodegenConstants
import org.openapitools.codegen.DefaultGenerator
import org.openapitools.codegen.config.CodegenConfigurator
import org.openapitools.codegen.config.GeneratorProperties


/**
Expand Down Expand Up @@ -323,8 +324,6 @@ open class GenerateTask : DefaultTask() {
@get:Internal
val configOptions = project.objects.property<Map<String, String>>()

private val originalEnvironmentVariables = mutableMapOf<String, String?>()

private fun <T : Any?> Property<T>.ifNotEmpty(block: Property<T>.(T) -> Unit) {
if (isPresent) {
val item: T? = get()
Expand Down Expand Up @@ -352,36 +351,33 @@ open class GenerateTask : DefaultTask() {
try {
if (systemProperties.isPresent) {
systemProperties.get().forEach { (key, value) ->
// System.setProperty returns the original value for a key, or null.
// Cache the original value or null…we will late put the properties back in their original state.
originalEnvironmentVariables[key] = System.setProperty(key, value)
configurator.addSystemProperty(key, value)
}
}

if (supportingFilesConstrainedTo.isPresent && supportingFilesConstrainedTo.get().isNotEmpty()) {
System.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesConstrainedTo.get().joinToString(","))
GeneratorProperties.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesConstrainedTo.get().joinToString(","))
} else {
System.clearProperty(CodegenConstants.SUPPORTING_FILES)
GeneratorProperties.clearProperty(CodegenConstants.SUPPORTING_FILES)
}

if (modelFilesConstrainedTo.isPresent && modelFilesConstrainedTo.get().isNotEmpty()) {
System.setProperty(CodegenConstants.MODELS, modelFilesConstrainedTo.get().joinToString(","))
GeneratorProperties.setProperty(CodegenConstants.MODELS, modelFilesConstrainedTo.get().joinToString(","))
} else {
System.clearProperty(CodegenConstants.MODELS)
GeneratorProperties.clearProperty(CodegenConstants.MODELS)
}

if (apiFilesConstrainedTo.isPresent && apiFilesConstrainedTo.get().isNotEmpty()) {
System.setProperty(CodegenConstants.APIS, apiFilesConstrainedTo.get().joinToString(","))
GeneratorProperties.setProperty(CodegenConstants.APIS, apiFilesConstrainedTo.get().joinToString(","))
} else {
System.clearProperty(CodegenConstants.APIS)
GeneratorProperties.clearProperty(CodegenConstants.APIS)
}

System.setProperty(CodegenConstants.API_DOCS, generateApiDocumentation.get().toString())
System.setProperty(CodegenConstants.MODEL_DOCS, generateModelDocumentation.get().toString())
System.setProperty(CodegenConstants.MODEL_TESTS, generateModelTests.get().toString())
System.setProperty(CodegenConstants.API_TESTS, generateApiTests.get().toString())
System.setProperty(CodegenConstants.WITH_XML, withXml.get().toString())
GeneratorProperties.setProperty(CodegenConstants.API_DOCS, generateApiDocumentation.get().toString())
GeneratorProperties.setProperty(CodegenConstants.MODEL_DOCS, generateModelDocumentation.get().toString())
GeneratorProperties.setProperty(CodegenConstants.MODEL_TESTS, generateModelTests.get().toString())
GeneratorProperties.setProperty(CodegenConstants.API_TESTS, generateApiTests.get().toString())
GeneratorProperties.setProperty(CodegenConstants.WITH_XML, withXml.get().toString())

// now override with any specified parameters
verbose.ifNotEmpty { value ->
Expand Down Expand Up @@ -541,14 +537,7 @@ open class GenerateTask : DefaultTask() {
throw GradleException("Code generation failed.", e)
}
} finally {
// Reset all modified system properties back to their original state
originalEnvironmentVariables.forEach {
when {
it.value == null -> System.clearProperty(it.key)
else -> System.setProperty(it.key, it.value)
}
}
originalEnvironmentVariables.clear()
GeneratorProperties.reset()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.config.CodegenConfigurator;
import org.openapitools.codegen.config.GeneratorProperties;
import org.sonatype.plexus.build.incremental.BuildContext;
import org.sonatype.plexus.build.incremental.DefaultBuildContext;
import org.slf4j.Logger;
Expand Down Expand Up @@ -499,28 +500,28 @@ public void execute() throws MojoExecutionException {

// Set generation options
if (null != generateApis && generateApis) {
System.setProperty(CodegenConstants.APIS, "");
GeneratorProperties.setProperty(CodegenConstants.APIS, "");
} else {
System.clearProperty(CodegenConstants.APIS);
GeneratorProperties.clearProperty(CodegenConstants.APIS);
}

if (null != generateModels && generateModels) {
System.setProperty(CodegenConstants.MODELS, modelsToGenerate);
GeneratorProperties.setProperty(CodegenConstants.MODELS, modelsToGenerate);
} else {
System.clearProperty(CodegenConstants.MODELS);
GeneratorProperties.clearProperty(CodegenConstants.MODELS);
}

if (null != generateSupportingFiles && generateSupportingFiles) {
System.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesToGenerate);
GeneratorProperties.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesToGenerate);
} else {
System.clearProperty(CodegenConstants.SUPPORTING_FILES);
GeneratorProperties.clearProperty(CodegenConstants.SUPPORTING_FILES);
}

System.setProperty(CodegenConstants.MODEL_TESTS, generateModelTests.toString());
System.setProperty(CodegenConstants.MODEL_DOCS, generateModelDocumentation.toString());
System.setProperty(CodegenConstants.API_TESTS, generateApiTests.toString());
System.setProperty(CodegenConstants.API_DOCS, generateApiDocumentation.toString());
System.setProperty(CodegenConstants.WITH_XML, withXml.toString());
GeneratorProperties.setProperty(CodegenConstants.MODEL_TESTS, generateModelTests.toString());
GeneratorProperties.setProperty(CodegenConstants.MODEL_DOCS, generateModelDocumentation.toString());
GeneratorProperties.setProperty(CodegenConstants.API_TESTS, generateApiTests.toString());
GeneratorProperties.setProperty(CodegenConstants.API_DOCS, generateApiDocumentation.toString());
GeneratorProperties.setProperty(CodegenConstants.WITH_XML, withXml.toString());

if (configOptions != null) {
// Retained for backwards-compataibility with configOptions -> instantiation-types
Expand Down Expand Up @@ -593,13 +594,13 @@ public void execute() throws MojoExecutionException {
if (environmentVariables != null) {

for (String key : environmentVariables.keySet()) {
originalEnvironmentVariables.put(key, System.getProperty(key));
originalEnvironmentVariables.put(key, GeneratorProperties.getProperty(key));
String value = environmentVariables.get(key);
if (value == null) {
// don't put null values
value = "";
}
System.setProperty(key, value);
GeneratorProperties.setProperty(key, value);
configurator.addSystemProperty(key, value);
}
}
Expand Down Expand Up @@ -680,9 +681,9 @@ private void addCompileSourceRootIfConfigured() {
// when running the plugin multiple consecutive times with different configurations.
for (Map.Entry<String, String> entry : originalEnvironmentVariables.entrySet()) {
if (entry.getValue() == null) {
System.clearProperty(entry.getKey());
GeneratorProperties.clearProperty(entry.getKey());
} else {
System.setProperty(entry.getKey(), entry.getValue());
GeneratorProperties.setProperty(entry.getKey(), entry.getValue());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.openapitools.codegen.CodegenDiscriminator.MappedModel;
import org.openapitools.codegen.config.GeneratorProperties;
import org.openapitools.codegen.examples.ExampleGenerator;
import org.openapitools.codegen.serializer.SerializerUtils;
import org.openapitools.codegen.utils.ModelUtils;
Expand Down Expand Up @@ -2792,7 +2793,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
}
codegenParameter.jsonSchema = Json.pretty(parameter);

if (System.getProperty("debugParser") != null) {
if (GeneratorProperties.getProperty("debugParser") != null) {
LOGGER.info("working on Parameter " + parameter.getName());
LOGGER.info("JSON schema: " + codegenParameter.jsonSchema);
}
Expand Down
Loading