diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/CodegenVisitor.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/CodegenVisitor.kt index ee1d786d7..213920c42 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/CodegenVisitor.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/CodegenVisitor.kt @@ -40,7 +40,7 @@ class CodegenVisitor(context: PluginContext) : ShapeVisitor.Default() { private val fileManifest: FileManifest = context.fileManifest private val symbolProvider: SymbolProvider private val writers: KotlinDelegator - private val integrations: List + private val integrations: MutableList private val protocolGenerator: ProtocolGenerator? private val applicationProtocol: ApplicationProtocol private val baseGenerationContext: GenerationContext @@ -50,20 +50,28 @@ class CodegenVisitor(context: PluginContext) : ShapeVisitor.Default() { LOGGER.info("Discovering KotlinIntegration providers...") integrations = ServiceLoader.load(KotlinIntegration::class.java, classLoader) .onEach { integration -> LOGGER.info("Loaded KotlinIntegration: ${integration.javaClass.name}") } - .filter { integration -> integration.enabledForService(context.model, settings) } // TODO: Change so we don't filter until previous integrations model modifications are complete - .onEach { integration -> LOGGER.info("Enabled KotlinIntegration: ${integration.javaClass.name}") } .sortedBy(KotlinIntegration::order) + .toMutableList() + + var resolvedModel = context.model + val disabledIntegrations = mutableListOf() LOGGER.info("Preprocessing model") + // Model pre-processing: // 1. Start with the model from the plugin context // 2. Apply integrations // 3. Flatten error shapes (see: https://github.com/awslabs/smithy/pull/919) // 4. Normalize the operations - var resolvedModel = context.model for (integration in integrations) { - resolvedModel = integration.preprocessModel(resolvedModel, settings) + if (integration.enabledForService(resolvedModel, settings)) { + LOGGER.info("Enabled KotlinIntegration: ${integration.javaClass.name}") + resolvedModel = integration.preprocessModel(resolvedModel, settings) + } else { + disabledIntegrations.add(integration) + } } + integrations.removeAll(disabledIntegrations) resolvedModel = ModelTransformer.create() .copyServiceErrorsToOperations(resolvedModel, settings.getService(resolvedModel)) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/SigV4AsymmetricAuthSchemeIntegration.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/SigV4AsymmetricAuthSchemeIntegration.kt index 9ead1b73e..df52f303f 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/SigV4AsymmetricAuthSchemeIntegration.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/SigV4AsymmetricAuthSchemeIntegration.kt @@ -25,15 +25,15 @@ import software.amazon.smithy.model.shapes.ShapeId * Register support for the `aws.auth#sigv4a` auth scheme. */ class SigV4AsymmetricAuthSchemeIntegration : KotlinIntegration { - // Needs to happen after the `SigV4AsymmetricTraitCustomization` (-60). - override val order: Byte = -50 - - // Needs to be true due to the way integrations are filtered out before application and sigV4a customization. - // See 'CodegenVisitor' & 'SigV4AsymmetricTraitCustomization' - override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = true + override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = + ServiceIndex + .of(model) + .getAuthSchemes(settings.service) + .values + .any { it.javaClass == SigV4ATrait::class.java } override fun authSchemes(ctx: ProtocolGenerator.GenerationContext): List = - if (modelHasSigV4aTrait(ctx)) listOf(SigV4AsymmetricAuthSchemeHandler()) else emptyList() + listOf(SigV4AsymmetricAuthSchemeHandler()) } private class SigV4AsymmetricAuthSchemeHandler : AuthSchemeHandler { @@ -69,10 +69,3 @@ private class SigV4AsymmetricAuthSchemeHandler : AuthSchemeHandler { writer.write("#T(#T, #S)", RuntimeTypes.Auth.HttpAuthAws.SigV4AsymmetricAuthScheme, RuntimeTypes.Auth.Signing.AwsSigningStandard.DefaultAwsSigner, signingService) } } - -internal fun modelHasSigV4aTrait(ctx: ProtocolGenerator.GenerationContext): Boolean = - ServiceIndex - .of(ctx.model) - .getAuthSchemes(ctx.service) - .values - .any { it.javaClass == SigV4ATrait::class.java } diff --git a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/SigV4AsymmetricAuthSchemeIntegrationTest.kt b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/SigV4AsymmetricAuthSchemeIntegrationTest.kt deleted file mode 100644 index 0990afbc6..000000000 --- a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/SigV4AsymmetricAuthSchemeIntegrationTest.kt +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0 - */ -package software.amazon.smithy.kotlin.codegen.rendering.auth - -import software.amazon.smithy.aws.traits.protocols.AwsJson1_0Trait -import software.amazon.smithy.build.MockManifest -import software.amazon.smithy.kotlin.codegen.KotlinSettings -import software.amazon.smithy.kotlin.codegen.core.KotlinDelegator -import software.amazon.smithy.kotlin.codegen.core.KotlinSymbolProvider -import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator -import software.amazon.smithy.kotlin.codegen.test.toSmithyModel -import software.amazon.smithy.model.shapes.ShapeId -import kotlin.test.Test -import kotlin.test.assertFalse -import kotlin.test.assertTrue - -class SigV4AsymmetricAuthSchemeIntegrationTest { - private val testModelWithoutSigV4aTrait = """ - namespace smithy.example - - use aws.protocols#awsJson1_0 - use aws.auth#sigv4 - - @awsJson1_0 - @sigv4(name: "example-signing-name") - service Example { - version: "1.0.0", - operations: [GetFoo] - } - - operation GetFoo { - input: GetFooInput - } - - operation GetNotFoo { - input: GetFooInput - } - - structure GetFooInput { - payload: String - } - """.toSmithyModel() - - private val testModelWithSigV4aTrait = """ - namespace smithy.example - - use aws.protocols#awsJson1_0 - use aws.auth#sigv4 - use aws.auth#sigv4a - - @awsJson1_0 - @sigv4(name: "example-signing-name") - @sigv4a(name: "example-signing-name") - service Example { - version: "1.0.0", - operations: [GetFoo] - } - - operation GetFoo { - input: GetFooInput - } - - operation GetNotFoo { - input: GetFooInput - } - - structure GetFooInput { - payload: String - } - """.toSmithyModel() - - @Test - fun testModelWithoutSigV4aTraitGetsFlaggedCorrectly() { - val settings = KotlinSettings( - testModelWithoutSigV4aTrait.serviceShapes.first().id, - KotlinSettings.PackageSettings("example", "1.0.0"), - testModelWithoutSigV4aTrait.serviceShapes.first().id.toString(), - ) - - val symbolProvider = KotlinSymbolProvider( - testModelWithoutSigV4aTrait, - settings, - ) - - val generationContext: ProtocolGenerator.GenerationContext = ProtocolGenerator.GenerationContext( - settings, - testModelWithoutSigV4aTrait, - testModelWithoutSigV4aTrait.serviceShapes.first(), - symbolProvider, - emptyList(), - ShapeId.from(AwsJson1_0Trait.ID.toString()), - KotlinDelegator( - settings, - testModelWithoutSigV4aTrait, - MockManifest(), - symbolProvider, - ), - ) - - assertFalse(modelHasSigV4aTrait(generationContext)) - } - - @Test - fun testModelWithSigV4aTraitGetsFlaggedCorrectly() { - val settings = KotlinSettings( - testModelWithSigV4aTrait.serviceShapes.first().id, - KotlinSettings.PackageSettings("example", "1.0.0"), - testModelWithSigV4aTrait.serviceShapes.first().id.toString(), - ) - - val symbolProvider = KotlinSymbolProvider( - testModelWithSigV4aTrait, - settings, - ) - - val generationContext: ProtocolGenerator.GenerationContext = ProtocolGenerator.GenerationContext( - settings, - testModelWithSigV4aTrait, - testModelWithSigV4aTrait.serviceShapes.first(), - symbolProvider, - emptyList(), - ShapeId.from(AwsJson1_0Trait.ID.toString()), - KotlinDelegator( - settings, - testModelWithSigV4aTrait, - MockManifest(), - symbolProvider, - ), - ) - - assertTrue(modelHasSigV4aTrait(generationContext)) - } -}