Skip to content

Commit 9920c43

Browse files
jimschubertwing328
authored andcommitted
[gradle] Support nullable system property values (#764)
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.
1 parent 4060fcb commit 9920c43

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

  • modules/openapi-generator-gradle-plugin

modules/openapi-generator-gradle-plugin/samples/local-spec/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ openApiGenerate {
4141
configOptions = [
4242
dateLibrary: "java8"
4343
]
44+
systemProperties = [
45+
modelDocs: "false"
46+
]
4447
}
4548

4649
task buildGoSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ open class GenerateTask : DefaultTask() {
323323
@get:Internal
324324
val configOptions = project.objects.property<Map<String, String>>()
325325

326-
private val originalEnvironmentVariables = mutableMapOf<String, String>()
326+
private val originalEnvironmentVariables = mutableMapOf<String, String?>()
327327

328328
private fun <T : Any?> Property<T>.ifNotEmpty(block: Property<T>.(T) -> Unit) {
329329
if (isPresent) {
@@ -352,8 +352,9 @@ open class GenerateTask : DefaultTask() {
352352
try {
353353
if (systemProperties.isPresent) {
354354
systemProperties.get().forEach { (key, value) ->
355-
originalEnvironmentVariables[key] = System.getProperty(key)
356-
System.setProperty(key, value)
355+
// System.setProperty returns the original value for a key, or null.
356+
// Cache the original value or null…we will late put the properties back in their original state.
357+
originalEnvironmentVariables[key] = System.setProperty(key, value)
357358
configurator.addSystemProperty(key, value)
358359
}
359360
}
@@ -540,8 +541,12 @@ open class GenerateTask : DefaultTask() {
540541
throw GradleException("Code generation failed.", e)
541542
}
542543
} finally {
543-
originalEnvironmentVariables.forEach { entry ->
544-
System.setProperty(entry.key, entry.value)
544+
// Reset all modified system properties back to their original state
545+
originalEnvironmentVariables.forEach {
546+
when {
547+
it.value == null -> System.clearProperty(it.key)
548+
else -> System.setProperty(it.key, it.value)
549+
}
545550
}
546551
originalEnvironmentVariables.clear()
547552
}

0 commit comments

Comments
 (0)