From 4e3517f23efa721519b9fb232a4a397030e09671 Mon Sep 17 00:00:00 2001 From: Jim Schubert <james.schubert@gmail.com> Date: Tue, 7 Aug 2018 15:59:56 -0400 Subject: [PATCH] [gradle] Support nullable system property values The gradle plugin sets all System properties before generation, then reverts them back to their original state. System.getProperty/setProperty return null if the property was not previously set. The Kotlin map was defined with non-nullable key/value constraints, so setting something not commonly set (modelDocs: "false") would result in an runtime exception. This changes the map to support nullable values, and rather than setting a null System property at the end, it clears those which previously had no value. --- .../samples/local-spec/build.gradle | 3 +++ .../generator/gradle/plugin/tasks/GenerateTask.kt | 15 ++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/modules/openapi-generator-gradle-plugin/samples/local-spec/build.gradle b/modules/openapi-generator-gradle-plugin/samples/local-spec/build.gradle index e9a5bb42e0e..e5cde468032 100644 --- a/modules/openapi-generator-gradle-plugin/samples/local-spec/build.gradle +++ b/modules/openapi-generator-gradle-plugin/samples/local-spec/build.gradle @@ -41,6 +41,9 @@ openApiGenerate { configOptions = [ dateLibrary: "java8" ] + systemProperties = [ + modelDocs: "false" + ] } task buildGoSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){ diff --git a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt index db901f021c7..7c6ecd099da 100644 --- a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt +++ b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt @@ -323,7 +323,7 @@ open class GenerateTask : DefaultTask() { @get:Internal val configOptions = project.objects.property<Map<String, String>>() - private val originalEnvironmentVariables = mutableMapOf<String, String>() + private val originalEnvironmentVariables = mutableMapOf<String, String?>() private fun <T : Any?> Property<T>.ifNotEmpty(block: Property<T>.(T) -> Unit) { if (isPresent) { @@ -352,8 +352,9 @@ open class GenerateTask : DefaultTask() { try { if (systemProperties.isPresent) { systemProperties.get().forEach { (key, value) -> - originalEnvironmentVariables[key] = System.getProperty(key) - System.setProperty(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) } } @@ -540,8 +541,12 @@ open class GenerateTask : DefaultTask() { throw GradleException("Code generation failed.", e) } } finally { - originalEnvironmentVariables.forEach { entry -> - System.setProperty(entry.key, entry.value) + // 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() } -- GitLab