Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,8 @@ static String sanitizeJavadoc(String documentation) {
documentation = documentation.replaceAll("\\s+$", "");
documentation = documentation.replaceAll("\\*/", "*/");
documentation = documentation.replaceAll("/\\*", "/*");
// Java translates Unicode escapes before lexing, including inside comments.
documentation = documentation.replace("\\", "\");
// Rewrite '@see <url>' to use an html anchor tag
documentation =
documentation.replaceAll("@see (http:" + URL_CHARS + "+)", "@see <a href=\"$1\">$1</a>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public void sanitizeJavadocStarSlash() {
assertThat(JavaGenerator.sanitizeJavadoc(input)).isEqualTo(expected);
}

@Test
public void sanitizeJavadocUnicodeEscapes() {
String input = "\\u002a\\u002f class Cheeky { } \\u002f\\u002a";
String expected = "&#92;u002a&#92;u002f class Cheeky { } &#92;u002f&#92;u002a";
assertThat(JavaGenerator.sanitizeJavadoc(input)).isEqualTo(expected);
}

@Test
public void generatedOptionTypeSanitizesJavadoc() throws Exception {
Schema schema =
Expand All @@ -74,6 +81,26 @@ public void generatedOptionTypeSanitizesJavadoc() throws Exception {
assertThat(javaOutput).doesNotContain("*/ class Cheeky");
}

@Test
public void generatedFieldSanitizesJavadocUnicodeEscapes() throws Exception {
Schema schema =
new SchemaBuilder()
.add(
Path.get("message.proto"),
""
+ "syntax = \"proto2\";\n"
+ "message Message {\n"
+ " // \\u002a\\u002f class Cheeky { } \\u002f\\u002a\n"
+ " optional string owner = 1;\n"
+ "}\n")
.build();

String javaOutput = new JavaWithProfilesGenerator(schema).generateJava("Message");

assertThat(javaOutput).contains("&#92;u002a&#92;u002f class Cheeky { } &#92;u002f&#92;u002a");
assertThat(javaOutput).doesNotContain("\\u002a\\u002f class Cheeky");
}

@Test
public void enclosingTypeSanitizesJavadoc() throws Exception {
Schema schema =
Expand Down
Loading