From f226295b24783c967d7f8f1f91d65eac5a16a3c7 Mon Sep 17 00:00:00 2001 From: John Woo <1911939+wooj2@users.noreply.github.com> Date: Mon, 1 Nov 2021 16:21:59 -0700 Subject: [PATCH] chore: upgrade to smithy 1.13.0 (#383) --- gradle.properties | 2 +- .../integration/HttpBindingProtocolGenerator.kt | 2 +- .../middlewares/ContentLengthMiddleware.kt | 9 +++++++-- .../middlewares/ContentTypeMiddleware.kt | 14 +++++++++----- .../middlewares/OperationInputBodyMiddleware.kt | 4 +--- .../middlewares/handlers/MiddlewareShapeUtils.kt | 10 ++++++++++ .../HttpProtocolUnitTestRequestGeneratorTests.kt | 4 ---- 7 files changed, 29 insertions(+), 16 deletions(-) diff --git a/gradle.properties b/gradle.properties index 16e60741c..2da2948dc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ kotlin.code.style=official # config # codegen -smithyVersion=1.12.0 +smithyVersion=1.13.0 # kotlin kotlinVersion=1.5.0 kotlin.native.ignoreDisabledTargets=true diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpBindingProtocolGenerator.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpBindingProtocolGenerator.kt index 3322188af..5f5e022e5 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpBindingProtocolGenerator.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpBindingProtocolGenerator.kt @@ -392,7 +392,7 @@ abstract class HttpBindingProtocolGenerator : ProtocolGenerator { operationMiddleware.appendMiddleware(operation, ContentTypeMiddleware(ctx.model, ctx.symbolProvider, resolver.determineRequestContentType(operation))) operationMiddleware.appendMiddleware(operation, OperationInputBodyMiddleware(ctx.model, ctx.symbolProvider)) - operationMiddleware.appendMiddleware(operation, ContentLengthMiddleware()) + operationMiddleware.appendMiddleware(operation, ContentLengthMiddleware(ctx.model)) operationMiddleware.appendMiddleware(operation, LoggingMiddleware()) operationMiddleware.appendMiddleware(operation, DeserializeMiddleware()) diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/ContentLengthMiddleware.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/ContentLengthMiddleware.kt index 5dd8cd9cd..6affbf78d 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/ContentLengthMiddleware.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/ContentLengthMiddleware.kt @@ -1,13 +1,15 @@ package software.amazon.smithy.swift.codegen.integration.middlewares +import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.swift.codegen.ClientRuntimeTypes import software.amazon.smithy.swift.codegen.SwiftWriter +import software.amazon.smithy.swift.codegen.integration.middlewares.handlers.MiddlewareShapeUtils import software.amazon.smithy.swift.codegen.middleware.MiddlewarePosition import software.amazon.smithy.swift.codegen.middleware.MiddlewareRenderable import software.amazon.smithy.swift.codegen.middleware.MiddlewareStep -class ContentLengthMiddleware : MiddlewareRenderable { +class ContentLengthMiddleware(val model: Model) : MiddlewareRenderable { override val name = "ContentLengthMiddleware" @@ -20,6 +22,9 @@ class ContentLengthMiddleware : MiddlewareRenderable { op: OperationShape, operationStackName: String ) { - writer.write("$operationStackName.${middlewareStep.stringValue()}.intercept(position: ${position.stringValue()}, middleware: \$N())", ClientRuntimeTypes.Middleware.ContentLengthMiddleware) + val hasHttpBody = MiddlewareShapeUtils.hasHttpBody(model, op) + if (hasHttpBody) { + writer.write("$operationStackName.${middlewareStep.stringValue()}.intercept(position: ${position.stringValue()}, middleware: \$N())", ClientRuntimeTypes.Middleware.ContentLengthMiddleware) + } } } diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/ContentTypeMiddleware.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/ContentTypeMiddleware.kt index 3c3986a34..19049efb2 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/ContentTypeMiddleware.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/ContentTypeMiddleware.kt @@ -12,7 +12,8 @@ import software.amazon.smithy.swift.codegen.middleware.MiddlewareStep class ContentTypeMiddleware( val model: Model, val symbolProvider: SymbolProvider, - val defaultContentType: String + val defaultContentType: String, + val shouldRender: Boolean = false ) : MiddlewareRenderable { override val name = "ContentTypeMiddleware" @@ -26,9 +27,12 @@ class ContentTypeMiddleware( op: OperationShape, operationStackName: String, ) { - val inputShapeName = MiddlewareShapeUtils.inputSymbol(symbolProvider, model, op).name - val outputShapeName = MiddlewareShapeUtils.outputSymbol(symbolProvider, model, op).name - val outputErrorName = MiddlewareShapeUtils.outputErrorSymbolName(op) - writer.write("$operationStackName.${middlewareStep.stringValue()}.intercept(position: ${position.stringValue()}, middleware: ContentTypeMiddleware<$inputShapeName, $outputShapeName, $outputErrorName>(contentType: \"${defaultContentType}\"))") + val hasHttpBody = MiddlewareShapeUtils.hasHttpBody(model, op) + if (hasHttpBody || shouldRender) { + val inputShapeName = MiddlewareShapeUtils.inputSymbol(symbolProvider, model, op).name + val outputShapeName = MiddlewareShapeUtils.outputSymbol(symbolProvider, model, op).name + val outputErrorName = MiddlewareShapeUtils.outputErrorSymbolName(op) + writer.write("$operationStackName.${middlewareStep.stringValue()}.intercept(position: ${position.stringValue()}, middleware: ContentTypeMiddleware<$inputShapeName, $outputShapeName, $outputErrorName>(contentType: \"${defaultContentType}\"))") + } } } diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/OperationInputBodyMiddleware.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/OperationInputBodyMiddleware.kt index 2e2a771af..dc390b565 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/OperationInputBodyMiddleware.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/OperationInputBodyMiddleware.kt @@ -4,7 +4,6 @@ import software.amazon.smithy.codegen.core.SymbolProvider import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.swift.codegen.SwiftWriter -import software.amazon.smithy.swift.codegen.integration.isInHttpBody import software.amazon.smithy.swift.codegen.integration.middlewares.handlers.MiddlewareShapeUtils import software.amazon.smithy.swift.codegen.middleware.MiddlewarePosition import software.amazon.smithy.swift.codegen.middleware.MiddlewareRenderable @@ -27,9 +26,8 @@ class OperationInputBodyMiddleware( op: OperationShape, operationStackName: String, ) { - val inputShape = model.expectShape(op.input.get()) val inputShapeName = MiddlewareShapeUtils.inputSymbol(symbolProvider, model, op).name - val hasHttpBody = inputShape.members().filter { it.isInHttpBody() }.count() > 0 + val hasHttpBody = MiddlewareShapeUtils.hasHttpBody(model, op) if (hasHttpBody || shouldRender) { writer.write("$operationStackName.${middlewareStep.stringValue()}.intercept(position: ${position.stringValue()}, middleware: ${inputShapeName}BodyMiddleware())") } diff --git a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/handlers/MiddlewareShapeUtils.kt b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/handlers/MiddlewareShapeUtils.kt index 25fc2acff..d3a65ac08 100644 --- a/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/handlers/MiddlewareShapeUtils.kt +++ b/smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/middlewares/handlers/MiddlewareShapeUtils.kt @@ -5,11 +5,16 @@ import software.amazon.smithy.codegen.core.SymbolProvider import software.amazon.smithy.model.Model import software.amazon.smithy.model.knowledge.OperationIndex import software.amazon.smithy.model.shapes.OperationShape +import software.amazon.smithy.model.shapes.Shape import software.amazon.smithy.swift.codegen.SwiftSettings +import software.amazon.smithy.swift.codegen.integration.isInHttpBody import software.amazon.smithy.swift.codegen.model.capitalizedName class MiddlewareShapeUtils { companion object { + fun inputShape(model: Model, op: OperationShape): Shape { + return model.expectShape(op.input.get()) + } fun inputSymbol(symbolProvider: SymbolProvider, model: Model, op: OperationShape): Symbol { val opIndex = OperationIndex.of(model) return inputSymbol(symbolProvider, opIndex, op) @@ -36,5 +41,10 @@ class MiddlewareShapeUtils { fun rootNamespace(settings: SwiftSettings): String { return settings.moduleName } + + fun hasHttpBody(model: Model, op: OperationShape): Boolean { + val inputShape = inputShape(model, op) + return inputShape.members().filter { it.isInHttpBody() }.count() > 0 + } } } diff --git a/smithy-swift-codegen/src/test/kotlin/HttpProtocolUnitTestRequestGeneratorTests.kt b/smithy-swift-codegen/src/test/kotlin/HttpProtocolUnitTestRequestGeneratorTests.kt index 8e888d835..83cff6d01 100644 --- a/smithy-swift-codegen/src/test/kotlin/HttpProtocolUnitTestRequestGeneratorTests.kt +++ b/smithy-swift-codegen/src/test/kotlin/HttpProtocolUnitTestRequestGeneratorTests.kt @@ -263,8 +263,6 @@ class HttpProtocolUnitTestRequestGeneratorTests { } operationStack.serializeStep.intercept(position: .after, middleware: EmptyInputAndEmptyOutputInputHeadersMiddleware()) operationStack.serializeStep.intercept(position: .after, middleware: EmptyInputAndEmptyOutputInputQueryItemMiddleware()) - operationStack.serializeStep.intercept(position: .after, middleware: ContentTypeMiddleware(contentType: "application/json")) - operationStack.finalizeStep.intercept(position: .before, middleware: ClientRuntime.ContentLengthMiddleware()) operationStack.deserializeStep.intercept(position: .after, middleware: MockDeserializeMiddleware( id: "TestDeserializeMiddleware"){ context, actual in @@ -499,8 +497,6 @@ class HttpProtocolUnitTestRequestGeneratorTests { } operationStack.serializeStep.intercept(position: .after, middleware: HttpPrefixHeadersInputHeadersMiddleware()) operationStack.serializeStep.intercept(position: .after, middleware: HttpPrefixHeadersInputQueryItemMiddleware()) - operationStack.serializeStep.intercept(position: .after, middleware: ContentTypeMiddleware(contentType: "application/json")) - operationStack.finalizeStep.intercept(position: .before, middleware: ClientRuntime.ContentLengthMiddleware()) operationStack.deserializeStep.intercept(position: .after, middleware: MockDeserializeMiddleware( id: "TestDeserializeMiddleware"){ context, actual in