Skip to content

Commit

Permalink
Keep public constraints for indirecly constrained aggregate shape tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fahad Zubair committed Oct 29, 2024
1 parent a7fb2e3 commit a25cced
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 30 deletions.
30 changes: 0 additions & 30 deletions codegen-core/common-test-models/constraints.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ service ConstraintsService {
ConstrainedHttpBoundShapesOperation,
ConstrainedHttpPayloadBoundShapeOperation,
ConstrainedRecursiveShapesOperation,
ConstrainedListWithIndirectlyConstrainedAggregateOperation,

// `httpQueryParams` and `httpPrefixHeaders` are structurually
// exclusive, so we need one operation per target shape type
Expand Down Expand Up @@ -84,13 +83,6 @@ operation ConstrainedRecursiveShapesOperation {
errors: [ValidationException]
}

@http(uri: "/constrained-list-with-indirectly-constrained-aggregate", method: "POST")
operation ConstrainedListWithIndirectlyConstrainedAggregateOperation {
input: ConstrainedListWithIndirectlyConstrainedAggregateInputOutput,
output: ConstrainedListWithIndirectlyConstrainedAggregateInputOutput,
errors: [ValidationException]
}

@http(uri: "/query-params-targeting-length-map", method: "POST")
operation QueryParamsTargetingLengthMapOperation {
input: QueryParamsTargetingLengthMapOperationInputOutput,
Expand Down Expand Up @@ -343,28 +335,6 @@ structure ConstrainedHttpPayloadBoundShapeOperationInputOutput {
httpPayloadBoundConstrainedShape: ConA
}

structure ConstrainedListWithIndirectlyConstrainedAggregateInputOutput {
a: ListWithIndirectlyConstrainedList,
b: ListWithIndirectlyConstrainedMap
}

@length(min:1, max: 10)
list ListWithIndirectlyConstrainedList {
member: IndirectlyConstrainedList
}
list IndirectlyConstrainedList {
member: LengthString
}

@length(min:1, max: 10)
list ListWithIndirectlyConstrainedMap {
member: IndirectlyConstrainedMap
}
map IndirectlyConstrainedMap {
key: LengthString,
value: LengthString
}

structure QueryParamsTargetingMapOfPatternStringOperationInputOutput {
@httpQueryParams
mapOfPatternString: MapOfPatternString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.AbstractTrait
import software.amazon.smithy.model.transform.ModelTransformer
import software.amazon.smithy.protocol.traits.Rpcv2CborTrait
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams
import software.amazon.smithy.rust.codegen.core.testutil.ServerAdditionalSettings
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.core.util.lookup
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverIntegrationTest
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverTestSymbolProvider
import java.io.File

Expand Down Expand Up @@ -219,4 +222,134 @@ class ConstraintsTest {
structWithInnerDefault.canReachConstrainedShape(model, symbolProvider) shouldBe false
primitiveBoolean.isDirectlyConstrained(symbolProvider) shouldBe false
}

private fun generateAndCompileServer(
model: Model,
pubConstraints: Boolean = true,
dir: File? = null,
) {
if (dir?.exists() == true) {
dir.deleteRecursively()
}

// Simply compiling the crate is sufficient as a test.
serverIntegrationTest(
model,
IntegrationTestParams(
service = "test#SampleService",
additionalSettings =
ServerAdditionalSettings.builder()
.publicConstrainedTypes(pubConstraints)
.toObjectNode(),
overrideTestDir = dir,
),
) { _, _ ->
}
}

private fun createModel(
inputMemberShape: String,
additionalShapes: () -> String,
) = """
namespace test
use aws.protocols#restJson1
use smithy.framework#ValidationException
@restJson1
service SampleService {
operations: [SampleOp]
}
@http(uri: "/sample", method: "POST")
operation SampleOp {
input := {
items : $inputMemberShape
}
errors: [ValidationException]
}
@length(min: 0 max: 65535)
string ItemName
string ItemDescription
${additionalShapes()}
""".asSmithyModel(smithyVersion = "2")

@Test
fun `constrained map with an indirectly constrained nested list should compile`() {
val model =
createModel("ItemMap") {
"""
@length(min: 1 max: 100)
map ItemMap {
key: ItemName,
value: ItemListA
}
list ItemListA {
member: ItemListB
}
list ItemListB {
member: ItemDescription
}
"""
}
generateAndCompileServer(model)
}

@Test
fun `constrained list with an indirectly constrained map should compile`() {
val model =
createModel("ItemList") {
"""
@length(min: 1 max: 100)
list ItemList {
member: Item
}
map Item {
key: ItemName
value: ItemDescription
}
"""
}
generateAndCompileServer(model)
}

@Test
fun `constrained list with an indirectly constrained nested list should compile`() {
val model =
createModel("ItemList") {
"""
@length(min: 1 max: 100)
list ItemList {
member: ItemA
}
list ItemA {
member: ItemB
}
list ItemB {
member: ItemName
}
"""
}
generateAndCompileServer(model)
}

@Test
fun `constrained list with an indirectly constrained list that has an indirectly constrained map should compile`() {
val model =
createModel("ItemList") {
"""
@length(min: 1 max: 100)
list ItemList {
member: NestedItemList
}
list NestedItemList {
member: Item
}
map Item {
key: ItemName
value: ItemDescription
}
"""
}
generateAndCompileServer(model)
}
}

0 comments on commit a25cced

Please sign in to comment.