Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 3 bugs in @length-constrained collection and map shapes #2085

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions codegen-core/common-test-models/constraints.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,14 @@ structure ConA {
fixedValueByte: FixedValueByte,

conBList: ConBList,
conBList2: ConBList2,
lengthList: LengthList,

// 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.
// conBSet: ConBSet,

conBMap: ConBMap,
lengthMap: LengthMap,

mapOfMapOfListOfListOfConB: MapOfMapOfListOfListOfConB,

Expand Down Expand Up @@ -837,14 +838,11 @@ list RecursiveList {
}

list ConBList {
member: NestedList
member: LengthList
}

list ConBList2 {
member: ConB
}

list NestedList {
@length(max: 69)
list LengthList {
member: ConB
}

Expand Down Expand Up @@ -874,6 +872,12 @@ map ConBMap {
value: LengthString
}

@length(min: 1, max: 69)
map LengthMap {
key: String,
value: String
}

@error("client")
structure ErrorWithLengthStringMessage {
// TODO Doesn't work yet because constrained string types don't implement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package software.amazon.smithy.rust.codegen.server.smithy.generators

import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.shapes.CollectionShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.shapes.UnionShape
import software.amazon.smithy.model.traits.LengthTrait
import software.amazon.smithy.model.traits.Trait
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
Expand All @@ -23,6 +25,7 @@ import software.amazon.smithy.rust.codegen.core.util.PANIC
import software.amazon.smithy.rust.codegen.core.util.orNull
import software.amazon.smithy.rust.codegen.server.smithy.PubCrateConstraintViolationSymbolProvider
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.canReachConstrainedShape
import software.amazon.smithy.rust.codegen.server.smithy.supportedCollectionConstraintTraits
import software.amazon.smithy.rust.codegen.server.smithy.validationErrorMessage

Expand Down Expand Up @@ -132,7 +135,12 @@ class ConstrainedCollectionGenerator(
"ValidationFunctions" to constraintsInfo.map { it.validationFunctionDefinition(constraintViolation, inner) }.join("\n"),
)

if (!publicConstrainedTypes && isValueConstrained(shape, model, symbolProvider)) {
val innerShape = model.expectShape(shape.member.target)
if (!publicConstrainedTypes
&& innerShape.canReachConstrainedShape(model, symbolProvider)
&& innerShape !is StructureShape
&& innerShape !is UnionShape
) {
writer.rustTemplate(
"""
impl #{From}<$name> for #{FullyUnconstrainedSymbol} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package software.amazon.smithy.rust.codegen.server.smithy.generators

import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.shapes.MapShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.shapes.UnionShape
import software.amazon.smithy.model.traits.LengthTrait
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.RustMetadata
Expand Down Expand Up @@ -115,7 +117,12 @@ class ConstrainedMapGenerator(
*codegenScope,
)

if (!publicConstrainedTypes && isValueConstrained(shape, model, symbolProvider)) {
val valueShape = model.expectShape(shape.value.target)
if (!publicConstrainedTypes
&& isValueConstrained(valueShape, model, symbolProvider)
&& valueShape !is StructureShape
&& valueShape !is UnionShape
) {
writer.rustTemplate(
"""
impl #{From}<$name> for #{FullyUnconstrainedSymbol} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package software.amazon.smithy.rust.codegen.server.smithy.generators
import software.amazon.smithy.model.shapes.CollectionShape
import software.amazon.smithy.rust.codegen.core.rustlang.RustType
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.makeMaybeConstrained
Expand Down Expand Up @@ -38,6 +40,8 @@ class UnconstrainedCollectionGenerator(
private val symbolProvider = codegenContext.symbolProvider
private val unconstrainedShapeSymbolProvider = codegenContext.unconstrainedShapeSymbolProvider
private val pubCrateConstrainedShapeSymbolProvider = codegenContext.pubCrateConstrainedShapeSymbolProvider
private val symbol = unconstrainedShapeSymbolProvider.toSymbol(shape)
private val name = symbol.name
private val publicConstrainedTypes = codegenContext.settings.codegenConfig.publicConstrainedTypes
private val constraintViolationSymbolProvider =
with(codegenContext.constraintViolationSymbolProvider) {
Expand All @@ -47,22 +51,20 @@ class UnconstrainedCollectionGenerator(
PubCrateConstraintViolationSymbolProvider(this)
}
}
private val constraintViolationSymbol = constraintViolationSymbolProvider.toSymbol(shape)
private val constrainedShapeSymbolProvider = codegenContext.constrainedShapeSymbolProvider
private val constrainedSymbol = if (shape.isDirectlyConstrained(symbolProvider)) {
constrainedShapeSymbolProvider.toSymbol(shape)
} else {
pubCrateConstrainedShapeSymbolProvider.toSymbol(shape)
}
private val innerShape = model.expectShape(shape.member.target)

fun render() {
check(shape.canReachConstrainedShape(model, symbolProvider))

val symbol = unconstrainedShapeSymbolProvider.toSymbol(shape)
val name = symbol.name
val innerShape = model.expectShape(shape.member.target)
val innerUnconstrainedSymbol = unconstrainedShapeSymbolProvider.toSymbol(innerShape)
val constraintViolationSymbol = constraintViolationSymbolProvider.toSymbol(shape)
val innerConstraintViolationSymbol = constraintViolationSymbolProvider.toSymbol(innerShape)

unconstrainedModuleWriter.withInlineModule(symbol.module()) {
rustTemplate(
Expand All @@ -75,29 +77,49 @@ class UnconstrainedCollectionGenerator(
Self::Unconstrained(value)
}
}
""",
"InnerUnconstrainedSymbol" to innerUnconstrainedSymbol,
"MaybeConstrained" to constrainedSymbol.makeMaybeConstrained(),
)

renderTryFromUnconstrainedForConstrained(this)
}
}

private fun renderTryFromUnconstrainedForConstrained(writer: RustWriter) {
val innerConstraintViolationSymbol = constraintViolationSymbolProvider.toSymbol(innerShape)

impl #{TryFrom}<$name> for #{ConstrainedSymbol} {
type Error = #{ConstraintViolationSymbol};
writer.rustBlock("impl std::convert::TryFrom<$name> for #{T}", constrainedSymbol) {
rust("type Error = #T;", constraintViolationSymbol)

fn try_from(value: $name) -> Result<Self, Self::Error> {
let res: Result<_, (usize, #{InnerConstraintViolationSymbol})> = value
rustBlock("fn try_from(value: $name) -> Result<Self, Self::Error>") {
if (innerShape.canReachConstrainedShape(model, symbolProvider)) {
val innerConstrainedSymbol = constrainedShapeSymbolProvider.toSymbol(innerShape)

rustTemplate(
"""
let res: Result<std::vec::Vec<#{InnerConstrainedSymbol}>, (usize, #{InnerConstraintViolationSymbol})> = value
.0
.into_iter()
.enumerate()
.map(|(idx, inner)| inner.try_into().map_err(|inner_violation| (idx, inner_violation)))
.collect();
res.map(Self)
.map_err(|(idx, inner_violation)| #{ConstraintViolationSymbol}::Member(idx, inner_violation))
}
let inner = res.map_err(|(idx, inner_violation)| Self::Error::Member(idx, inner_violation))?;
""",
"InnerConstrainedSymbol" to innerConstrainedSymbol,
"InnerConstraintViolationSymbol" to innerConstraintViolationSymbol,
"TryFrom" to RuntimeType.TryFrom,
)
} else {
rust("let inner = value.0;")
}
""",
"InnerUnconstrainedSymbol" to innerUnconstrainedSymbol,
"InnerConstraintViolationSymbol" to innerConstraintViolationSymbol,
"ConstrainedSymbol" to constrainedSymbol,
"ConstraintViolationSymbol" to constraintViolationSymbol,
"MaybeConstrained" to constrainedSymbol.makeMaybeConstrained(),
"TryFrom" to RuntimeType.TryFrom,
)

if (shape.isDirectlyConstrained(symbolProvider)) {
rust("Self::try_from(inner)")
} else {
rust("Ok(Self(inner))")
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -947,10 +947,6 @@ class ServerProtocolTestGenerator(
// See https://github.com/awslabs/smithy-rs/issues/1401.
FailingTest(RestJsonValidation, "RestJsonMalformedLengthBlob_case0", TestType.MalformedRequest),
FailingTest(RestJsonValidation, "RestJsonMalformedLengthBlob_case1", TestType.MalformedRequest),
FailingTest(RestJsonValidation, "RestJsonMalformedLengthList_case0", TestType.MalformedRequest),
FailingTest(RestJsonValidation, "RestJsonMalformedLengthList_case1", TestType.MalformedRequest),
FailingTest(RestJsonValidation, "RestJsonMalformedLengthMapValue_case0", TestType.MalformedRequest),
FailingTest(RestJsonValidation, "RestJsonMalformedLengthMapValue_case1", TestType.MalformedRequest),
FailingTest(RestJsonValidation, "RestJsonMalformedRangeFloat_case0", TestType.MalformedRequest),
FailingTest(RestJsonValidation, "RestJsonMalformedRangeFloat_case1", TestType.MalformedRequest),
FailingTest(RestJsonValidation, "RestJsonMalformedRangeMaxFloat", TestType.MalformedRequest),
Expand Down