Unverified Commit a6161140 authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Fix bug where idempotency tokens weren't discovered on resource-bound operations (#425)

parent 281ee445
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ package software.amazon.smithy.rust.codegen.smithy.generators.config

import software.amazon.smithy.model.Model
import software.amazon.smithy.model.knowledge.OperationIndex
import software.amazon.smithy.model.knowledge.TopDownIndex
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.traits.IdempotencyTokenTrait
import software.amazon.smithy.rust.codegen.rustlang.RustWriter
@@ -69,7 +70,8 @@ sealed class ServiceConfig(name: String) : Section(name) {
// TODO: if this becomes hot, it may need to be cached in a knowledge index
fun ServiceShape.needsIdempotencyToken(model: Model): Boolean {
    val operationIndex = OperationIndex.of(model)
    return this.allOperations.flatMap { operationIndex.getInputMembers(it).values }.any { it.hasTrait<IdempotencyTokenTrait>() }
    val topDownIndex = TopDownIndex.of(model)
    return topDownIndex.getContainedOperations(this.id).flatMap { operationIndex.getInputMembers(it).values }.any { it.hasTrait<IdempotencyTokenTrait>() }
}

typealias ConfigCustomization = NamedSectionGenerator<ServiceConfig>
+24 −0
Original line number Diff line number Diff line
@@ -52,6 +52,30 @@ internal class ServiceConfigGeneratorTest {
        withoutToken.lookup<ServiceShape>("com.example#HelloService").needsIdempotencyToken(withoutToken) shouldBe false
    }

    @Test
    fun `find idempotency token via resources`() {
        val model = """
            namespace com.example
            service ResourceService {
                resources: [Resource],
                version: "1"
            }

            resource Resource {
                operations: [CreateResource]
            }
            operation CreateResource {
                input: IdempotentInput
            }

            structure IdempotentInput {
                @idempotencyToken
                tok: String
            }
        """.asSmithyModel()
        model.lookup<ServiceShape>("com.example#ResourceService").needsIdempotencyToken(model) shouldBe true
    }

    @Test
    fun `generate customizations as specified`() {
        class ServiceCustomizer : NamedSectionGenerator<ServiceConfig>() {