Skip to content

Commit

Permalink
fix: route53 custom error unmarshalling (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
0marperez authored Jul 7, 2023
1 parent b3ef626 commit d44c039
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.codegen.customization.route53

import aws.sdk.kotlin.codegen.protocols.core.AwsHttpBindingProtocolGenerator
import aws.sdk.kotlin.codegen.sdkId
import software.amazon.smithy.kotlin.codegen.KotlinSettings
import software.amazon.smithy.kotlin.codegen.core.withBlock
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration
import software.amazon.smithy.kotlin.codegen.integration.SectionWriterBinding
import software.amazon.smithy.kotlin.codegen.model.expectShape
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.ServiceShape

class ChangeResourceRecordSetsUnmarshallingIntegration : KotlinIntegration {
override val sectionWriters: List<SectionWriterBinding> = listOf(
SectionWriterBinding(AwsHttpBindingProtocolGenerator.ProtocolErrorDeserialization) { writer, _ ->
writer.withBlock("payload?.let {", "}\n") {
withBlock("aws.sdk.kotlin.services.route53.internal.parseRestXmlInvalidChangeBatchResponse(payload)?.let {", "}") {
write("setAseErrorMetadata(it.exception, wrappedResponse, it.errorDetails)")
write("throw it.exception")
}
}
},
)

override fun enabledForService(model: Model, settings: KotlinSettings) =
model.expectShape<ServiceShape>(settings.service).sdkId.equals("route 53", ignoreCase = true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import aws.sdk.kotlin.codegen.protocols.protocoltest.AwsHttpProtocolUnitTestResp
import software.amazon.smithy.aws.traits.protocols.AwsQueryCompatibleTrait
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.kotlin.codegen.core.*
import software.amazon.smithy.kotlin.codegen.integration.SectionId
import software.amazon.smithy.kotlin.codegen.lang.KotlinTypes
import software.amazon.smithy.kotlin.codegen.model.buildSymbol
import software.amazon.smithy.kotlin.codegen.model.hasTrait
Expand Down Expand Up @@ -99,6 +100,8 @@ abstract class AwsHttpBindingProtocolGenerator : HttpBindingProtocolGenerator()
}
}

object ProtocolErrorDeserialization : SectionId

protected open fun renderThrowOperationError(
ctx: ProtocolGenerator.GenerationContext,
op: OperationShape,
Expand All @@ -108,6 +111,7 @@ abstract class AwsHttpBindingProtocolGenerator : HttpBindingProtocolGenerator()
writer.write("val payload = response.body.#T()", RuntimeTypes.Http.readAll)
.write("val wrappedResponse = response.#T(payload)", RuntimeTypes.AwsProtocolCore.withPayload)
.write("")
.declareSection(ProtocolErrorDeserialization)
.write("val errorDetails = try {")
.indent()
.call {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ aws.sdk.kotlin.codegen.customization.RemoveEventStreamOperations
aws.sdk.kotlin.codegen.customization.flexiblechecksums.FlexibleChecksumsRequest
aws.sdk.kotlin.codegen.customization.flexiblechecksums.FlexibleChecksumsResponse
aws.sdk.kotlin.codegen.customization.route53.TrimResourcePrefix
aws.sdk.kotlin.codegen.customization.route53.ChangeResourceRecordSetsUnmarshallingIntegration
aws.sdk.kotlin.codegen.customization.s3.ClientConfigIntegration
aws.sdk.kotlin.codegen.customization.s3.ContinueIntegration
aws.sdk.kotlin.codegen.customization.s3.HttpPathFilter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.codegen.customization.route53

import org.junit.jupiter.api.Test
import software.amazon.smithy.kotlin.codegen.test.defaultSettings
import software.amazon.smithy.kotlin.codegen.test.prependNamespaceAndService
import software.amazon.smithy.kotlin.codegen.test.toSmithyModel
import software.amazon.smithy.model.Model
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class ChangeResourceRecordSetsUnmarshallingIntegrationTest {
@Test
fun nonRoute53ModelIntegration() {
val model = sampleModel("not route 53")
val isEnabledForModel = ChangeResourceRecordSetsUnmarshallingIntegration().enabledForService(model, model.defaultSettings())
assertFalse(isEnabledForModel)
}

@Test
fun route53ModelIntegration() {
val model = sampleModel("route 53")
val isEnabledForModel = ChangeResourceRecordSetsUnmarshallingIntegration().enabledForService(model, model.defaultSettings())
assertTrue(isEnabledForModel)
}
}

private fun sampleModel(serviceName: String): Model =
"""
@http(method: "PUT", uri: "/foo")
operation Foo { }
@http(method: "POST", uri: "/bar")
operation Bar { }
"""
.prependNamespaceAndService(operations = listOf("Foo", "Bar"), serviceName = serviceName)
.toSmithyModel()

0 comments on commit d44c039

Please sign in to comment.