Unverified Commit ffafbec8 authored by Manuel Sugawara's avatar Manuel Sugawara Committed by GitHub
Browse files

Correctly determine nullability of members in IDLv2 models (#1725)

parent 0313a3c7
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -236,3 +236,9 @@ renamed to `with_retry_classifier` and `retry_classifier` respectively. Public m
references = ["smithy-rs#1715", "smithy-rs#1717"]
meta = { "breaking" = true, "tada" = false, "bug" = false }
author = "jdisanti"

[[smithy-rs]]
message = "Correctly determine nullability of members in IDLv2 models"
references = ["smithy-rs#1725"]
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "all"}
author = "sugmanue"
+23 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package software.amazon.smithy.rustsdk.customize.ec2

import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.traits.ClientOptionalTrait
import software.amazon.smithy.model.transform.ModelTransformer

object EC2MakePrimitivesOptional {
    fun processModel(model: Model): Model {
        val updates = arrayListOf<Shape>()
        for (struct in model.structureShapes) {
            for (member in struct.allMembers.values) {
                updates.add(member.toBuilder().addTrait(ClientOptionalTrait()).build())
            }
        }
        return ModelTransformer.create().replaceShapes(model, updates)
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ class Ec2Decorator : RustCodegenDecorator<ClientCodegenContext> {
        // need to be boxed for the API to work properly
        return model.letIf(
            applies(service),
            BoxPrimitiveShapes::processModel,
            EC2MakePrimitivesOptional::processModel,
        )
    }

+5 −10
Original line number Diff line number Diff line
@@ -7,13 +7,12 @@ package software.amazon.smithy.rustsdk.customize.ec2

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import software.amazon.smithy.model.knowledge.NullableIndex
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.BoxTrait
import software.amazon.smithy.rust.codegen.client.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.core.util.hasTrait
import software.amazon.smithy.rust.codegen.core.util.lookup

internal class BoxPrimitiveShapesTest {
internal class EC2MakePrimitivesOptionalTest {
    @Test
    fun `primitive shapes are boxed`() {
        val baseModel = """
@@ -33,15 +32,11 @@ internal class BoxPrimitiveShapesTest {
            structure Other {}

        """.asSmithyModel()
        val model = BoxPrimitiveShapes.processModel(baseModel)

        val model = EC2MakePrimitivesOptional.processModel(baseModel)
        val nullableIndex = NullableIndex(model)
        val struct = model.lookup<StructureShape>("test#Primitives")
        struct.members().forEach {
            val target = model.expectShape(it.target)
            when (target) {
                is StructureShape -> target.hasTrait<BoxTrait>() shouldBe false
                else -> target.hasTrait<BoxTrait>() shouldBe true
            }
            nullableIndex.isMemberNullable(it, NullableIndex.CheckMode.CLIENT_ZERO_VALUE_V1) shouldBe true
        }
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ package software.amazon.smithy.rust.codegen.client.smithy

import software.amazon.smithy.build.PluginContext
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.knowledge.NullableIndex
import software.amazon.smithy.model.neighbor.Walker
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.shapes.Shape
@@ -61,8 +62,8 @@ class CodegenVisitor(context: PluginContext, private val codegenDecorator: RustC
            SymbolVisitorConfig(
                runtimeConfig = settings.runtimeConfig,
                renameExceptions = settings.codegenConfig.renameExceptions,
                handleRequired = false,
                handleRustBoxing = true,
                nullabilityCheckMode = NullableIndex.CheckMode.CLIENT_ZERO_VALUE_V1,
            )
        val baseModel = baselineTransform(context.model)
        val service = settings.getService(baseModel)
Loading