-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sanitize generated comments #39
Conversation
We're seeing issues with the codegen in which comments containing `*/` literals aren't being escaped. (An example can be found in [`google.longrunning.Operations.ListOperations`][1]). KotlinPoet has an [open issue for sanitizing comments][2], so for now, borrow [their implementation in Wire][3]. [1]: https://bufbuild.internal/googleapis/googleapis/docs/main:google.longrunning#google.longrunning.Operations.ListOperations [2]: square/kotlinpoet#887 [3]: square/wire#1445
(In draft because I'm still testing this 😅) |
The fix looks sensible to me. I'm not sure why whitespace is modified, and I believe A test for this would be very nice to have. What about a proto file with a service that has offending comments attached? I think it should be generated automatically when put into |
Following the [current implementation][1]. [1]: https://github.com/square/wire/blob/efc66f08ea080b084bb822584eb2dca9b5ff82c7/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt#L2815-L2816
No reason to have this above the other private func, so they can all be together.
Sounds good - I'm less familiar with this, but looks to me like we should be changing literal |
ok, added a simple test case in 09cd4bb - happy to extend it in any way that would make us more confident. |
Influence of Go is too strong :^)
generated code looks like this FWIW, which is valid kotlin: // Code generated by connect-kotlin. DO NOT EDIT.
//
// Source: buf/evilcomments/v1/evilcomments.proto
//
package buf.evilcomments.v1
import build.buf.connect.Headers
import build.buf.connect.MethodSpec
import build.buf.connect.ProtocolClientInterface
import build.buf.connect.ResponseMessage
import build.buf.connect.http.Cancelable
import kotlin.Unit
public class EvilCommentsServiceClient(
private val client: ProtocolClientInterface,
) : EvilCommentsServiceClientInterface {
/**
* This comment contains characters that should be escaped.
* @ is valid in KDoc, but not in proto.
* Comments in KDoc use C-style block comments, so */ and /* should be escaped.
* \[ and \] characters should also be escaped.
*/
public override suspend fun evilComments(request: Evilcomments.EvilCommentsRequest,
headers: Headers): ResponseMessage<Evilcomments.EvilCommentsResponse> = client.unary(
request,
headers,
MethodSpec(
"buf.evilcomments.v1.EvilCommentsService/EvilComments",
buf.evilcomments.v1.Evilcomments.EvilCommentsRequest::class,
buf.evilcomments.v1.Evilcomments.EvilCommentsResponse::class
),
)
/**
* This comment contains characters that should be escaped.
* @ is valid in KDoc, but not in proto.
* Comments in KDoc use C-style block comments, so */ and /* should be escaped.
* \[ and \] characters should also be escaped.
*/
public override fun evilComments(
request: Evilcomments.EvilCommentsRequest,
headers: Headers,
onResult: (ResponseMessage<Evilcomments.EvilCommentsResponse>) -> Unit,
): Cancelable = client.unary(
request,
headers,
MethodSpec(
"buf.evilcomments.v1.EvilCommentsService/EvilComments",
buf.evilcomments.v1.Evilcomments.EvilCommentsRequest::class,
buf.evilcomments.v1.Evilcomments.EvilCommentsResponse::class
),
onResult
)
} |
.replace("\\*/".toRegex(), "*/") | ||
.replace("/\\*".toRegex(), "/*") | ||
.replace("""[""", """\[""") | ||
.replace("""]""", """\]""") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we may want to do:
.replace("""[""", "[")
.replace("""]""", "]")
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should fix the much more pressing issue with */
and /*
, thank you for adding the tests!
Not sure about escaping []
though, see comment below.
protoc-gen-connect-kotlin/src/main/kotlin/build/buf/protocgen/connect/Generator.kt
Outdated
Show resolved
Hide resolved
Based on [this suggestion][1]. [1]: #39 (comment)
We're seeing issues with the codegen in which comments containing
*/
literals aren't being escaped. (An example can be found ingoogle.longrunning.Operations.ListOperations
).KotlinPoet has an open issue for sanitizing comments, so for now, borrow their implementation in Wire.