-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prefer EndpointResolver Bucket for S3 in http label, AccountId f…
…or S3Control host label (#660)
- Loading branch information
Ganesh Jangir
authored
Oct 31, 2022
1 parent
f296069
commit 31c8bb9
Showing
5 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...main/kotlin/software/amazon/smithy/aws/swift/codegen/model/AWSEndpointTraitTransformer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package software.amazon.smithy.aws.swift.codegen.model | ||
|
||
import software.amazon.smithy.model.Model | ||
import software.amazon.smithy.model.shapes.OperationShape | ||
import software.amazon.smithy.model.traits.EndpointTrait | ||
import software.amazon.smithy.model.transform.ModelTransformer | ||
import software.amazon.smithy.rulesengine.traits.StaticContextParamsTrait | ||
import software.amazon.smithy.swift.codegen.SwiftSettings | ||
import software.amazon.smithy.swift.codegen.integration.SwiftIntegration | ||
import software.amazon.smithy.swift.codegen.model.getTrait | ||
|
||
/** | ||
* This integration is responsible for removing the EndpointTrait from the operation | ||
* - For S3 Control, if hostPrefix is {AccountId}., then remove the Endpoint Trait because it is already handled | ||
* within the EndpointResolver | ||
*/ | ||
class AWSEndpointTraitTransformer : SwiftIntegration { | ||
override fun preprocessModel(model: Model, settings: SwiftSettings): Model { | ||
return when (settings.service.namespace) { | ||
"com.amazonaws.s3control" -> { | ||
ModelTransformer.create().mapShapes(model) { shape -> | ||
when (shape) { | ||
is OperationShape -> { | ||
val shapeBuilder = shape.toBuilder() | ||
shape.getTrait<StaticContextParamsTrait>()?.let { staticContextParamsTrait -> | ||
val requiresAccountId = | ||
staticContextParamsTrait.parameters["RequiresAccountId"]?.value | ||
.toString() | ||
.toBoolean() | ||
if (requiresAccountId) { | ||
shape.getTrait<EndpointTrait>()?.let { endpointTrait -> | ||
val hostPrefix = endpointTrait.hostPrefix.toString() | ||
if (hostPrefix == "{AccountId}.") { | ||
shapeBuilder.removeTrait(endpointTrait.toShapeId()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
shapeBuilder.build() | ||
} | ||
|
||
else -> shape | ||
} | ||
} | ||
} | ||
|
||
else -> model | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...src/main/kotlin/software/amazon/smithy/aws/swift/codegen/model/AWSHttpTraitTransformer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package software.amazon.smithy.aws.swift.codegen.model | ||
|
||
import software.amazon.smithy.model.Model | ||
import software.amazon.smithy.model.pattern.UriPattern | ||
import software.amazon.smithy.model.shapes.OperationShape | ||
import software.amazon.smithy.model.traits.HttpLabelTrait | ||
import software.amazon.smithy.model.traits.HttpTrait | ||
import software.amazon.smithy.model.transform.ModelTransformer | ||
import software.amazon.smithy.rulesengine.traits.ContextParamTrait | ||
import software.amazon.smithy.swift.codegen.SwiftSettings | ||
import software.amazon.smithy.swift.codegen.getOrNull | ||
import software.amazon.smithy.swift.codegen.integration.SwiftIntegration | ||
import software.amazon.smithy.swift.codegen.model.getTrait | ||
import software.amazon.smithy.swift.codegen.model.hasTrait | ||
|
||
/** | ||
* This integration is responsible for updating the `@httpLabel` trait to the input shape of an operation | ||
* - For S3, if the HttpLabel is /{BucketName}{Suffix} then update the trait with /{Suffix} because | ||
* the bucket name is already handled within the EndpointResolver | ||
*/ | ||
class AWSHttpTraitTransformer : SwiftIntegration { | ||
override fun preprocessModel(model: Model, settings: SwiftSettings): Model { | ||
return when (settings.service.namespace) { | ||
"com.amazonaws.s3" -> { | ||
ModelTransformer.create().mapShapes(model) { shape -> | ||
when (shape) { | ||
is OperationShape -> { | ||
val shapeBuilder = shape.toBuilder() | ||
shape.input.getOrNull()?.let { input -> | ||
val inputShape = model.expectShape(input.toShapeId()) | ||
shape.getTrait<HttpTrait>()?.let { httpTrait -> | ||
val uriPattern = httpTrait.uri.toString() | ||
val httpTraitBuilder = httpTrait.toBuilder() | ||
val members = inputShape.members() ?: emptyList() | ||
members.forEach { member -> | ||
if (member.hasTrait<ContextParamTrait>() && | ||
member.hasTrait<HttpLabelTrait>() && | ||
member.memberName == "Bucket" && | ||
uriPattern.startsWith("/{Bucket}") | ||
) { | ||
var newPattern = uriPattern.substring("/{Bucket}".length) | ||
if (!newPattern.startsWith("/")) { | ||
newPattern = "/$newPattern" | ||
} | ||
httpTraitBuilder.uri(UriPattern.parse(newPattern)) | ||
shapeBuilder.addTrait(httpTraitBuilder.build()) | ||
} | ||
} | ||
} | ||
} | ||
shapeBuilder.build() | ||
} | ||
|
||
else -> shape | ||
} | ||
} | ||
} | ||
|
||
else -> model | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters