-
Notifications
You must be signed in to change notification settings - Fork 26
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
fix: escape brackets in documentation traits #401
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
684a219
fix: escape brackets in generated kdoc when identifier is invalid
aajtodd eb303e7
fix: escape square brackets in all documentation traits
aajtodd 9e643a7
fix: handle nested brackets
aajtodd 00ac1ba
you win again tests
aajtodd 3c4c082
Merge remote-tracking branch 'origin/main' into fix-generated-kdoc
aajtodd 1269c2f
Merge remote-tracking branch 'origin/main' into fix-generated-kdoc
aajtodd 9e2a673
Merge remote-tracking branch 'origin/main' into fix-generated-kdoc
aajtodd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...n/src/main/kotlin/software/amazon/smithy/kotlin/codegen/lang/DocumentationPreprocessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package software.amazon.smithy.kotlin.codegen.lang | ||
|
||
import software.amazon.smithy.kotlin.codegen.KotlinSettings | ||
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration | ||
import software.amazon.smithy.model.Model | ||
import software.amazon.smithy.model.traits.DocumentationTrait | ||
import software.amazon.smithy.model.transform.ModelTransformer | ||
|
||
/** | ||
* Sanitize all instances of [DocumentationTrait] | ||
*/ | ||
class DocumentationPreprocessor : KotlinIntegration { | ||
|
||
override fun preprocessModel(model: Model, settings: KotlinSettings): Model { | ||
val transformer = ModelTransformer.create() | ||
return transformer.mapTraits(model) { _, trait -> | ||
when (trait) { | ||
is DocumentationTrait -> { | ||
val docs = sanitize(trait.value) | ||
DocumentationTrait(docs, trait.sourceLocation) | ||
} | ||
else -> trait | ||
} | ||
} | ||
} | ||
|
||
// KDoc comments use inline markdown. Replace square brackets with escaped equivalents so that they | ||
// are not rendered as invalid links | ||
private fun sanitize(str: String): String = str.replace(Regex("(\\[)(.*)(\\])"), "[$2]") | ||
} |
1 change: 1 addition & 0 deletions
1
...ces/META-INF/services/software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
software.amazon.smithy.kotlin.codegen.lang.BuiltinPreprocessor | ||
software.amazon.smithy.kotlin.codegen.lang.DocumentationPreprocessor |
71 changes: 71 additions & 0 deletions
71
...c/test/kotlin/software/amazon/smithy/kotlin/codegen/lang/DocumentationPreprocessorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package software.amazon.smithy.kotlin.codegen.lang | ||
|
||
import org.junit.jupiter.api.Test | ||
import software.amazon.smithy.kotlin.codegen.KotlinSettings | ||
import software.amazon.smithy.kotlin.codegen.model.expectTrait | ||
import software.amazon.smithy.kotlin.codegen.test.toSmithyModel | ||
import software.amazon.smithy.model.shapes.ShapeId | ||
import software.amazon.smithy.model.traits.DocumentationTrait | ||
import kotlin.test.assertEquals | ||
|
||
class DocumentationPreprocessorTest { | ||
@Test | ||
fun itEscapesSquareBrackets() { | ||
// https://github.com/awslabs/aws-sdk-kotlin/issues/153 | ||
val model = """ | ||
namespace com.test | ||
|
||
service FooService { | ||
version: "1.0.0" | ||
} | ||
|
||
@documentation("This should not be modified") | ||
structure Foo { | ||
@documentation("member docs") | ||
Unit: Unit, | ||
Str: String | ||
} | ||
|
||
@documentation("UserName@[SubDomain.]Domain.TopLevelDomain") | ||
structure Unit { | ||
Int: Integer | ||
} | ||
|
||
union MyUnion { | ||
@documentation("foo [bar baz qux] quux") | ||
Integer: Integer, | ||
String: String, | ||
Unit: Unit | ||
} | ||
""".toSmithyModel() | ||
|
||
val settings = KotlinSettings( | ||
ShapeId.from("com.test#FooService"), | ||
KotlinSettings.PackageSettings( | ||
"test", | ||
"1.0", | ||
"" | ||
), | ||
"Foo" | ||
) | ||
|
||
val integration = DocumentationPreprocessor() | ||
val modified = integration.preprocessModel(model, settings) | ||
val expectedDocs = listOf( | ||
"com.test#Foo" to "This should not be modified", | ||
"com.test#Foo\$Unit" to "member docs", | ||
"com.test#Unit" to "UserName@[SubDomain.]Domain.TopLevelDomain", | ||
"com.test#MyUnion\$Integer" to "foo [bar baz qux] quux", | ||
) | ||
expectedDocs.forEach { (shapeId, expected) -> | ||
val shape = modified.expectShape(ShapeId.from(shapeId)) | ||
val docs = shape.expectTrait<DocumentationTrait>().value | ||
assertEquals(expected, docs) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Correctness: This regex won't capture nested braces or multiple braces on a line. For example:
a [ b [ c ] d ]
→a [ b [ c ] d ]
a [ b ] c [ d ]
→a [ b ] c [ d ]
This won't capture unpaired braces either, but I'm unclear if those are a problem or not.
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.
Unpaired braces shouldn't be a problem as far as I know since they won't be processed as inline markdown links.