Skip to content

Commit 7b1b313

Browse files
authored
fix: use the original service shape name to construct x-amz-target header (#126)
1 parent f64a997 commit 7b1b313

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

client-runtime/protocols/aws-json-protocols/common/src/aws/sdk/kotlin/runtime/protocol/json/AwsJsonProtocol.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ import software.aws.clientrt.util.get
2222
*/
2323
@InternalSdkApi
2424
public class AwsJsonProtocol(config: Config) : Feature {
25-
private val version: String = requireNotNull(config.version) { "AWS JSON Protocol version must be specified" }
25+
private val serviceShapeName: String = requireNotNull(config.serviceShapeName) { "AWS JSON protocol service name must be specified" }
26+
private val version: String = requireNotNull(config.version) { "AWS JSON protocol version must be specified" }
2627

2728
public class Config {
29+
/**
30+
* The original service (shape) name
31+
*/
32+
public var serviceShapeName: String? = null
33+
2834
/**
2935
* The protocol version e.g. "1.0"
3036
*/
@@ -44,11 +50,10 @@ public class AwsJsonProtocol(config: Config) : Feature {
4450
operation.execution.mutate.intercept { req, next ->
4551
val context = req.context
4652
// required context elements
47-
val serviceName = context[SdkClientOption.ServiceName]
4853
val operationName = context[SdkClientOption.OperationName]
4954

5055
// see: https://awslabs.github.io/smithy/1.0/spec/aws/aws-json-1_0-protocol.html#protocol-behaviors
51-
req.subject.headers.append("X-Amz-Target", "$serviceName.$operationName")
56+
req.subject.headers.append("X-Amz-Target", "$serviceShapeName.$operationName")
5257
req.subject.headers.setMissing("Content-Type", "application/x-amz-json-$version")
5358

5459
if (req.subject.body is HttpBody.Empty) {

client-runtime/protocols/aws-json-protocols/common/test/aws/sdk/kotlin/runtime/protocol/json/AwsJsonProtocolTest.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,17 @@ class AwsJsonProtocolTest {
4242
}
4343
val client = sdkHttpClient(mockEngine)
4444
op.install(AwsJsonProtocol) {
45+
serviceShapeName = "FooService_blah"
4546
version = "1.1"
4647
}
4748

4849
op.roundTrip(client, Unit)
4950
val request = op.context[HttpOperationContext.HttpCallList].last().request
5051

5152
assertEquals("application/x-amz-json-1.1", request.headers["Content-Type"])
52-
assertEquals("FooService.Bar", request.headers["X-Amz-Target"])
53+
// ensure we use the original shape id name, NOT the one from the context
54+
// see: https://github.com/awslabs/smithy-kotlin/issues/316
55+
assertEquals("FooService_blah.Bar", request.headers["X-Amz-Target"])
5356
}
5457

5558
@Test
@@ -72,6 +75,7 @@ class AwsJsonProtocolTest {
7275
}
7376
val client = sdkHttpClient(mockEngine)
7477
op.install(AwsJsonProtocol) {
78+
serviceShapeName = "FooService"
7579
version = "1.1"
7680
}
7781

@@ -109,6 +113,7 @@ class AwsJsonProtocolTest {
109113
}
110114
val client = sdkHttpClient(mockEngine)
111115
op.install(AwsJsonProtocol) {
116+
serviceShapeName = "FooService"
112117
version = "1.1"
113118
}
114119

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/protocols/AwsJson1_0.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class AwsJson1_0 : AwsHttpBindingProtocolGenerator() {
2828
override fun getDefaultHttpMiddleware(ctx: ProtocolGenerator.GenerationContext): List<ProtocolMiddleware> {
2929
val httpMiddleware = super.getDefaultHttpMiddleware(ctx)
3030
val awsJsonMiddleware = listOf(
31-
AwsJsonProtocolMiddleware("1.0"),
31+
AwsJsonProtocolMiddleware(ctx.settings.service, "1.0"),
3232
AwsJsonModeledExceptionsMiddleware(ctx, getProtocolHttpBindingResolver(ctx))
3333
)
3434

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/protocols/AwsJson1_1.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class AwsJson1_1 : AwsHttpBindingProtocolGenerator() {
3333
override fun getDefaultHttpMiddleware(ctx: ProtocolGenerator.GenerationContext): List<ProtocolMiddleware> {
3434
val httpMiddleware = super.getDefaultHttpMiddleware(ctx)
3535
val awsJsonFeatures = listOf(
36-
AwsJsonProtocolMiddleware("1.1"),
36+
AwsJsonProtocolMiddleware(ctx.settings.service, "1.1"),
3737
AwsJsonModeledExceptionsMiddleware(ctx, getProtocolHttpBindingResolver(ctx))
3838
)
3939

codegen/smithy-aws-kotlin-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/protocols/json/AwsJsonProtocolMiddleware.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import software.amazon.smithy.kotlin.codegen.core.KotlinWriter
1010
import software.amazon.smithy.kotlin.codegen.model.buildSymbol
1111
import software.amazon.smithy.kotlin.codegen.model.namespace
1212
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolMiddleware
13+
import software.amazon.smithy.model.shapes.ShapeId
1314

1415
/**
1516
* Configure the AwsJsonProtocol middleware
1617
* @param protocolVersion The AWS JSON protocol version (e.g. "1.0", "1.1", etc)
1718
*/
18-
class AwsJsonProtocolMiddleware(private val protocolVersion: String) : ProtocolMiddleware {
19+
class AwsJsonProtocolMiddleware(
20+
private val serviceShapeId: ShapeId,
21+
private val protocolVersion: String
22+
) : ProtocolMiddleware {
1923
override val name: String = "AwsJsonProtocol"
2024

2125
override fun addImportsAndDependencies(writer: KotlinWriter) {
@@ -29,6 +33,7 @@ class AwsJsonProtocolMiddleware(private val protocolVersion: String) : ProtocolM
2933
}
3034

3135
override fun renderConfigure(writer: KotlinWriter) {
36+
writer.write("serviceShapeName = #S", serviceShapeId.name)
3237
writer.write("version = #S", protocolVersion)
3338
}
3439
}

0 commit comments

Comments
 (0)