Sanitize block comment endings in generated Kotlin doc comments (#1830)

This commit is contained in:
Jen Basch
2026-08-25 08:27:30 -07:00
committed by GitHub
parent 7404f7d160
commit 79af6b1676
3 changed files with 36 additions and 1 deletions
@@ -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<String, String>.validateContents(
@Suppress("RemoveRedundantQualifierName") vararg assertions: kotlin.Pair<String, List<String>>
) {
@@ -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("*/", "*&#47;")
private fun PClass.toKotlinPoetName(): ClassName {
val (packageName, moduleTypeName) = nameMapper.map(moduleName)
@@ -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("*&#47;")
}
private fun Map<String, String>.validateContents(
@Suppress("RemoveRedundantQualifierName") vararg assertions: kotlin.Pair<String, List<String>>
) {