Skip to content

Commit f1f9e8f

Browse files
authored
fix: update test operation generator to find any operation in the service closure (#3922)
## Motivation and Context When using operations nested under resources, the current search will raise "NoSuchElementException". This fixes the search to include all operations within the service closure ## Description The error is visible in client generates like this: ``` Projection rust-client failed: java.util.NoSuchElementException: Collection contains no element matching the predicate. java.util.NoSuchElementException: Collection contains no element matching the predicate. at software.amazon.smithy.rustsdk.endpoints.OperationInputTestGeneratorKt.operationId(OperationInputTestGenerator.kt:242) at software.amazon.smithy.rustsdk.endpoints.OperationInputTestGenerator$operationInvocation$1.invoke(OperationInputTestGenerator.kt:183) at software.amazon.smithy.rustsdk.endpoints.OperationInputTestGenerator$operationInvocation$1.invoke(OperationInputTestGenerator.kt:180) ``` ## Testing `gradle :aws:sdk-codegen:check` ## Checklist - [ ] For changes to the smithy-rs codegen or runtime crates, I have created a changelog entry Markdown file in the `.changelog` directory, specifying "client," "server," or both in the `applies_to` key. - [ ] For changes to the AWS SDK, generated SDK code, or SDK runtime crates, I have created a changelog entry Markdown file in the `.changelog` directory, specifying "aws-sdk-rust" in the `applies_to` key. ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
1 parent cd4d650 commit f1f9e8f

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/endpoints/OperationInputTestGenerator.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package software.amazon.smithy.rustsdk.endpoints
77

8+
import software.amazon.smithy.model.knowledge.TopDownIndex
89
import software.amazon.smithy.model.node.Node
910
import software.amazon.smithy.model.shapes.OperationShape
1011
import software.amazon.smithy.model.shapes.ShapeId
@@ -222,4 +223,7 @@ class OperationInputTestGenerator(_ctx: ClientCodegenContext, private val test:
222223
}
223224

224225
fun ClientCodegenContext.operationId(testOperationInput: EndpointTestOperationInput): ShapeId =
225-
this.serviceShape.allOperations.first { it.name == testOperationInput.operationName }
226+
TopDownIndex.of(this.model)
227+
.getContainedOperations(this.serviceShape)
228+
.map { it.toShapeId() }
229+
.first { it.name == testOperationInput.operationName }

aws/sdk-codegen/src/test/kotlin/software/amazon/smithy/rustsdk/OperationInputTestGeneratorTests.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,50 @@ class OperationInputTestGeneratorTests {
5555
assertEquals("operations#Ping", operationId.toString())
5656
}
5757

58+
@Test
59+
fun `finds operation shape by name from nested operations`() {
60+
val prefix = "\$version: \"2\""
61+
val operationModel =
62+
"""
63+
$prefix
64+
namespace operations.bells
65+
66+
resource Bell {
67+
operations: [Ding]
68+
}
69+
70+
operation Ding {}
71+
""".trimIndent()
72+
val serviceModel =
73+
"""
74+
$prefix
75+
namespace service
76+
77+
use operations.bells#Bell
78+
79+
service MyService {
80+
resources: [Bell]
81+
}
82+
""".trimIndent()
83+
84+
val model =
85+
Model.assembler()
86+
.discoverModels()
87+
.addUnparsedModel("operation.smithy", operationModel)
88+
.addUnparsedModel("main.smithy", serviceModel)
89+
.assemble()
90+
.unwrap()
91+
92+
val context = testClientCodegenContext(model)
93+
val testOperationInput =
94+
EndpointTestOperationInput.builder()
95+
.operationName("Ding")
96+
.build()
97+
98+
val operationId = context.operationId(testOperationInput)
99+
assertEquals("operations.bells#Ding", operationId.toString())
100+
}
101+
58102
@Test
59103
fun `fails for operation name not found`() {
60104
val model =

0 commit comments

Comments
 (0)