mirror of
https://github.com/apple/pkl.git
synced 2026-09-16 06:41:58 +02:00
Fix invalid prefix when generating spring boot config (#1862)
Fix invalid prefix when generating spring boot config In Spring Boot, the prefix passed to `@ConfigurationProperties` must be in "canonical form". The presence of this annotation allows spring boot to directly inject properties into a class, but is not actually needed. The correct behavior here is to omit this annotation if the property name does not match Spring Boot's canonical form.
This commit is contained in:
@@ -22,6 +22,7 @@ import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
|
||||
import java.io.StringWriter
|
||||
import java.util.*
|
||||
import org.pkl.commons.NameMapper
|
||||
import org.pkl.commons.isValidConfigurationPropertiesPrefix
|
||||
import org.pkl.core.*
|
||||
import org.pkl.core.util.CodeGeneratorUtils
|
||||
import org.pkl.core.util.IoUtils
|
||||
@@ -437,7 +438,10 @@ class KotlinCodeGenerator(
|
||||
}
|
||||
propertyType is PType.Class && propertyType.pClass == pClass
|
||||
}
|
||||
if (modulePropertiesWithMatchingType.size == 1) {
|
||||
val singleProperty = modulePropertiesWithMatchingType.singleOrNull()
|
||||
if (
|
||||
singleProperty != null && singleProperty.simpleName.isValidConfigurationPropertiesPrefix
|
||||
) {
|
||||
// exactly one module property has this type -> make it available for direct injection
|
||||
// (potential improvement: make type available for direct injection if it occurs exactly
|
||||
// once in property tree)
|
||||
|
||||
@@ -1470,6 +1470,56 @@ class KotlinCodeGeneratorTest {
|
||||
assertThat(kotlinCodeWithoutSpringAnnotations).compilesSuccessfully()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `spring boot -- single module property has type but invalid prefix`() {
|
||||
val javaCode =
|
||||
generateKotlinCode(
|
||||
"""
|
||||
module my.mod
|
||||
|
||||
fooBar: FooBar
|
||||
|
||||
class FooBar
|
||||
"""
|
||||
.trimIndent(),
|
||||
generateSpringBootConfig = true,
|
||||
)
|
||||
// "fooBar" is not a valid prefix for `@ConfigurationProperties` annotations, so no
|
||||
// annotation is added
|
||||
assertThat(javaCode)
|
||||
.contains(
|
||||
"""
|
||||
|
|
||||
| data class FooBar
|
||||
"""
|
||||
.trimMargin()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `spring boot -- single module property has type and valid prefix`() {
|
||||
val javaCode =
|
||||
generateKotlinCode(
|
||||
"""
|
||||
module my.mod
|
||||
|
||||
server: Server
|
||||
|
||||
class Server
|
||||
"""
|
||||
.trimIndent(),
|
||||
generateSpringBootConfig = true,
|
||||
)
|
||||
assertThat(javaCode)
|
||||
.contains(
|
||||
"""
|
||||
| @ConfigurationProperties("server")
|
||||
| data class Server
|
||||
"""
|
||||
.trimMargin()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `import module`() {
|
||||
val library =
|
||||
|
||||
Reference in New Issue
Block a user