From 79af6b167633a02d56ee2a71547f6135b34a88f0 Mon Sep 17 00:00:00 2001 From: Jen Basch Date: Tue, 25 Aug 2026 08:27:30 -0700 Subject: [PATCH] Sanitize block comment endings in generated Kotlin doc comments (#1830) --- .../pkl/codegen/java/JavaCodeGeneratorTest.kt | 18 ++++++++++++++++++ .../pkl/codegen/kotlin/KotlinCodeGenerator.kt | 2 +- .../codegen/kotlin/KotlinCodeGeneratorTest.kt | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pkl-codegen-java/src/test/kotlin/org/pkl/codegen/java/JavaCodeGeneratorTest.kt b/pkl-codegen-java/src/test/kotlin/org/pkl/codegen/java/JavaCodeGeneratorTest.kt index 3d9e97c70..b7323da21 100644 --- a/pkl-codegen-java/src/test/kotlin/org/pkl/codegen/java/JavaCodeGeneratorTest.kt +++ b/pkl-codegen-java/src/test/kotlin/org/pkl/codegen/java/JavaCodeGeneratorTest.kt @@ -2261,6 +2261,24 @@ class JavaCodeGeneratorTest { ) } + @Test + fun `block comment escaping in doc comments`() { + val javaCode = + generateJavaCode( + """ + module my.mod + class Person { + /// This doc coment needs */ escaping + foo: Int + } + """ + .trimIndent(), + JavaCodeGeneratorOptions(generateJavadoc = true), + ) + + assertThat(javaCode).compilesSuccessfully().contains("*/") + } + private fun Map.validateContents( @Suppress("RemoveRedundantQualifierName") vararg assertions: kotlin.Pair> ) { diff --git a/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt b/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt index 70313e3c2..26a845188 100644 --- a/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt +++ b/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt @@ -609,7 +609,7 @@ class KotlinCodeGenerator( // do the minimum work necessary to avoid kotlin compile errors // generating idiomatic KDoc would require parsing doc comments, converting member links, etc. - private fun renderAsKdoc(docComment: String): String = docComment + private fun renderAsKdoc(docComment: String): String = docComment.replace("*/", "*/") private fun PClass.toKotlinPoetName(): ClassName { val (packageName, moduleTypeName) = nameMapper.map(moduleName) diff --git a/pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt b/pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt index da0ddd6a2..0db629455 100644 --- a/pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt +++ b/pkl-codegen-kotlin/src/test/kotlin/org/pkl/codegen/kotlin/KotlinCodeGeneratorTest.kt @@ -2054,6 +2054,23 @@ class KotlinCodeGeneratorTest { ) } + @Test + fun `block comment escaping in doc comments`() { + val kotlinCode = + generateKotlinCode( + """ + class Person { + /// This doc coment needs */ escaping + foo: Int + } + """ + .trimIndent(), + generateKdoc = true, + ) + + assertThat(kotlinCode).compilesSuccessfully().contains("*/") + } + private fun Map.validateContents( @Suppress("RemoveRedundantQualifierName") vararg assertions: kotlin.Pair> ) {