From 0e540f415eeb4b2dd20cc7769931b996abb36538 Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Mon, 28 Nov 2022 17:02:00 +0000 Subject: [PATCH 1/7] Extract a ConstrainedNumberGenerator from ConstrainedIntegerGenerator --- .../generators/ConstrainedIntegerGenerator.kt | 26 ++++++++++++++----- ...hapeReachableFromOperationInputTagTrait.kt | 2 ++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGenerator.kt index 10c5e25396..0b50f9e308 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGenerator.kt @@ -7,6 +7,7 @@ package software.amazon.smithy.rust.codegen.server.smithy.generators import software.amazon.smithy.codegen.core.Symbol import software.amazon.smithy.model.shapes.IntegerShape +import software.amazon.smithy.model.shapes.NumberShape import software.amazon.smithy.model.traits.RangeTrait import software.amazon.smithy.rust.codegen.core.rustlang.Attribute import software.amazon.smithy.rust.codegen.core.rustlang.RustMetadata @@ -31,15 +32,28 @@ import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.server.smithy.traits.isReachableFromOperationInput import software.amazon.smithy.rust.codegen.server.smithy.validationErrorMessage +class ConstrainedIntegerGenerator( + val codegenContext: ServerCodegenContext, + val writer: RustWriter, + val shape: IntegerShape, +) { + val inner = ConstrainedNumberGenerator(codegenContext, writer, shape, RustType.Integer(32)) + + fun render() { + inner.render() + } +} + /** - * [ConstrainedIntegerGenerator] generates a wrapper newtype holding a constrained `i32`. + * [ConstrainedNumberGenerator] generates a wrapper newtype holding a constrained number primitive. * This type can be built from unconstrained values, yielding a `ConstraintViolation` when the input does not satisfy * the constraints. */ -class ConstrainedIntegerGenerator( +class ConstrainedNumberGenerator( val codegenContext: ServerCodegenContext, val writer: RustWriter, - val shape: IntegerShape, + val shape: NumberShape, + private val unconstrainedType: RustType, ) { val model = codegenContext.model val constrainedShapeSymbolProvider = codegenContext.constrainedShapeSymbolProvider @@ -58,7 +72,7 @@ class ConstrainedIntegerGenerator( val symbol = constrainedShapeSymbolProvider.toSymbol(shape) val constrainedTypeName = symbol.name - val unconstrainedTypeName = RustType.Integer(32).render() + val unconstrainedTypeName = unconstrainedType.render() val constraintViolation = constraintViolationSymbolProvider.toSymbol(shape) val constraintsInfo = listOf(Range(rangeTrait).toTraitInfo(unconstrainedTypeName)) @@ -171,7 +185,7 @@ private data class Range(val rangeTrait: RangeTrait) { fun toTraitInfo(unconstrainedTypeName: String): TraitInfo = TraitInfo( { rust("Self::check_range(value)?;") }, { - docs("Error when an integer doesn't satisfy its `@range` requirements.") + docs("Error when a number doesn't satisfy its `@range` requirements.") rust("Range($unconstrainedTypeName)") }, { @@ -188,7 +202,7 @@ private data class Range(val rangeTrait: RangeTrait) { ) /** - * Renders a `check_range` function to validate the integer matches the + * Renders a `check_range` function to validate that the value matches the * required range indicated by the `@range` trait. */ private fun renderValidationFunction(constraintViolation: Symbol, unconstrainedTypeName: String): Writable = { diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/traits/ShapeReachableFromOperationInputTagTrait.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/traits/ShapeReachableFromOperationInputTagTrait.kt index b4bd9a255b..d00293aab9 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/traits/ShapeReachableFromOperationInputTagTrait.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/traits/ShapeReachableFromOperationInputTagTrait.kt @@ -10,6 +10,7 @@ import software.amazon.smithy.model.shapes.CollectionShape import software.amazon.smithy.model.shapes.IntegerShape import software.amazon.smithy.model.shapes.ListShape import software.amazon.smithy.model.shapes.MapShape +import software.amazon.smithy.model.shapes.NumberShape import software.amazon.smithy.model.shapes.Shape import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.model.shapes.StringShape @@ -42,3 +43,4 @@ fun CollectionShape.isReachableFromOperationInput() = isShapeReachableFromOperat fun UnionShape.isReachableFromOperationInput() = isShapeReachableFromOperationInput(this) fun MapShape.isReachableFromOperationInput() = isShapeReachableFromOperationInput(this) fun IntegerShape.isReachableFromOperationInput() = isShapeReachableFromOperationInput(this) +fun NumberShape.isReachableFromOperationInput() = isShapeReachableFromOperationInput(this) From 3a4e3763b49f48e75b81f3503d459756b1bd7530 Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Mon, 28 Nov 2022 17:02:42 +0000 Subject: [PATCH 2/7] Update tests' expectations to assume that we support @range for short --- .../generators/protocol/ServerProtocolTestGenerator.kt | 4 ---- .../server/smithy/ConstrainedShapeSymbolProviderTest.kt | 6 ++++++ .../smithy/ValidateUnsupportedConstraintsAreNotUsedTest.kt | 5 ++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocolTestGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocolTestGenerator.kt index 60a26e54c2..c8e40002bd 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocolTestGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocolTestGenerator.kt @@ -1000,13 +1000,9 @@ class ServerProtocolTestGenerator( // Tests involving using @range on bytes, shorts and longs. // See https://github.com/awslabs/smithy-rs/issues/1968 - FailingTest(MalformedRangeValidation, "RestJsonMalformedRangeShort_case0", TestType.MalformedRequest), - FailingTest(MalformedRangeValidation, "RestJsonMalformedRangeShort_case1", TestType.MalformedRequest), FailingTest(MalformedRangeValidation, "RestJsonMalformedRangeLong_case0", TestType.MalformedRequest), FailingTest(MalformedRangeValidation, "RestJsonMalformedRangeLong_case1", TestType.MalformedRequest), - FailingTest(MalformedRangeValidation, "RestJsonMalformedRangeMaxShort", TestType.MalformedRequest), FailingTest(MalformedRangeValidation, "RestJsonMalformedRangeMaxLong", TestType.MalformedRequest), - FailingTest(MalformedRangeValidation, "RestJsonMalformedRangeMinShort", TestType.MalformedRequest), FailingTest(MalformedRangeValidation, "RestJsonMalformedRangeMinLong", TestType.MalformedRequest), // See https://github.com/awslabs/smithy-rs/issues/1969 diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProviderTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProviderTest.kt index fb6c2ec1c7..fc0d732591 100644 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProviderTest.kt +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProviderTest.kt @@ -14,6 +14,7 @@ import software.amazon.smithy.model.shapes.IntegerShape import software.amazon.smithy.model.shapes.MapShape import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.Shape +import software.amazon.smithy.model.shapes.ShortShape import software.amazon.smithy.model.shapes.StringShape import software.amazon.smithy.rust.codegen.core.rustlang.RustType import software.amazon.smithy.rust.codegen.core.smithy.rustType @@ -39,6 +40,7 @@ const val baseModelString = structure TestInputOutput { constrainedString: ConstrainedString, constrainedInteger: ConstrainedInteger, + constrainedShort: ConstrainedShort, constrainedMap: ConstrainedMap, unconstrainedMap: TransitivelyConstrainedMap } @@ -46,6 +48,9 @@ const val baseModelString = @length(min: 1, max: 69) string ConstrainedString + @range(min: -2, max: 10) + integer ConstrainedShort + @range(min: 10, max: 29) integer ConstrainedInteger @@ -79,6 +84,7 @@ class ConstrainedShapeSymbolProviderTest { fun getConstrainedShapes(): Stream = Stream.of( Arguments.of("ConstrainedInteger", { s: Shape -> s is IntegerShape }), + Arguments.of("ConstrainedShort", { s: Shape -> s is ShortShape }), Arguments.of("ConstrainedString", { s: Shape -> s is StringShape }), Arguments.of("ConstrainedMap", { s: Shape -> s is MapShape }), ) diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraintsAreNotUsedTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraintsAreNotUsedTest.kt index 25461f1875..5d0842681d 100644 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraintsAreNotUsedTest.kt +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraintsAreNotUsedTest.kt @@ -199,10 +199,9 @@ internal class ValidateUnsupportedConstraintsAreNotUsedTest { """.asSmithyModel() val validationResult = validateModel(model) - validationResult.messages shouldHaveSize 3 + validationResult.messages shouldHaveSize 2 validationResult.messages[0].message shouldContain "The long shape `test#RangeLong` has the constraint trait `smithy.api#range` attached" - validationResult.messages[1].message shouldContain "The short shape `test#RangeShort` has the constraint trait `smithy.api#range` attached" - validationResult.messages[2].message shouldContain "The byte shape `test#RangeByte` has the constraint trait `smithy.api#range` attached" + validationResult.messages[1].message shouldContain "The byte shape `test#RangeByte` has the constraint trait `smithy.api#range` attached" } @Test From a1fb1aad90770501a5560489b8fd943b344ce14a Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Mon, 28 Nov 2022 17:17:28 +0000 Subject: [PATCH 3/7] Add ShortShape to all the key places to get @range support --- .../server/smithy/ConstrainedShapeSymbolProvider.kt | 5 ++++- .../smithy/ConstraintViolationSymbolProvider.kt | 4 +++- .../rust/codegen/server/smithy/Constraints.kt | 5 +++-- .../codegen/server/smithy/ServerCodegenVisitor.kt | 11 +++++++++++ .../server/smithy/ValidateUnsupportedConstraints.kt | 3 ++- .../BeforeSerializingMemberJsonCustomization.kt | 3 ++- .../generators/ConstrainedIntegerGenerator.kt | 13 +++++++++++++ .../http/ServerResponseBindingGenerator.kt | 4 +++- .../ShapesReachableFromOperationInputTagger.kt | 4 +++- .../smithy/ConstrainedShapeSymbolProviderTest.kt | 2 +- 10 files changed, 45 insertions(+), 9 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProvider.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProvider.kt index 240d583065..64a45b389a 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProvider.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProvider.kt @@ -14,6 +14,7 @@ import software.amazon.smithy.model.shapes.MapShape import software.amazon.smithy.model.shapes.MemberShape import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.Shape +import software.amazon.smithy.model.shapes.ShortShape import software.amazon.smithy.model.shapes.StringShape import software.amazon.smithy.model.traits.LengthTrait import software.amazon.smithy.rust.codegen.core.rustlang.RustType @@ -96,7 +97,8 @@ class ConstrainedShapeSymbolProvider( symbolBuilder(shape, RustType.Vec(inner.rustType())).addReference(inner).build() } } - is StringShape, is IntegerShape -> { + + is StringShape, is IntegerShape, is ShortShape -> { if (shape.isDirectlyConstrained(base)) { val rustType = RustType.Opaque(shape.contextName(serviceShape).toPascalCase()) symbolBuilder(shape, rustType).locatedIn(ModelsModule).build() @@ -104,6 +106,7 @@ class ConstrainedShapeSymbolProvider( base.toSymbol(shape) } } + else -> base.toSymbol(shape) } } diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstraintViolationSymbolProvider.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstraintViolationSymbolProvider.kt index 264edf545b..2a0fc92009 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstraintViolationSymbolProvider.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstraintViolationSymbolProvider.kt @@ -12,6 +12,7 @@ import software.amazon.smithy.model.shapes.IntegerShape import software.amazon.smithy.model.shapes.MapShape import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.Shape +import software.amazon.smithy.model.shapes.ShortShape import software.amazon.smithy.model.shapes.StringShape import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.shapes.UnionShape @@ -113,7 +114,8 @@ class ConstraintViolationSymbolProvider( .locatedIn(module) .build() } - is StringShape, is IntegerShape -> { + + is StringShape, is IntegerShape, is ShortShape -> { val module = shape.shapeModule() val rustType = RustType.Opaque(constraintViolationName, module.fullyQualifiedPath()) Symbol.builder() diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/Constraints.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/Constraints.kt index 592fe74282..aeffa2345c 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/Constraints.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/Constraints.kt @@ -13,6 +13,7 @@ import software.amazon.smithy.model.shapes.IntegerShape import software.amazon.smithy.model.shapes.MapShape import software.amazon.smithy.model.shapes.MemberShape import software.amazon.smithy.model.shapes.Shape +import software.amazon.smithy.model.shapes.ShortShape import software.amazon.smithy.model.shapes.SimpleShape import software.amazon.smithy.model.shapes.StringShape import software.amazon.smithy.model.shapes.StructureShape @@ -72,7 +73,7 @@ fun Shape.isDirectlyConstrained(symbolProvider: SymbolProvider): Boolean = when is MapShape -> this.hasTrait() is StringShape -> this.hasTrait() || supportedStringConstraintTraits.any { this.hasTrait(it) } - is IntegerShape -> this.hasTrait() + is IntegerShape, is ShortShape -> this.hasTrait() else -> false } @@ -98,7 +99,7 @@ fun MemberShape.targetCanReachConstrainedShape(model: Model, symbolProvider: Sym fun Shape.hasPublicConstrainedWrapperTupleType(model: Model, publicConstrainedTypes: Boolean): Boolean = when (this) { is MapShape -> publicConstrainedTypes && this.hasTrait() is StringShape -> !this.hasTrait() && (publicConstrainedTypes && supportedStringConstraintTraits.any(this::hasTrait)) - is IntegerShape -> publicConstrainedTypes && this.hasTrait() + is IntegerShape, is ShortShape -> publicConstrainedTypes && this.hasTrait() is MemberShape -> model.expectShape(this.target).hasPublicConstrainedWrapperTupleType(model, publicConstrainedTypes) else -> false } diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt index d4aa6eccd1..5207cd3aa3 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt @@ -18,6 +18,7 @@ import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.SetShape import software.amazon.smithy.model.shapes.Shape import software.amazon.smithy.model.shapes.ShapeVisitor +import software.amazon.smithy.model.shapes.ShortShape import software.amazon.smithy.model.shapes.StringShape import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.shapes.UnionShape @@ -45,6 +46,7 @@ import software.amazon.smithy.rust.codegen.core.util.hasTrait import software.amazon.smithy.rust.codegen.core.util.runCommand import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstrainedIntegerGenerator import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstrainedMapGenerator +import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstrainedShortGenerator import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstrainedStringGenerator import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstrainedTraitForEnumGenerator import software.amazon.smithy.rust.codegen.server.smithy.generators.MapConstraintViolationGenerator @@ -361,6 +363,15 @@ open class ServerCodegenVisitor( } } + override fun shortShape(shape: ShortShape) { + if (shape.isDirectlyConstrained(codegenContext.symbolProvider)) { + logger.info("[rust-server-codegen] Generating a constrained short $shape") + rustCrate.withModule(ModelsModule) { + ConstrainedShortGenerator(codegenContext, this, shape).render() + } + } + } + protected fun stringShape( shape: StringShape, enumShapeGeneratorFactory: (codegenContext: ServerCodegenContext, writer: RustWriter, shape: StringShape) -> ServerEnumGenerator, diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraints.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraints.kt index 76f5fd1d25..aecde3538d 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraints.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraints.kt @@ -17,6 +17,7 @@ import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.SetShape import software.amazon.smithy.model.shapes.Shape import software.amazon.smithy.model.shapes.ShapeId +import software.amazon.smithy.model.shapes.ShortShape import software.amazon.smithy.model.shapes.UnionShape import software.amazon.smithy.model.traits.EnumTrait import software.amazon.smithy.model.traits.LengthTrait @@ -245,7 +246,7 @@ fun validateUnsupportedConstraints( val unsupportedRangeTraitOnShapeSet = walker .walkShapes(service) .asSequence() - .filterNot { it is IntegerShape } + .filterNot { it is IntegerShape || it is ShortShape } .filterMapShapesToTraits(setOf(RangeTrait::class.java)) .map { (shape, rangeTrait) -> UnsupportedRangeTraitOnShape(shape, rangeTrait as RangeTrait) } .toSet() diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/BeforeSerializingMemberJsonCustomization.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/BeforeSerializingMemberJsonCustomization.kt index 0308a9dd5f..4d3936496b 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/BeforeSerializingMemberJsonCustomization.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/BeforeSerializingMemberJsonCustomization.kt @@ -6,6 +6,7 @@ package software.amazon.smithy.rust.codegen.server.smithy.customizations import software.amazon.smithy.model.shapes.IntegerShape +import software.amazon.smithy.model.shapes.ShortShape import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.writable import software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize.JsonSerializerCustomization @@ -27,7 +28,7 @@ class BeforeSerializingMemberJsonCustomization(private val codegenContext: Serve codegenContext.settings.codegenConfig.publicConstrainedTypes, ) ) { - if (section.shape is IntegerShape) { + if (section.shape is IntegerShape || section.shape is ShortShape) { section.context.valueExpression = ValueExpression.Reference("&${section.context.valueExpression.name}.0") } diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGenerator.kt index 0b50f9e308..2cb14edf7f 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGenerator.kt @@ -8,6 +8,7 @@ package software.amazon.smithy.rust.codegen.server.smithy.generators import software.amazon.smithy.codegen.core.Symbol import software.amazon.smithy.model.shapes.IntegerShape import software.amazon.smithy.model.shapes.NumberShape +import software.amazon.smithy.model.shapes.ShortShape import software.amazon.smithy.model.traits.RangeTrait import software.amazon.smithy.rust.codegen.core.rustlang.Attribute import software.amazon.smithy.rust.codegen.core.rustlang.RustMetadata @@ -44,6 +45,18 @@ class ConstrainedIntegerGenerator( } } +class ConstrainedShortGenerator( + val codegenContext: ServerCodegenContext, + val writer: RustWriter, + val shape: ShortShape, +) { + val inner = ConstrainedNumberGenerator(codegenContext, writer, shape, RustType.Integer(16)) + + fun render() { + inner.render() + } +} + /** * [ConstrainedNumberGenerator] generates a wrapper newtype holding a constrained number primitive. * This type can be built from unconstrained values, yielding a `ConstraintViolation` when the input does not satisfy diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/http/ServerResponseBindingGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/http/ServerResponseBindingGenerator.kt index 20e9f97363..1503096027 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/http/ServerResponseBindingGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/http/ServerResponseBindingGenerator.kt @@ -6,8 +6,10 @@ package software.amazon.smithy.rust.codegen.server.smithy.generators.http import software.amazon.smithy.codegen.core.Symbol +import software.amazon.smithy.model.shapes.IntegerShape import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.model.shapes.Shape +import software.amazon.smithy.model.shapes.ShortShape import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.rust @@ -90,7 +92,7 @@ class ServerResponseBeforeRenderingHeadersHttpBindingCustomization(val codegenCo codegenContext.settings.codegenConfig.publicConstrainedTypes, ) ) { - if (section.context.shape.isIntegerShape) { + if (section.context.shape is IntegerShape || section.context.shape is ShortShape) { section.context.valueExpression = ValueExpression.Reference("&${section.context.valueExpression.name.removePrefix("&")}.0") } diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/transformers/ShapesReachableFromOperationInputTagger.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/transformers/ShapesReachableFromOperationInputTagger.kt index 0036238e6b..3a16481935 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/transformers/ShapesReachableFromOperationInputTagger.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/transformers/ShapesReachableFromOperationInputTagger.kt @@ -10,6 +10,7 @@ import software.amazon.smithy.model.neighbor.Walker import software.amazon.smithy.model.shapes.IntegerShape import software.amazon.smithy.model.shapes.ListShape import software.amazon.smithy.model.shapes.MapShape +import software.amazon.smithy.model.shapes.ShortShape import software.amazon.smithy.model.shapes.StringShape import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.shapes.UnionShape @@ -51,7 +52,7 @@ object ShapesReachableFromOperationInputTagger { return ModelTransformer.create().mapShapes(model) { shape -> when (shape) { - is StructureShape, is UnionShape, is ListShape, is MapShape, is StringShape, is IntegerShape -> { + is StructureShape, is UnionShape, is ListShape, is MapShape, is StringShape, is IntegerShape, is ShortShape -> { if (shapesReachableFromOperationInputs.contains(shape)) { val builder = when (shape) { is StructureShape -> shape.toBuilder() @@ -60,6 +61,7 @@ object ShapesReachableFromOperationInputTagger { is MapShape -> shape.toBuilder() is StringShape -> shape.toBuilder() is IntegerShape -> shape.toBuilder() + is ShortShape -> shape.toBuilder() else -> UNREACHABLE("the `when` is exhaustive") } builder.addTrait(ShapeReachableFromOperationInputTagTrait()).build() diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProviderTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProviderTest.kt index fc0d732591..90501ac178 100644 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProviderTest.kt +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstrainedShapeSymbolProviderTest.kt @@ -49,7 +49,7 @@ const val baseModelString = string ConstrainedString @range(min: -2, max: 10) - integer ConstrainedShort + short ConstrainedShort @range(min: 10, max: 29) integer ConstrainedInteger From b58f9c6a4990da371bd9ee1baa466037011ce7df Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Tue, 29 Nov 2022 11:24:52 +0000 Subject: [PATCH 4/7] Generalize generator tests. --- ...hapeReachableFromOperationInputTagTrait.kt | 7 +- .../ConstrainedIntegerGeneratorTest.kt | 124 --------------- .../ConstrainedNumberGeneratorTest.kt | 146 ++++++++++++++++++ 3 files changed, 151 insertions(+), 126 deletions(-) delete mode 100644 codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGeneratorTest.kt create mode 100644 codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedNumberGeneratorTest.kt diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/traits/ShapeReachableFromOperationInputTagTrait.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/traits/ShapeReachableFromOperationInputTagTrait.kt index d00293aab9..f0cfccb7d7 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/traits/ShapeReachableFromOperationInputTagTrait.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/traits/ShapeReachableFromOperationInputTagTrait.kt @@ -13,6 +13,7 @@ import software.amazon.smithy.model.shapes.MapShape import software.amazon.smithy.model.shapes.NumberShape import software.amazon.smithy.model.shapes.Shape import software.amazon.smithy.model.shapes.ShapeId +import software.amazon.smithy.model.shapes.ShortShape import software.amazon.smithy.model.shapes.StringShape import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.shapes.UnionShape @@ -32,9 +33,11 @@ class ShapeReachableFromOperationInputTagTrait : AnnotationTrait(ID, Node.object } private fun isShapeReachableFromOperationInput(shape: Shape) = when (shape) { - is StructureShape, is UnionShape, is MapShape, is ListShape, is StringShape, is IntegerShape -> { + is StructureShape, is UnionShape, is MapShape, is ListShape, is StringShape, is IntegerShape, is ShortShape -> { shape.hasTrait() - } else -> PANIC("this method does not support shape type ${shape.type}") + } + + else -> PANIC("this method does not support shape type ${shape.type}") } fun StringShape.isReachableFromOperationInput() = isShapeReachableFromOperationInput(this) diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGeneratorTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGeneratorTest.kt deleted file mode 100644 index a889281327..0000000000 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedIntegerGeneratorTest.kt +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -package software.amazon.smithy.rust.codegen.server.smithy.generators - -import io.kotest.matchers.string.shouldContain -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.extension.ExtensionContext -import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.Arguments -import org.junit.jupiter.params.provider.ArgumentsProvider -import org.junit.jupiter.params.provider.ArgumentsSource -import software.amazon.smithy.model.Model -import software.amazon.smithy.model.shapes.IntegerShape -import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter -import software.amazon.smithy.rust.codegen.core.smithy.ModelsModule -import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace -import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel -import software.amazon.smithy.rust.codegen.core.testutil.compileAndTest -import software.amazon.smithy.rust.codegen.core.testutil.unitTest -import software.amazon.smithy.rust.codegen.core.util.lookup -import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverTestCodegenContext -import java.util.stream.Stream - -class ConstrainedIntegerGeneratorTest { - - data class TestCase(val model: Model, val validInteger: Int, val invalidInteger: Int) - - class ConstrainedIntGeneratorTestProvider : ArgumentsProvider { - private val testCases = listOf( - // Min and max. - Triple("@range(min: 10, max: 12)", 11, 13), - // Min equal to max. - Triple("@range(min: 11, max: 11)", 11, 12), - // Only min. - Triple("@range(min: 11)", 12, 2), - // Only max. - Triple("@range(max: 11)", 0, 12), - ).map { - TestCase( - """ - namespace test - - ${it.first} - integer ConstrainedInteger - """.asSmithyModel(), - it.second, - it.third, - ) - } - - override fun provideArguments(context: ExtensionContext?): Stream = - testCases.map { Arguments.of(it) }.stream() - } - - @ParameterizedTest - @ArgumentsSource(ConstrainedIntGeneratorTestProvider::class) - fun `it should generate constrained integer types`(testCase: TestCase) { - val constrainedIntegerShape = testCase.model.lookup("test#ConstrainedInteger") - - val codegenContext = serverTestCodegenContext(testCase.model) - val symbolProvider = codegenContext.symbolProvider - - val project = TestWorkspace.testProject(symbolProvider) - - project.withModule(ModelsModule) { - ConstrainedIntegerGenerator(codegenContext, this, constrainedIntegerShape).render() - - unitTest( - name = "try_from_success", - test = """ - let _constrained: ConstrainedInteger = ${testCase.validInteger}.try_into().unwrap(); - """, - ) - unitTest( - name = "try_from_fail", - test = """ - let constrained_res: Result = ${testCase.invalidInteger}.try_into(); - constrained_res.unwrap_err(); - """, - ) - unitTest( - name = "inner", - test = """ - let constrained = ConstrainedInteger::try_from(${testCase.validInteger}).unwrap(); - assert_eq!(constrained.inner(), &${testCase.validInteger}); - """, - ) - unitTest( - name = "into_inner", - test = """ - let int = ${testCase.validInteger}; - let constrained = ConstrainedInteger::try_from(int).unwrap(); - - assert_eq!(constrained.into_inner(), int); - """, - ) - } - - project.compileAndTest() - } - - @Test - fun `type should not be constructible without using a constructor`() { - val model = """ - namespace test - - @range(min: -1, max: 69) - integer ConstrainedInteger - """.asSmithyModel() - val constrainedIntegerShape = model.lookup("test#ConstrainedInteger") - - val codegenContext = serverTestCodegenContext(model) - - val writer = RustWriter.forModule(ModelsModule.name) - - ConstrainedIntegerGenerator(codegenContext, writer, constrainedIntegerShape).render() - - // Check that the wrapped type is `pub(crate)`. - writer.toString() shouldContain "pub struct ConstrainedInteger(pub(crate) i32);" - } -} diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedNumberGeneratorTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedNumberGeneratorTest.kt new file mode 100644 index 0000000000..4289c9f338 --- /dev/null +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedNumberGeneratorTest.kt @@ -0,0 +1,146 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.rust.codegen.server.smithy.generators + +import io.kotest.matchers.string.shouldContain +import org.junit.jupiter.api.extension.ExtensionContext +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.ArgumentsProvider +import org.junit.jupiter.params.provider.ArgumentsSource +import software.amazon.smithy.model.Model +import software.amazon.smithy.model.shapes.IntegerShape +import software.amazon.smithy.model.shapes.NumberShape +import software.amazon.smithy.model.shapes.ShortShape +import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter +import software.amazon.smithy.rust.codegen.core.smithy.ModelsModule +import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace +import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel +import software.amazon.smithy.rust.codegen.core.testutil.compileAndTest +import software.amazon.smithy.rust.codegen.core.testutil.unitTest +import software.amazon.smithy.rust.codegen.core.util.lookup +import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverTestCodegenContext +import java.util.stream.Stream + +class ConstrainedNumberGeneratorTest { + + data class TestCaseInputs(val constraintAnnotation: String, val validValue: Int, val invalidValue: Int) + data class TestCase(val model: Model, val validValue: Int, val invalidValue: Int, val shapeName: String) + + class ConstrainedNumberGeneratorTestProvider : ArgumentsProvider { + private val testCases = { type: String -> + listOf( + // Min and max. + TestCaseInputs("@range(min: 10, max: 12)", 11, 13), + // Min equal to max. + TestCaseInputs("@range(min: 11, max: 11)", 11, 12), + // Only min. + TestCaseInputs("@range(min: 11)", 12, 2), + // Only max. + TestCaseInputs("@range(max: 11)", 0, 12), + ).map { + val shapeName = "Constrained${type.replaceFirstChar { c -> c.uppercaseChar() }}" + TestCase( + """ + namespace test + + ${it.constraintAnnotation} + $type $shapeName + """.asSmithyModel(), + it.validValue, + it.invalidValue, + shapeName, + ) + } + } + + override fun provideArguments(context: ExtensionContext?): Stream = + listOf("integer", "short").map { type -> testCases(type) }.flatten().map { Arguments.of(it) }.stream() + } + + @ParameterizedTest + @ArgumentsSource(ConstrainedNumberGeneratorTestProvider::class) + fun `it should generate constrained number types`(testCase: TestCase) { + val shape = testCase.model.lookup("test#${testCase.shapeName}") + + val codegenContext = serverTestCodegenContext(testCase.model) + val symbolProvider = codegenContext.symbolProvider + + val project = TestWorkspace.testProject(symbolProvider) + + project.withModule(ModelsModule) { + when (shape) { + is IntegerShape -> ConstrainedIntegerGenerator(codegenContext, this, shape).render() + is ShortShape -> ConstrainedShortGenerator(codegenContext, this, shape).render() + } + + unitTest( + name = "try_from_success", + test = """ + let _constrained: ${testCase.shapeName} = ${testCase.validValue}.try_into().unwrap(); + """, + ) + unitTest( + name = "try_from_fail", + test = """ + let constrained_res: Result<${testCase.shapeName}, _> = ${testCase.invalidValue}.try_into(); + constrained_res.unwrap_err(); + """, + ) + unitTest( + name = "inner", + test = """ + let constrained = ${testCase.shapeName}::try_from(${testCase.validValue}).unwrap(); + assert_eq!(constrained.inner(), &${testCase.validValue}); + """, + ) + unitTest( + name = "into_inner", + test = """ + let v = ${testCase.validValue}; + let constrained = ${testCase.shapeName}::try_from(v).unwrap(); + + assert_eq!(constrained.into_inner(), v); + """, + ) + } + + project.compileAndTest() + } + + class NoStructuralConstructorTestProvider : ArgumentsProvider { + override fun provideArguments(context: ExtensionContext?): Stream = + listOf( + Triple("integer", "ConstrainedInteger", "i32"), + Triple("short", "ConstrainedShort", "i16"), + ).map { Arguments.of(it) }.stream() + } + + @ParameterizedTest + @ArgumentsSource(NoStructuralConstructorTestProvider::class) + fun `type should not be constructible without using a constructor`(args: Triple) { + val (smithyType, shapeName, rustType) = args + val model = """ + namespace test + + @range(min: -1, max: 5) + $smithyType $shapeName + """.asSmithyModel() + val constrainedShape = model.lookup("test#$shapeName") + + val codegenContext = serverTestCodegenContext(model) + + val writer = RustWriter.forModule(ModelsModule.name) + + when (constrainedShape) { + is IntegerShape -> ConstrainedIntegerGenerator(codegenContext, writer, constrainedShape).render() + is ShortShape -> ConstrainedShortGenerator(codegenContext, writer, constrainedShape).render() + } + + // Check that the wrapped type is `pub(crate)`. + writer.toString() shouldContain "pub struct $shapeName(pub(crate) $rustType);" + } +} From 91067476d105c9b6fee71addcfd14f241f61d9de Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Tue, 29 Nov 2022 11:45:27 +0000 Subject: [PATCH 5/7] Add tests for the @range trait on short to our constraints.smithy model. --- .../common-test-models/constraints.smithy | 72 ++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/codegen-core/common-test-models/constraints.smithy b/codegen-core/common-test-models/constraints.smithy index ccef90c147..7c393e9af3 100644 --- a/codegen-core/common-test-models/constraints.smithy +++ b/codegen-core/common-test-models/constraints.smithy @@ -49,7 +49,10 @@ operation ConstrainedShapesOperation { errors: [ValidationException] } -@http(uri: "/constrained-http-bound-shapes-operation/{rangeIntegerLabel}/{lengthStringLabel}/{enumStringLabel}", method: "POST") +@http( + uri: "/constrained-http-bound-shapes-operation/{rangeIntegerLabel}/{rangeShortLabel}/{lengthStringLabel}/{enumStringLabel}", + method: "POST" +) operation ConstrainedHttpBoundShapesOperation { input: ConstrainedHttpBoundShapesOperationInputOutput, output: ConstrainedHttpBoundShapesOperationInputOutput, @@ -179,6 +182,10 @@ structure ConstrainedHttpBoundShapesOperationInputOutput { @httpLabel rangeIntegerLabel: RangeInteger, + @required + @httpLabel + rangeShortLabel: RangeShort, + @required @httpLabel enumStringLabel: EnumString, @@ -193,6 +200,9 @@ structure ConstrainedHttpBoundShapesOperationInputOutput { @httpHeader("X-Range-Integer") rangeIntegerHeader: RangeInteger, + @httpHeader("X-Range-Short") + rangeShortHeader: RangeShort, + // @httpHeader("X-Length-MediaType") // lengthStringHeaderWithMediaType: MediaTypeLengthString, @@ -208,10 +218,15 @@ structure ConstrainedHttpBoundShapesOperationInputOutput { // just a `list` shape with `uniqueItems`, which hasn't been implemented yet. // @httpHeader("X-Range-Integer-Set") // rangeIntegerSetHeader: SetOfRangeInteger, + // @httpHeader("X-Range-Short-Set") + // rangeShortSetHeader: SetOfShortInteger, @httpHeader("X-Range-Integer-List") rangeIntegerListHeader: ListOfRangeInteger, + @httpHeader("X-Range-Short-List") + rangeShortListHeader: ListOfRangeShort, + // TODO(https://github.com/awslabs/smithy-rs/issues/1431) // @httpHeader("X-Enum") //enumStringHeader: EnumString, @@ -225,6 +240,9 @@ structure ConstrainedHttpBoundShapesOperationInputOutput { @httpQuery("rangeInteger") rangeIntegerQuery: RangeInteger, + @httpQuery("rangeShort") + rangeShortQuery: RangeShort, + @httpQuery("enumString") enumStringQuery: EnumString, @@ -239,10 +257,15 @@ structure ConstrainedHttpBoundShapesOperationInputOutput { @httpQuery("rangeIntegerList") rangeIntegerListQuery: ListOfRangeInteger, + @httpQuery("rangeShortList") + rangeShortListQuery: ListOfRangeShort, + // TODO(https://github.com/awslabs/smithy-rs/issues/1401): a `set` shape is // just a `list` shape with `uniqueItems`, which hasn't been implemented yet. // @httpQuery("rangeIntegerSet") // rangeIntegerSetQuery: SetOfRangeInteger, + // @httpQuery("rangeShortSet") + // rangeShortSetQuery: SetOfRangeShort, @httpQuery("enumStringList") enumStringListQuery: ListOfEnumString, @@ -364,6 +387,11 @@ structure ConA { maxRangeInteger: MaxRangeInteger, fixedValueInteger: FixedValueInteger, + rangeShort: RangeShort, + minRangeShort: MinRangeShort, + maxRangeShort: MaxRangeShort, + fixedValueShort: FixedValueShort, + conBList: ConBList, conBList2: ConBList2, @@ -390,6 +418,12 @@ structure ConA { // setOfRangeInteger: SetOfRangeInteger, mapOfRangeInteger: MapOfRangeInteger, + listOfRangeShort: ListOfRangeShort, + // TODO(https://github.com/awslabs/smithy-rs/issues/1401): a `set` shape is + // just a `list` shape with `uniqueItems`, which hasn't been implemented yet. + // setOfRangeShort: SetOfRangeShort, + mapOfRangeShort: MapOfRangeShort, + nonStreamingBlob: NonStreamingBlob patternString: PatternString, @@ -417,6 +451,11 @@ map MapOfRangeInteger { value: RangeInteger, } +map MapOfRangeShort { + key: String, + value: RangeShort, +} + map MapOfEnumString { key: EnumString, value: EnumString, @@ -453,10 +492,17 @@ map MapOfSetOfLengthString { // TODO(https://github.com/awslabs/smithy-rs/issues/1401): a `set` shape is // just a `list` shape with `uniqueItems`, which hasn't been implemented yet. // map MapOfSetOfRangeInteger { -// key: LengthString, +// key: String, // value: SetOfRangeInteger, // } +// TODO(https://github.com/awslabs/smithy-rs/issues/1401): a `set` shape is +// just a `list` shape with `uniqueItems`, which hasn't been implemented yet. +// map MapOfSetOfRangeShort { +// key: String, +// value: SetOfRangeShort, +// } + @length(min: 2, max: 8) list LengthListOfLengthString { member: LengthString @@ -497,6 +543,18 @@ integer MaxRangeInteger @range(min: 69, max: 69) integer FixedValueInteger +@range(min: -0, max: 10) +integer RangeShort + +@range(min: -10) +integer MinRangeShort + +@range(max: 11) +integer MaxRangeShort + +@range(min: 10, max: 10) +integer FixedValueShort + /// A union with constrained members. union ConstrainedUnion { enumString: EnumString, @@ -552,6 +610,16 @@ list ListOfRangeInteger { member: RangeInteger } +// TODO(https://github.com/awslabs/smithy-rs/issues/1401): a `set` shape is +// just a `list` shape with `uniqueItems`, which hasn't been implemented yet. +// set SetOfRangeShort { +// member: RangeShort +// } + +list ListOfRangeShort { + member: RangeShort +} + list ListOfEnumString { member: EnumString } From 40cda7dd7e324725ef96b34ae90d3fb1ebbf89a1 Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Tue, 29 Nov 2022 11:46:40 +0000 Subject: [PATCH 6/7] Update changelog --- CHANGELOG.next.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.next.toml b/CHANGELOG.next.toml index 53cf679909..ce040e782a 100644 --- a/CHANGELOG.next.toml +++ b/CHANGELOG.next.toml @@ -243,13 +243,14 @@ message = """ * The `length` trait on `string` shapes. * The `length` trait on `map` shapes. * The `range` trait on `integer` shapes. +* The `range` trait on `short` shapes. * The `pattern` trait on `string` shapes. Upon receiving a request that violates the modeled constraints, the server SDK will reject it with a message indicating why. Unsupported (constraint trait, target shape) combinations will now fail at code generation time, whereas previously they were just ignored. This is a breaking change to raise awareness in service owners of their server SDKs behaving differently than what was modeled. To continue generating a server SDK with unsupported constraint traits, set `codegenConfig.ignoreUnsupportedConstraints` to `true` in your `smithy-build.json`. """ -references = ["smithy-rs#1199", "smithy-rs#1342", "smithy-rs#1401", "smithy-rs#2005", "smithy-rs#1998"] +references = ["smithy-rs#1199", "smithy-rs#1342", "smithy-rs#1401", "smithy-rs#2005", "smithy-rs#1998", "smithy-rs#2034"] meta = { "breaking" = true, "tada" = true, "bug" = false, "target" = "server" } author = "david-perez" From 3fd6655869effd811e2c242c40a07d9db11a9932 Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Tue, 29 Nov 2022 12:36:31 +0000 Subject: [PATCH 7/7] Fix type in constraints.smithy --- codegen-core/common-test-models/constraints.smithy | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/codegen-core/common-test-models/constraints.smithy b/codegen-core/common-test-models/constraints.smithy index 7c393e9af3..d22ea9fcfc 100644 --- a/codegen-core/common-test-models/constraints.smithy +++ b/codegen-core/common-test-models/constraints.smithy @@ -544,16 +544,16 @@ integer MaxRangeInteger integer FixedValueInteger @range(min: -0, max: 10) -integer RangeShort +short RangeShort @range(min: -10) -integer MinRangeShort +short MinRangeShort @range(max: 11) -integer MaxRangeShort +short MaxRangeShort @range(min: 10, max: 10) -integer FixedValueShort +short FixedValueShort /// A union with constrained members. union ConstrainedUnion {