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

Upgrade to Smithy 1.28.1 (#2463)



* Upgrade to Smithy 1.28.1

* fix a couple of failing protocol tests

* Adhere to new ValidationException message format

* Add RestJsonMalformed to FailingTest

* Add various fixes

---------

Co-authored-by: default avatarHarry Barber <hlbarber@amazon.co.uk>
Co-authored-by: default avatarHarry Barber <106155934+hlbarber@users.noreply.github.com>
parent a02426fc
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@ import software.amazon.smithy.rust.codegen.core.util.expectMember
import software.amazon.smithy.rust.codegen.core.util.hasTrait
import software.amazon.smithy.rust.codegen.core.util.isTargetUnit
import software.amazon.smithy.rust.codegen.core.util.letIf
import java.math.BigDecimal

/**
 * Class describing an instantiator section that can be used in a customization.
@@ -125,10 +126,16 @@ open class Instantiator(
            is MemberShape -> renderMember(writer, shape, data, ctx)

            // Wrapped Shapes
            is TimestampShape -> writer.rust(
                "#T::from_secs(${(data as NumberNode).value})",
            is TimestampShape -> {
                val node = (data as NumberNode)
                val num = BigDecimal(node.toString())
                val wholePart = num.toInt()
                val fractionalPart = num.remainder(BigDecimal.ONE)
                writer.rust(
                    "#T::from_fractional_secs($wholePart, ${fractionalPart}_f64)",
                    RuntimeType.dateTime(runtimeConfig),
                )
            }

            /**
             * ```rust
+45 −0
Original line number Diff line number Diff line
@@ -8,16 +8,20 @@ package software.amazon.smithy.rust.codegen.core.smithy.generators
import org.junit.jupiter.api.Test
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.node.Node
import software.amazon.smithy.model.node.NumberNode
import software.amazon.smithy.model.node.StringNode
import software.amazon.smithy.model.shapes.BlobShape
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.shapes.TimestampShape
import software.amazon.smithy.model.shapes.UnionShape
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.withBlock
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.transformers.RecursiveShapeBoxer
import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
@@ -292,4 +296,45 @@ class InstantiatorTest {
        }
        project.compileAndTest()
    }

    @Test
    fun `integer and fractional timestamps`() {
        // "Parameter values that contain binary data MUST be defined using values
        // that can be represented in plain text (for example, use "foo" and not "Zm9vCg==")."
        val sut = Instantiator(
            symbolProvider,
            model,
            runtimeConfig,
            BuilderKindBehavior(codegenContext),
            ::enumFromStringFn,
        )
        val project = TestWorkspace.testProject(model)
        project.testModule {
            unitTest("timestamps") {
                withBlock("let ts_frac = ", ";") {
                    sut.render(
                        this,
                        TimestampShape.builder().id(ShapeId.from("com.example#Timestamp")).build(),
                        NumberNode.from(946845296.123),
                    )
                }
                withBlock("let ts_int = ", ";") {
                    sut.render(
                        this,
                        TimestampShape.builder().id(ShapeId.from("com.example#Timestamp")).build(),
                        NumberNode.from(123),
                    )
                }
                rustTemplate(
                    "assert_eq!(ts_frac, #{Timestamp}::from_millis(946845296*1000 + 123));",
                    "Timestamp" to RuntimeType.dateTime(runtimeConfig),
                )
                rustTemplate(
                    "assert_eq!(ts_int, #{Timestamp}::from_secs(123));",
                    "Timestamp" to RuntimeType.dateTime(runtimeConfig),
                )
            }
        }
        project.compileAndTest()
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ package software.amazon.smithy.rust.codegen.server.smithy
import software.amazon.smithy.model.traits.RangeTrait

fun RangeTrait.validationErrorMessage(): String {
    val beginning = "Value {} at '{}' failed to satisfy constraint: Member must be "
    val beginning = "Value at '{}' failed to satisfy constraint: Member must be "
    val ending = if (this.min.isPresent && this.max.isPresent) {
        "between ${this.min.get()} and ${this.max.get()}, inclusive"
    } else if (this.min.isPresent) {
+6 −6
Original line number Diff line number Diff line
@@ -101,7 +101,7 @@ class ValidationExceptionWithReasonConversionGenerator(private val codegenContex
                        is Pattern -> {
                            rustTemplate(
                                """
                                Self::Pattern(string) => crate::model::ValidationExceptionField {
                                Self::Pattern(_) => crate::model::ValidationExceptionField {
                                    message: #{MessageWritable:W},
                                    name: path,
                                    reason: crate::model::ValidationExceptionFieldReason::PatternNotValid,
@@ -140,12 +140,12 @@ class ValidationExceptionWithReasonConversionGenerator(private val codegenContex

    override fun enumShapeConstraintViolationImplBlock(enumTrait: EnumTrait) = writable {
        val enumValueSet = enumTrait.enumDefinitionValues.joinToString(", ")
        val message = "Value {} at '{}' failed to satisfy constraint: Member must satisfy enum value set: [$enumValueSet]"
        val message = "Value at '{}' failed to satisfy constraint: Member must satisfy enum value set: [$enumValueSet]"
        rustTemplate(
            """
            pub(crate) fn as_validation_exception_field(self, path: #{String}) -> crate::model::ValidationExceptionField {
                crate::model::ValidationExceptionField {
                    message: format!(r##"$message"##, &self.0, &path),
                    message: format!(r##"$message"##, &path),
                    name: path,
                    reason: crate::model::ValidationExceptionFieldReason::ValueNotValid,
                }
@@ -160,8 +160,8 @@ class ValidationExceptionWithReasonConversionGenerator(private val codegenContex
            """
            pub(crate) fn as_validation_exception_field(self, path: #{String}) -> crate::model::ValidationExceptionField {
                match self {
                    Self::Range(value) => crate::model::ValidationExceptionField {
                        message: format!("${rangeInfo.rangeTrait.validationErrorMessage()}", value, &path),
                    Self::Range(_) => crate::model::ValidationExceptionField {
                        message: format!("${rangeInfo.rangeTrait.validationErrorMessage()}", &path),
                        name: path,
                        reason: crate::model::ValidationExceptionFieldReason::ValueNotValid,
                    }
@@ -243,7 +243,7 @@ class ValidationExceptionWithReasonConversionGenerator(private val codegenContex
                    rust(
                        """
                        ConstraintViolation::${it.name()} => crate::model::ValidationExceptionField {
                            message: format!("Value null at '{}/${it.forMember.memberName}' failed to satisfy constraint: Member must not be null", path),
                            message: format!("Value at '{}/${it.forMember.memberName}' failed to satisfy constraint: Member must not be null", path),
                            name: path + "/${it.forMember.memberName}",
                            reason: crate::model::ValidationExceptionFieldReason::Other,
                        },
+3 −3
Original line number Diff line number Diff line
@@ -160,12 +160,12 @@ class SmithyValidationExceptionConversionGenerator(private val codegenContext: S

    override fun enumShapeConstraintViolationImplBlock(enumTrait: EnumTrait) = writable {
        val enumValueSet = enumTrait.enumDefinitionValues.joinToString(", ")
        val message = "Value {} at '{}' failed to satisfy constraint: Member must satisfy enum value set: [$enumValueSet]"
        val message = "Value at '{}' failed to satisfy constraint: Member must satisfy enum value set: [$enumValueSet]"
        rustTemplate(
            """
            pub(crate) fn as_validation_exception_field(self, path: #{String}) -> crate::model::ValidationExceptionField {
                crate::model::ValidationExceptionField {
                    message: format!(r##"$message"##, &self.0, &path),
                    message: format!(r##"$message"##, &path),
                    path,
                }
            }
@@ -197,7 +197,7 @@ class SmithyValidationExceptionConversionGenerator(private val codegenContext: S
                    rust(
                        """
                        ConstraintViolation::${it.name()} => crate::model::ValidationExceptionField {
                            message: format!("Value null at '{}/${it.forMember.memberName}' failed to satisfy constraint: Member must not be null", path),
                            message: format!("Value at '{}/${it.forMember.memberName}' failed to satisfy constraint: Member must not be null", path),
                            path: path + "/${it.forMember.memberName}",
                        },
                        """,
Loading