Skip to content
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 7 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -295,7 +295,7 @@ private val commonHtmlTags = setOf(
// NOTE: Currently we look for specific strings of Html tags commonly found in docs
// and remove them. A better solution would be to generally convert from HTML to "pure"
// markdown such that formatting is preserved.
// TODO: https://www.pivotaltracker.com/story/show/177053427
// TODO: https://github.com/awslabs/smithy-kotlin/issues/136
private fun sanitizeDocumentation(doc: String): String = doc
.stripAll(commonHtmlTags)
// Docs can have valid $ characters that shouldn't run through formatters.
Expand Down
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]")
Copy link
Contributor

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.

Copy link
Contributor Author

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.

}
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
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)
}
}
}