Skip to content

Commit

Permalink
[gradle] Support nullable system property values (OpenAPITools#764)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jimschubert authored and wing328 committed Aug 9, 2018
1 parent 18a02d7 commit 468a36f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ openApiGenerate {
configOptions = [
dateLibrary: "java8"
]
systemProperties = [
modelDocs: "false"
]
}

task buildGoSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 468a36f

Please # to comment.