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 @@ -623,6 +623,7 @@ static String sanitizeJavadoc(String documentation) {
documentation = documentation.replaceAll("[^\\S\n]+\n", "\n");
documentation = documentation.replaceAll("\\s+$", "");
documentation = documentation.replaceAll("\\*/", "*/");
documentation = documentation.replaceAll("/\\*", "/*");
// Rewrite '@see <url>' to use an html anchor tag
documentation =
documentation.replaceAll("@see (http:" + URL_CHARS + "+)", "@see <a href=\"$1\">$1</a>");
Expand Down Expand Up @@ -829,7 +830,9 @@ private TypeSpec generateMessage(MessageType type) {
}
fieldBuilder.addAnnotation(wireFieldAnnotation(nameAllocator, field, type));
if (field.isExtension()) {
fieldBuilder.addJavadoc("Extension source: $L\n", field.getLocation().withPathOnly());
fieldBuilder.addJavadoc(
"Extension source: $L\n",
sanitizeJavadoc(field.getLocation().withPathOnly().toString()));
}
if (field.isDeprecated()) {
fieldBuilder.addAnnotation(Deprecated.class);
Expand Down Expand Up @@ -900,7 +903,7 @@ private TypeSpec generateEnclosingType(EnclosingType type) {
documentation +=
"<b>NOTE:</b> This type only exists to maintain class structure"
+ " for its nested types and is not an actual message.";
builder.addJavadoc("$L\n", documentation);
builder.addJavadoc("$L\n", sanitizeJavadoc(documentation));

builder.addMethod(
MethodSpec.constructorBuilder()
Expand Down Expand Up @@ -2392,7 +2395,7 @@ public ClassName generatedTypeName(ProtoMember member) {
.build());

if (!field.getDocumentation().isEmpty()) {
builder.addJavadoc("$L\n", field.getDocumentation());
builder.addJavadoc("$L\n", sanitizeJavadoc(field.getDocumentation()));
}

builder.addMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,58 @@ public void sanitizeJavadocWrapsSeeLinks() {
@Test
public void sanitizeJavadocStarSlash() {
String input = "/* comment inside comment. */";
String expected = "/* comment inside comment. &#42;/";
String expected = "/&#42; comment inside comment. &#42;/";
assertThat(JavaGenerator.sanitizeJavadoc(input)).isEqualTo(expected);
}

@Test
public void generatedOptionTypeSanitizesJavadoc() throws Exception {
Schema schema =
new SchemaBuilder()
.add(
Path.get("message.proto"),
""
+ "syntax = \"proto2\";\n"
+ "import \"google/protobuf/descriptor.proto\";\n"
+ "message Message {\n"
+ " extend google.protobuf.MessageOptions {\n"
+ " // */ class Cheeky { } /*\n"
+ " optional string owner = 55682;\n"
+ " }\n"
+ "}\n")
.build();

String javaOutput =
new JavaWithProfilesGenerator(schema).generateJava("Message", null, false, true);

assertThat(javaOutput).contains("&#42;/ class Cheeky { } /&#42;");
assertThat(javaOutput).doesNotContain("*/ class Cheeky");
}

@Test
public void enclosingTypeSanitizesJavadoc() throws Exception {
Schema schema =
new SchemaBuilder()
.add(
Path.get("message.proto"),
""
+ "// */ class Cheeky { } /*\n"
+ "message A {\n"
+ " message B {\n"
+ " }\n"
+ " optional B b = 1;\n"
+ "}\n")
.build();

Schema pruned = schema.prune(new PruningRules.Builder().addRoot("A.B").build());
JavaGenerator javaGenerator = JavaGenerator.get(schema);
TypeSpec typeSpec = javaGenerator.generateType(pruned.getType("A"));
String javaOutput = JavaFile.builder("", typeSpec).build().toString();

assertThat(javaOutput).contains("&#42;/ class Cheeky { } /&#42;");
assertThat(javaOutput).doesNotContain("*/ class Cheeky");
}

@Test
public void generateTypeUsesNameAllocatorInMessageBuilderBuild() throws Exception {
Schema schema =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,10 @@ class KotlinGenerator private constructor(
addKdoc("%L\n", field.documentation.sanitizeKdoc())
}
if (field.isExtension) {
addKdoc("Extension source: %L\n", field.location.withPathOnly())
addKdoc(
"Extension source: %L\n",
field.location.withPathOnly().toString().sanitizeKdoc(),
)
}
}
return parameterSpec.build() to propertySpec.build()
Expand Down Expand Up @@ -2877,7 +2880,7 @@ class KotlinGenerator private constructor(
.build(),
)
if (field.documentation.isNotEmpty()) {
builder.addKdoc("%L\n", field.documentation)
builder.addKdoc("%L\n", field.documentation.sanitizeKdoc())
}
builder.primaryConstructor(
FunSpec.constructorBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,29 @@ class KotlinGeneratorTest {
assertThat(input.sanitizeKdoc()).isEqualTo(expected)
}

@Test fun generatedOptionTypeSanitizesKdoc() {
val schema = buildSchema {
add(
"message.proto".toPath(),
"""
|syntax = "proto2";
|import "google/protobuf/descriptor.proto";
|message Message {
| extend google.protobuf.MessageOptions {
| // */ class Cheeky { } /*
| optional string owner = 55682;
| }
|}
""".trimMargin(),
)
}

val code = KotlinWithProfilesGenerator(schema).generateKotlin("Message")

assertThat(code).contains("&#42;/ class Cheeky { } /&#42;")
assertThat(code).doesNotContain("*/ class Cheeky")
}

@Test fun handleLongIdentifiers() {
val longType =
"MessageWithNameLongerThan100Chars00000000000000000000000000000000000000000000000000000000000000000000"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ class SwiftGenerator private constructor(
if (field.documentation.isNotBlank()) {
addDoc("\n%L\n", field.documentation.sanitizeDoc())
}
addDoc("\nSource: %L\n", field.location.withPathOnly())
addDoc("\nSource: %L\n", field.location.withPathOnly().toString().sanitizeDoc())

if (field.isDeprecated) {
addAttribute(deprecated)
Expand Down Expand Up @@ -1266,7 +1266,7 @@ class SwiftGenerator private constructor(
addDoc("\n%L\n", field.documentation.sanitizeDoc())
}
if (!forStorageType) {
addDoc("\nSource: %L\n", field.location.withPathOnly())
addDoc("\nSource: %L\n", field.location.withPathOnly().toString().sanitizeDoc())
}

if (field.isDeprecated) {
Expand Down
Loading