From fb882233f163a0648a412a27bdc7f3254765605c Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 30 Oct 2024 10:28:46 -0400 Subject: [PATCH 1/6] Add x-amzn-query-mode customization --- .../20e287a5-cee0-4a5f-9d08-4022dcdee843.json | 5 ++++ .../AwsQueryModeCustomization.kt | 28 +++++++++++++++++++ ...tlin.codegen.integration.KotlinIntegration | 1 + 3 files changed, 34 insertions(+) create mode 100644 .changes/20e287a5-cee0-4a5f-9d08-4022dcdee843.json create mode 100644 codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomization.kt diff --git a/.changes/20e287a5-cee0-4a5f-9d08-4022dcdee843.json b/.changes/20e287a5-cee0-4a5f-9d08-4022dcdee843.json new file mode 100644 index 00000000000..37442f4cdd2 --- /dev/null +++ b/.changes/20e287a5-cee0-4a5f-9d08-4022dcdee843.json @@ -0,0 +1,5 @@ +{ + "id": "20e287a5-cee0-4a5f-9d08-4022dcdee843", + "type": "misc", + "description": "Send x-amzn-query-mode=true for services with query-compatible trait" +} \ No newline at end of file diff --git a/codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomization.kt b/codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomization.kt new file mode 100644 index 00000000000..4e8de297cd5 --- /dev/null +++ b/codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomization.kt @@ -0,0 +1,28 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ +package aws.sdk.kotlin.codegen.customization + +import software.amazon.smithy.aws.traits.protocols.AwsQueryCompatibleTrait +import software.amazon.smithy.kotlin.codegen.KotlinSettings +import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration +import software.amazon.smithy.kotlin.codegen.model.hasTrait +import software.amazon.smithy.kotlin.codegen.rendering.protocol.MutateHeadersMiddleware +import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator +import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolMiddleware +import software.amazon.smithy.model.Model + +/** + * Send an extra `x-amzn-query-mode` header with a value of `true` for services which have the [AwsQueryCompatibleTrait] applied. + */ +class AwsQueryModeCustomization : KotlinIntegration { + override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = + model + .getShape(settings.service) + .get() + .hasTrait() + + override fun customizeMiddleware(ctx: ProtocolGenerator.GenerationContext, resolved: List): List = + resolved + MutateHeadersMiddleware(extraHeaders = mapOf("x-amzn-query-mode" to "true")) +} \ No newline at end of file diff --git a/codegen/aws-sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration b/codegen/aws-sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration index 3ef264034df..6d8199d3072 100644 --- a/codegen/aws-sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration +++ b/codegen/aws-sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration @@ -43,3 +43,4 @@ aws.sdk.kotlin.codegen.customization.s3.express.S3ExpressIntegration aws.sdk.kotlin.codegen.customization.s3.S3ExpiresIntegration aws.sdk.kotlin.codegen.BusinessMetricsIntegration aws.sdk.kotlin.codegen.smoketests.SmokeTestsDenyListIntegration +aws.sdk.kotlin.codegen.customization.AwsQueryModeCustomization From 4ec395903eccb62ca39b33865876b173b4bf3993 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 30 Oct 2024 10:32:35 -0400 Subject: [PATCH 2/6] ktlint --- .../kotlin/codegen/customization/AwsQueryModeCustomization.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomization.kt b/codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomization.kt index 4e8de297cd5..1d97a3c7a08 100644 --- a/codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomization.kt +++ b/codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomization.kt @@ -25,4 +25,4 @@ class AwsQueryModeCustomization : KotlinIntegration { override fun customizeMiddleware(ctx: ProtocolGenerator.GenerationContext, resolved: List): List = resolved + MutateHeadersMiddleware(extraHeaders = mapOf("x-amzn-query-mode" to "true")) -} \ No newline at end of file +} From 00241d294fff060eb0d85639bf3e9ebf15211d88 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 30 Oct 2024 11:29:38 -0400 Subject: [PATCH 3/6] Add tests --- .../AwsQueryModeCustomizationTest.kt | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt diff --git a/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt b/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt new file mode 100644 index 00000000000..f72e944dbed --- /dev/null +++ b/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt @@ -0,0 +1,131 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ +package aws.sdk.kotlin.codegen.customization + +import aws.sdk.kotlin.codegen.testutil.lines +import kotlin.test.Test +import kotlinx.coroutines.test.runTest +import software.amazon.smithy.kotlin.codegen.test.* +import kotlin.test.assertTrue + +class AwsQueryModeCustomizationTest { + private val queryCompatibleModel = """ + namespace com.test + + use aws.protocols#awsJson1_0 + use aws.protocols#awsQueryCompatible + use aws.api#service + + @awsJson1_0 + @awsQueryCompatible + @service(sdkId: "QueryCompatible") + service QueryCompatible { + version: "1.0.0", + operations: [GetFoo] + } + + @http(method: "POST", uri: "/foo") + operation GetFoo { + input: GetFooInput + } + + structure GetFooInput { + payload: String + } + """ + .trimIndent() + .toSmithyModel() + + private val nonQueryCompatibleModel = """ + namespace com.test + + use aws.protocols#awsJson1_0 + use aws.protocols#awsQueryCompatible + use aws.api#service + + @awsJson1_0 + @service(sdkId: "NonQueryCompatible") + service NonQueryCompatible { + version: "1.0.0", + operations: [GetFoo] + } + + @http(method: "POST", uri: "/foo") + operation GetFoo { + input: GetFooInput + } + + structure GetFooInput { + payload: String + } + """ + .trimIndent() + .toSmithyModel() + + @Test + fun testEnabledForQueryCompatibleModel() { + assertTrue { + AwsQueryModeCustomization() + .enabledForService(queryCompatibleModel, queryCompatibleModel.defaultSettings()) + } + } + + @Test + fun testNotExpectedForNonS3Model() { + assertTrue { + AwsQueryModeCustomization() + .enabledForService(nonQueryCompatibleModel, nonQueryCompatibleModel.defaultSettings()) + } + } + + @Test + fun `x-amzn-query-mode applied`() { + val ctx = queryCompatibleModel.newTestContext("QueryCompatible", integrations = listOf(AwsQueryModeCustomization())) + val generator = MockHttpProtocolGenerator(queryCompatibleModel) + generator.generateProtocolClient(ctx.generationCtx) + + ctx.generationCtx.delegator.finalize() + ctx.generationCtx.delegator.flushWriters() + + val actual = ctx.manifest.expectFileString("/src/main/kotlin/com/test/DefaultTestClient.kt") + + val getFooMethod = actual.lines(" override suspend fun getFoo(input: GetFooRequest): GetFooResponse {", " }") + + val expectedHeaderMutation = """ + op.install( + MutateHeaders().apply { + append("x-amzn-query-mode", "true") + } + ) + """.replaceIndent(" ") + + getFooMethod.shouldContainOnlyOnceWithDiff(expectedHeaderMutation) + } + + @Test + fun `x-amzn-query-mode NOT applied`() { + val ctx = nonQueryCompatibleModel.newTestContext("NonQueryCompatible", integrations = listOf()) + val generator = MockHttpProtocolGenerator(nonQueryCompatibleModel) + generator.generateProtocolClient(ctx.generationCtx) + + ctx.generationCtx.delegator.finalize() + ctx.generationCtx.delegator.flushWriters() + + val actual = ctx.manifest.expectFileString("/src/main/kotlin/com/test/DefaultTestClient.kt") + println(actual) + + val getFooMethod = actual.lines(" override suspend fun getFoo(input: GetFooRequest): GetFooResponse {", " }") + + val unexpectedHeaderMutation = """ + op.install( + MutateHeaders().apply { + append("x-amzn-query-mode", "true") + } + ) + """.replaceIndent(" ") + + getFooMethod.shouldNotContainOnlyOnceWithDiff(unexpectedHeaderMutation) + } +} \ No newline at end of file From eed77e9849077289fa9db3e3626307a14d148243 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 30 Oct 2024 11:36:09 -0400 Subject: [PATCH 4/6] Remove println --- .../codegen/customization/AwsQueryModeCustomizationTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt b/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt index f72e944dbed..7b684e4fd1b 100644 --- a/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt +++ b/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt @@ -114,7 +114,6 @@ class AwsQueryModeCustomizationTest { ctx.generationCtx.delegator.flushWriters() val actual = ctx.manifest.expectFileString("/src/main/kotlin/com/test/DefaultTestClient.kt") - println(actual) val getFooMethod = actual.lines(" override suspend fun getFoo(input: GetFooRequest): GetFooResponse {", " }") From ef6bfb63fd0cd1988f222f6757b2aac17011073c Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 30 Oct 2024 12:23:47 -0400 Subject: [PATCH 5/6] ktlint --- .../codegen/customization/AwsQueryModeCustomizationTest.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt b/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt index 7b684e4fd1b..346835ec150 100644 --- a/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt +++ b/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt @@ -5,9 +5,8 @@ package aws.sdk.kotlin.codegen.customization import aws.sdk.kotlin.codegen.testutil.lines -import kotlin.test.Test -import kotlinx.coroutines.test.runTest import software.amazon.smithy.kotlin.codegen.test.* +import kotlin.test.Test import kotlin.test.assertTrue class AwsQueryModeCustomizationTest { @@ -127,4 +126,4 @@ class AwsQueryModeCustomizationTest { getFooMethod.shouldNotContainOnlyOnceWithDiff(unexpectedHeaderMutation) } -} \ No newline at end of file +} From e60b99b078d1248cfcf5f86835369ada1784143f Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 30 Oct 2024 13:13:07 -0400 Subject: [PATCH 6/6] fix test assertion --- .../codegen/customization/AwsQueryModeCustomizationTest.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt b/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt index 346835ec150..b8d5e3e67e3 100644 --- a/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt +++ b/codegen/aws-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/customization/AwsQueryModeCustomizationTest.kt @@ -7,6 +7,7 @@ package aws.sdk.kotlin.codegen.customization import aws.sdk.kotlin.codegen.testutil.lines import software.amazon.smithy.kotlin.codegen.test.* import kotlin.test.Test +import kotlin.test.assertFalse import kotlin.test.assertTrue class AwsQueryModeCustomizationTest { @@ -72,8 +73,8 @@ class AwsQueryModeCustomizationTest { } @Test - fun testNotExpectedForNonS3Model() { - assertTrue { + fun testNotExpectedForNonQueryCompatibleModel() { + assertFalse { AwsQueryModeCustomization() .enabledForService(nonQueryCompatibleModel, nonQueryCompatibleModel.defaultSettings()) }