Unverified Commit fd8b2d4e authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Workaround bug in smithy-rules-engine (#2179)

* Workaround bug in smithy-rules-engine

This PR works around a bug in smithy-rules-engine `Parameter.toBuilder()` — the method was discarding the default value (and other fields). This was fixed here: https://github.com/awslabs/smithy/pull/1571/files. Until that's merged, this works around it by copying the value manually. This also adds an SDK adhoc test that includes an endpoint test.

* change to TODO comment
parent 4af30b68
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -54,6 +54,22 @@ val allCodegenTests = listOf(
            }
        """,
    ),
    CodegenTest(
        "com.amazonaws.testservice#TestService",
        "endpoint-test-service",
        imports = listOf("models/single-static-endpoint.smithy"),
        extraConfig = """
            ,
            "codegen": {
                "includeFluentClient": false
            },
            "customizationConfig": {
                "awsSdk": {
                    "generateReadme": false
                }
            }
        """,
    ),
)

project.registerGenerateSmithyBuildTask(rootProject, pluginName, allCodegenTests)
+68 −0
Original line number Diff line number Diff line
$version: "2"
namespace com.amazonaws.testservice

use aws.api#service
use aws.protocols#restJson1
use smithy.rules#endpointRuleSet
use smithy.rules#endpointTests

@endpointRuleSet({
    "version": "1.0",
    "parameters": {
        "Region": {
            "required": true,
            "type": "String",
            "builtIn": "AWS::Region",
            "default": "us-east-2",
        },
    },
    "rules": [
        {
            "type": "endpoint",
            "conditions": [],
            "endpoint": {
                "url": "https://prod.{Region}.api.myservice.aws.dev",
                "properties": {
                    "authSchemes": [{
                                        "name": "sigv4",
                                        "signingRegion": "{Region}",
                                    }]
                }
            }
        }
    ]
})
@endpointTests({"version": "1", "testCases": [
    {
        "documentation": "region should fallback to the default",
        "expect": {
            "endpoint": {
                "url": "https://prod.us-east-2.api.myservice.aws.dev",
                "properties": {
                    "authSchemes": [{
                                        "name": "sigv4",
                                        "signingRegion": "us-east-2",
                                    }]

                }
            }
        },
        "params": { }
    }]
})
@restJson1
@title("Test Service")
@service(sdkId: "Test")
service TestService {
    operations: [TestOperation]
}

@input
structure Foo { bar: Bar }

structure Bar {}

@http(uri: "/foo", method: "POST")
operation TestOperation {
    input: Foo
}
+6 −2
Original line number Diff line number Diff line
@@ -60,8 +60,12 @@ class AwsEndpointDecorator : ClientCodegenDecorator {
                    val newParameters = Parameters.builder()
                    epRules.parameters.toList()
                        .map { param ->
                            param.letIf(param.builtIn == Builtins.REGION.builtIn) {
                                it.toBuilder().required(true).build()
                            param.letIf(param.builtIn == Builtins.REGION.builtIn) { parameter ->
                                val builder = parameter.toBuilder().required(true)
                                // TODO(https://github.com/awslabs/smithy-rs/issues/2187): undo this workaround
                                parameter.defaultValue.ifPresent { default -> builder.defaultValue(default) }

                                builder.build()
                            }
                        }
                        .forEach(newParameters::addParameter)
+1 −0
Original line number Diff line number Diff line
@@ -129,6 +129,7 @@ fun Project.registerGenerateSmithyBuildTask(
    this.tasks.register("generateSmithyBuild") {
        description = "generate smithy-build.json"
        outputs.file(project.projectDir.resolve("smithy-build.json"))
        allCodegenTests.flatMap { it.imports }.forEach { inputs.file(project.projectDir.resolve(it)) }

        doFirst {
            project.projectDir.resolve("smithy-build.json")