Skip to content

Commit 2ccdf40

Browse files
authored
fix: temporarily bypass httpchecksum traits until full flexible checksum support is available (#558)
1 parent eea3984 commit 2ccdf40

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
package aws.sdk.kotlin.codegen.customization
6+
7+
import software.amazon.smithy.aws.traits.HttpChecksumTrait
8+
import software.amazon.smithy.kotlin.codegen.KotlinSettings
9+
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration
10+
import software.amazon.smithy.kotlin.codegen.model.expectTrait
11+
import software.amazon.smithy.kotlin.codegen.model.hasTrait
12+
import software.amazon.smithy.kotlin.codegen.model.shapes
13+
import software.amazon.smithy.kotlin.codegen.utils.getOrNull
14+
import software.amazon.smithy.model.Model
15+
import software.amazon.smithy.model.shapes.MemberShape
16+
import software.amazon.smithy.model.shapes.OperationShape
17+
import software.amazon.smithy.model.transform.ModelTransformer
18+
import java.util.logging.Logger
19+
20+
/**
21+
* Temporary integration to remove flexible checksum fields from models.
22+
* TODO https://github.com/awslabs/aws-sdk-kotlin/issues/557
23+
*/
24+
class RemoveChecksumSelectionFields : KotlinIntegration {
25+
private val logger = Logger.getLogger(javaClass.name)
26+
27+
override val order: Byte = -127
28+
29+
override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = model
30+
.shapes<OperationShape>()
31+
.any { it.hasTrait<HttpChecksumTrait>() }
32+
33+
override fun preprocessModel(model: Model, settings: KotlinSettings): Model {
34+
val dropMembers = model
35+
.shapes<OperationShape>()
36+
.filter { it.hasTrait<HttpChecksumTrait>() }
37+
.flatMap { op ->
38+
val trait = op.expectTrait<HttpChecksumTrait>()
39+
40+
val requestAlgorithmMember = trait.requestAlgorithmMember.getOrNull()
41+
val requestValidationModeMember = trait.requestValidationModeMember.getOrNull()
42+
43+
listOfNotNull(requestAlgorithmMember, requestValidationModeMember)
44+
.map { findInputMember(model, op, it) }
45+
}
46+
.toSet()
47+
48+
return ModelTransformer.create().filterShapes(model) { shape ->
49+
when (shape) {
50+
is MemberShape -> (shape !in dropMembers).also {
51+
if (!it) {
52+
logger.warning("Removed $shape from model because it is a flexible checksum member")
53+
}
54+
}
55+
else -> true
56+
}
57+
}
58+
}
59+
}
60+
61+
private fun findInputMember(model: Model, op: OperationShape, name: String): MemberShape =
62+
model.expectShape(op.inputShape).members().first { it.memberName == name }

codegen/smithy-aws-kotlin-codegen/src/main/resources/META-INF/services/software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ aws.sdk.kotlin.codegen.customization.glacier.GlacierBodyChecksum
1717
aws.sdk.kotlin.codegen.customization.machinelearning.MachineLearningEndpointCustomization
1818
aws.sdk.kotlin.codegen.customization.BackfillOptionalAuth
1919
aws.sdk.kotlin.codegen.customization.RemoveEventStreamOperations
20+
aws.sdk.kotlin.codegen.customization.RemoveChecksumSelectionFields
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
package aws.sdk.kotlin.codegen.customization
6+
7+
import software.amazon.smithy.kotlin.codegen.model.expectShape
8+
import software.amazon.smithy.kotlin.codegen.test.newTestContext
9+
import software.amazon.smithy.kotlin.codegen.test.prependNamespaceAndService
10+
import software.amazon.smithy.kotlin.codegen.test.toSmithyModel
11+
import software.amazon.smithy.model.shapes.OperationShape
12+
import software.amazon.smithy.model.shapes.StructureShape
13+
import kotlin.test.Test
14+
import kotlin.test.assertFalse
15+
import kotlin.test.assertTrue
16+
17+
class RemoveChecksumSelectionFieldsTest {
18+
@Test
19+
fun testRemovingChecksumFields() {
20+
val model = """
21+
@httpChecksum(
22+
requestValidationModeMember: "checksumMode"
23+
)
24+
operation GetObject {
25+
input: GetObjectRequest,
26+
}
27+
28+
structure GetObjectRequest {
29+
key: String,
30+
checksumMode: ChecksumMode
31+
}
32+
33+
@enum([
34+
{
35+
value: "ENABLED",
36+
name: "ENABLED"
37+
}
38+
])
39+
string ChecksumMode
40+
41+
@httpChecksum(
42+
requestAlgorithmMember: "checksumAlgorithm"
43+
)
44+
operation PutObject {
45+
input: PutObjectRequest,
46+
}
47+
48+
structure PutObjectRequest {
49+
key: String,
50+
checksumAlgorithm: ChecksumAlgorithm
51+
}
52+
53+
@enum([
54+
{
55+
value: "CRC32C",
56+
name: "CRC32C"
57+
},
58+
{
59+
value: "CRC32",
60+
name: "CRC32"
61+
},
62+
{
63+
value: "SHA1",
64+
name: "SHA1"
65+
},
66+
{
67+
value: "SHA256",
68+
name: "SHA256"
69+
}
70+
])
71+
string ChecksumAlgorithm
72+
""".prependNamespaceAndService(
73+
imports = listOf("aws.protocols#httpChecksum"),
74+
operations = listOf("GetObject", "PutObject"),
75+
).toSmithyModel()
76+
77+
val ctx = model.newTestContext()
78+
val transformed = RemoveChecksumSelectionFields().preprocessModel(model, ctx.generationCtx.settings)
79+
80+
val getOp = transformed.expectShape<OperationShape>("com.test#PutObject")
81+
val getReq = transformed.expectShape<StructureShape>(getOp.inputShape)
82+
assertTrue("key" in getReq.memberNames, "Expected 'key' in request object")
83+
assertFalse("checksumMode" in getReq.memberNames, "Unexpected 'checksumMode' in request object")
84+
85+
val putOp = transformed.expectShape<OperationShape>("com.test#PutObject")
86+
val putReq = transformed.expectShape<StructureShape>(putOp.inputShape)
87+
assertTrue("key" in putReq.memberNames, "Expected 'key' in request object")
88+
assertFalse("checksumAlgorithm" in putReq.memberNames, "Unexpected 'checksumAlgorithm' in request object")
89+
}
90+
}

0 commit comments

Comments
 (0)