Skip to content

Commit c836ace

Browse files
authored
fix: codegen visitor filtering bug (#1049)
1 parent efd8bde commit c836ace

File tree

3 files changed

+20
-154
lines changed

3 files changed

+20
-154
lines changed

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/CodegenVisitor.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class CodegenVisitor(context: PluginContext) : ShapeVisitor.Default<Unit>() {
4040
private val fileManifest: FileManifest = context.fileManifest
4141
private val symbolProvider: SymbolProvider
4242
private val writers: KotlinDelegator
43-
private val integrations: List<KotlinIntegration>
43+
private val integrations: MutableList<KotlinIntegration>
4444
private val protocolGenerator: ProtocolGenerator?
4545
private val applicationProtocol: ApplicationProtocol
4646
private val baseGenerationContext: GenerationContext
@@ -50,20 +50,28 @@ class CodegenVisitor(context: PluginContext) : ShapeVisitor.Default<Unit>() {
5050
LOGGER.info("Discovering KotlinIntegration providers...")
5151
integrations = ServiceLoader.load(KotlinIntegration::class.java, classLoader)
5252
.onEach { integration -> LOGGER.info("Loaded KotlinIntegration: ${integration.javaClass.name}") }
53-
.filter { integration -> integration.enabledForService(context.model, settings) } // TODO: Change so we don't filter until previous integrations model modifications are complete
54-
.onEach { integration -> LOGGER.info("Enabled KotlinIntegration: ${integration.javaClass.name}") }
5553
.sortedBy(KotlinIntegration::order)
54+
.toMutableList()
55+
56+
var resolvedModel = context.model
57+
val disabledIntegrations = mutableListOf<KotlinIntegration>()
5658

5759
LOGGER.info("Preprocessing model")
60+
5861
// Model pre-processing:
5962
// 1. Start with the model from the plugin context
6063
// 2. Apply integrations
6164
// 3. Flatten error shapes (see: https://github.com/awslabs/smithy/pull/919)
6265
// 4. Normalize the operations
63-
var resolvedModel = context.model
6466
for (integration in integrations) {
65-
resolvedModel = integration.preprocessModel(resolvedModel, settings)
67+
if (integration.enabledForService(resolvedModel, settings)) {
68+
LOGGER.info("Enabled KotlinIntegration: ${integration.javaClass.name}")
69+
resolvedModel = integration.preprocessModel(resolvedModel, settings)
70+
} else {
71+
disabledIntegrations.add(integration)
72+
}
6673
}
74+
integrations.removeAll(disabledIntegrations)
6775

6876
resolvedModel = ModelTransformer.create()
6977
.copyServiceErrorsToOperations(resolvedModel, settings.getService(resolvedModel))

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/SigV4AsymmetricAuthSchemeIntegration.kt

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ import software.amazon.smithy.model.shapes.ShapeId
2525
* Register support for the `aws.auth#sigv4a` auth scheme.
2626
*/
2727
class SigV4AsymmetricAuthSchemeIntegration : KotlinIntegration {
28-
// Needs to happen after the `SigV4AsymmetricTraitCustomization` (-60).
29-
override val order: Byte = -50
30-
31-
// Needs to be true due to the way integrations are filtered out before application and sigV4a customization.
32-
// See 'CodegenVisitor' & 'SigV4AsymmetricTraitCustomization'
33-
override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = true
28+
override fun enabledForService(model: Model, settings: KotlinSettings): Boolean =
29+
ServiceIndex
30+
.of(model)
31+
.getAuthSchemes(settings.service)
32+
.values
33+
.any { it.javaClass == SigV4ATrait::class.java }
3434

3535
override fun authSchemes(ctx: ProtocolGenerator.GenerationContext): List<AuthSchemeHandler> =
36-
if (modelHasSigV4aTrait(ctx)) listOf(SigV4AsymmetricAuthSchemeHandler()) else emptyList()
36+
listOf(SigV4AsymmetricAuthSchemeHandler())
3737
}
3838

3939
private class SigV4AsymmetricAuthSchemeHandler : AuthSchemeHandler {
@@ -69,10 +69,3 @@ private class SigV4AsymmetricAuthSchemeHandler : AuthSchemeHandler {
6969
writer.write("#T(#T, #S)", RuntimeTypes.Auth.HttpAuthAws.SigV4AsymmetricAuthScheme, RuntimeTypes.Auth.Signing.AwsSigningStandard.DefaultAwsSigner, signingService)
7070
}
7171
}
72-
73-
internal fun modelHasSigV4aTrait(ctx: ProtocolGenerator.GenerationContext): Boolean =
74-
ServiceIndex
75-
.of(ctx.model)
76-
.getAuthSchemes(ctx.service)
77-
.values
78-
.any { it.javaClass == SigV4ATrait::class.java }

codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/SigV4AsymmetricAuthSchemeIntegrationTest.kt

Lines changed: 0 additions & 135 deletions
This file was deleted.

0 commit comments

Comments
 (0)