From d8f2f35c1ac9a6610b1b21099471b716f638f123 Mon Sep 17 00:00:00 2001 From: Will Jones Date: Tue, 13 Aug 2024 11:49:24 +0100 Subject: [PATCH] Fix HTML escaping after literals in non-code Javadoc When formatting Javadocs, we use a state machine to track what we should be escaping and when. In the case of non-code blocks, we generally HTML-escape all characters except `@`, which must be encoded using a Javadoc `{@literal @}` block. If we see a sequence of `@` characters, we enter a state where we try to print as many `@`s as possible before closing the `@literal` block. Unfortunately, there is a bug whereby when closing the literal block (because we have seen a non-`@` character), we do not HTML-escape the first character after it. This commit fixes this issue. Fixes #1408 --- pkg/codegen/java/utilities.go | 2 +- pkg/codegen/java/utilities_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/codegen/java/utilities.go b/pkg/codegen/java/utilities.go index 00d8024f9bf..f7062c437aa 100644 --- a/pkg/codegen/java/utilities.go +++ b/pkg/codegen/java/utilities.go @@ -148,7 +148,7 @@ func (w *plainJavadocWriter) WriteRune(r rune) { w.b.WriteRune('@') } else { w.b.WriteRune('}') - w.b.WriteRune(r) + w.b.WriteString(html.EscapeString(string(r))) w.state = plainJavadocText } } diff --git a/pkg/codegen/java/utilities_test.go b/pkg/codegen/java/utilities_test.go index 5586ebb7872..5552dc67023 100644 --- a/pkg/codegen/java/utilities_test.go +++ b/pkg/codegen/java/utilities_test.go @@ -96,10 +96,12 @@ func TestFormatForeignComments(t *testing.T) { - @Foo @ bar - Foo @ bar - Foo bar @@ +- Foo @ works? `, expected: autogold.Expect(` * - {@literal @}Foo {@literal @} bar * - Foo {@literal @} bar * - Foo bar {@literal @@} + * - Foo {@literal @}<this> works? * `), }, {