Add support for customizing spring boot ConfigurationProperties (#1897)
This adds an option which allows users to control exactly how `@ConfigurationProperties` annotations are attached to classes. This helps when: * Spring Boot users don't have a single root class, but multiple classes that are injected in disparate places * Pkl's module structure doesn't match the resulting `.properties` layout
This commit is contained in:
1 parent
f973fd6717
commit
88a50d167e
15 files changed
+294
-10
No files matched your search
+11
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,6 +49,15 @@ data class CliKotlinCodeGeneratorOptions(
|
||||
* original Pkl module name, and the value is the desired replacement.
|
||||
*/
|
||||
val renames: Map<String, String> = emptyMap(),
|
||||
|
||||
/**
|
||||
* The mapping of Pkl class names to the spring boot `@ConfigurationProperties` prefix name, when
|
||||
* [generateSpringBootConfig] is enabled.
|
||||
*
|
||||
* Setting this field is optional, and by default is inferred through the schema of the input
|
||||
* modules.
|
||||
*/
|
||||
val springBootConfigurationProperties: Map<String, String> = emptyMap(),
|
||||
) {
|
||||
@Suppress("DeprecatedCallableAddReplaceWith")
|
||||
@Deprecated("deprecated without replacement")
|
||||
@@ -62,5 +71,6 @@ data class CliKotlinCodeGeneratorOptions(
|
||||
implementSerializable,
|
||||
addGeneratedAnnotation,
|
||||
renames,
|
||||
springBootConfigurationProperties,
|
||||
)
|
||||
}
|
||||
@@ -53,7 +53,25 @@ data class KotlinCodeGeneratorOptions(
|
||||
* from the corresponding name derived from the Pkl module declaration .
|
||||
*/
|
||||
val renames: Map<String, String> = emptyMap(),
|
||||
)
|
||||
|
||||
/**
|
||||
* Explicitly configures `@ConfigurationProperties` on generated classes.
|
||||
*
|
||||
* The key is the Pkl class name (e.g. `my.mod` for a module class, `my.mod#Server` for a nested
|
||||
* class), and the value is the prefix. An empty prefix generates a bare annotation.
|
||||
*
|
||||
* If non-empty, no `@ConfigurationProperties` annotations are inferred.
|
||||
*/
|
||||
val springBootConfigurationProperties: Map<String, String> = emptyMap(),
|
||||
) {
|
||||
init {
|
||||
for (value in springBootConfigurationProperties.values) {
|
||||
require(value.isEmpty() || value.isValidConfigurationPropertiesPrefix) {
|
||||
"Invalid value in `springBootConfigurationProperties`: expected a valid ConfigurationProperties prefix, but got '$value'."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class KotlinCodeGeneratorException(message: String) : RuntimeException(message)
|
||||
|
||||
@@ -136,7 +154,11 @@ class KotlinCodeGenerator(
|
||||
|
||||
val hasModuleProperties = pModuleClass.properties.any { !it.value.isHidden }
|
||||
val isGenerateModuleClass =
|
||||
hasModuleProperties || pModuleClass.isOpen || pModuleClass.isAbstract
|
||||
hasModuleProperties ||
|
||||
pModuleClass.isOpen ||
|
||||
pModuleClass.isAbstract ||
|
||||
options.generateSpringBootConfig &&
|
||||
options.springBootConfigurationProperties.containsKey(pModuleClass.displayName)
|
||||
|
||||
fun generateCompanionRelatedCode(
|
||||
builder: TypeSpec.Builder,
|
||||
@@ -420,7 +442,21 @@ class KotlinCodeGenerator(
|
||||
}
|
||||
|
||||
fun generateSpringBootAnnotations(builder: TypeSpec.Builder) {
|
||||
if (isModuleClass) {
|
||||
if (options.springBootConfigurationProperties.isNotEmpty()) {
|
||||
// explicit configuration: the user is in total control, so don't infer anything
|
||||
val prefix = options.springBootConfigurationProperties[pClass.displayName] ?: return
|
||||
val annotation =
|
||||
AnnotationSpec.builder(
|
||||
ClassName("org.springframework.boot.context.properties", "ConfigurationProperties")
|
||||
)
|
||||
.apply {
|
||||
if (prefix.isNotEmpty()) {
|
||||
addMember("%S", prefix)
|
||||
}
|
||||
}
|
||||
.build()
|
||||
builder.addAnnotation(annotation)
|
||||
} else if (isModuleClass) {
|
||||
builder.addAnnotation(
|
||||
ClassName("org.springframework.boot.context.properties", "ConfigurationProperties")
|
||||
)
|
||||
|
||||
@@ -22,12 +22,14 @@ import com.github.ajalt.clikt.parameters.options.associate
|
||||
import com.github.ajalt.clikt.parameters.options.default
|
||||
import com.github.ajalt.clikt.parameters.options.flag
|
||||
import com.github.ajalt.clikt.parameters.options.option
|
||||
import com.github.ajalt.clikt.parameters.options.validate
|
||||
import com.github.ajalt.clikt.parameters.types.path
|
||||
import java.nio.file.Path
|
||||
import org.pkl.commons.cli.CliBaseOptions
|
||||
import org.pkl.commons.cli.cliMain
|
||||
import org.pkl.commons.cli.commands.ModulesCommand
|
||||
import org.pkl.commons.cli.commands.installCommonOptions
|
||||
import org.pkl.commons.isValidConfigurationPropertiesPrefix
|
||||
import org.pkl.commons.toPath
|
||||
import org.pkl.core.Release
|
||||
|
||||
@@ -72,6 +74,29 @@ class PklKotlinCodegenCommand : ModulesCommand(name = "pkl-codegen-kotlin", help
|
||||
)
|
||||
.flag()
|
||||
|
||||
private val springBootConfigurationProperties: Map<String, String> by
|
||||
option(
|
||||
names = arrayOf("--spring-boot-configuration-properties"),
|
||||
metavar = "pkl_class_name=prefix_value",
|
||||
help =
|
||||
"""
|
||||
Turn the specified Pkl classes into Kotlin classes with the `@ConfigurationProperties`
|
||||
annotation.
|
||||
|
||||
If this setting is omitted, the `@ConfigurationProperties` annotation is inferred
|
||||
according to the structure of the input modules.
|
||||
"""
|
||||
.trimIndent(),
|
||||
)
|
||||
.associate()
|
||||
.validate { map ->
|
||||
for (prefix in map.values) {
|
||||
require(prefix.isEmpty() || prefix.isValidConfigurationPropertiesPrefix) {
|
||||
"value must be a valid spring boot config prefix, but got '$prefix'"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val implementSerializable: Boolean by
|
||||
option(
|
||||
names = arrayOf("--implement-serializable"),
|
||||
@@ -113,6 +138,7 @@ class PklKotlinCodegenCommand : ModulesCommand(name = "pkl-codegen-kotlin", help
|
||||
implementSerializable = implementSerializable,
|
||||
addGeneratedAnnotation = addGeneratedAnnotation,
|
||||
renames = renames,
|
||||
springBootConfigurationProperties = springBootConfigurationProperties,
|
||||
)
|
||||
CliKotlinCodeGenerator(options).run()
|
||||
}
|
||||
|
||||
@@ -142,6 +142,7 @@ class KotlinCodeGeneratorTest {
|
||||
generateKdoc: Boolean = false,
|
||||
generateSpringBootConfig: Boolean = false,
|
||||
implementSerializable: Boolean = false,
|
||||
springBootConfigurationProperties: Map<String, String> = emptyMap(),
|
||||
): KotlinSourceCode {
|
||||
|
||||
val module = Evaluator.preconfigured().evaluateSchema(ModuleSource.text(pklCode))
|
||||
@@ -153,6 +154,7 @@ class KotlinCodeGeneratorTest {
|
||||
generateKdoc = generateKdoc,
|
||||
generateSpringBootConfig = generateSpringBootConfig,
|
||||
implementSerializable = implementSerializable,
|
||||
springBootConfigurationProperties = springBootConfigurationProperties,
|
||||
),
|
||||
)
|
||||
return KotlinSourceCode(generator.kotlinFile)
|
||||
@@ -1566,6 +1568,42 @@ class KotlinCodeGeneratorTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `spring boot -- explicitly configured ConfigurationProperties`() {
|
||||
val kotlinCode =
|
||||
generateKotlinCode(
|
||||
"""
|
||||
module my.mod
|
||||
|
||||
class Server
|
||||
"""
|
||||
.trimIndent(),
|
||||
generateSpringBootConfig = true,
|
||||
springBootConfigurationProperties =
|
||||
mapOf("my.mod" to "", "my.mod#Server" to "my-server-prefix"),
|
||||
)
|
||||
assertThat(kotlinCode).contains("@ConfigurationProperties\ndata class Mod")
|
||||
assertThat(kotlinCode).contains("@ConfigurationProperties(\"my-server-prefix\")")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `spring boot -- explicitly configured properties disables inference`() {
|
||||
val kotlinCode =
|
||||
generateKotlinCode(
|
||||
"""
|
||||
module my.mod
|
||||
|
||||
server: Server
|
||||
|
||||
class Server
|
||||
"""
|
||||
.trimIndent(),
|
||||
generateSpringBootConfig = true,
|
||||
springBootConfigurationProperties = mapOf("foo" to "foo"),
|
||||
)
|
||||
assertThat(kotlinCode).doesNotContain("@ConfigurationProperties")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `import module`() {
|
||||
val library =
|
||||
|
||||
Reference in new issue
Block a user