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: Just emit a comment for validating SDK output structures in Rust #752

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/test_models_rust_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
- name: Set up Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: "1.80.0"
toolchain: "1.81.0"
rustflags: ""
components: rustfmt

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,45 +1049,65 @@ private String generateValidationFunction(
);
}
} else if (shape instanceof StructureShape structureShape) {
var isPositionalOutput =
(operation == null ||
operation.getOutputShape().equals(shape.getId())) &&
structureShape.hasTrait(PositionalTrait.class);
for (final var memberShape : structureShape.getAllMembers().values()) {
final var memberVariables = structureMemberVariables(memberShape);
memberVariables.put(
"memberValidationFunctionName",
shapeValidationFunctionName(null, null, memberShape)
// If this is a response shape for an AWS SDK,
// make validation a no-op for now.
// This is because the SDKs will produce values that violate constraints,
// such as a `Some({})` on an optional map with @length(min: 1).
// This could be considered an SDK bug since information is lost,
// SDKs are also not supposed to validate constraints
// which makes it harder to argue it should be fixed.
// See https://github.com/smithy-lang/smithy-dafny/issues/751
var generator = mergedGenerator.generatorForShape(shape);
if (
generator instanceof RustAwsSdkShimGenerator &&
operationIndex.isOutputStructure(shape)
) {
validationBlocks.add(
"// Validation intentionally suppressed for AWS SDK response structures"
);

if (isPositionalOutput) {
validationBlocks.add(
evalTemplate(
"$memberValidationFunctionName:L(&Some(input.clone()))?;",
memberVariables
)
);
} else if (
mergedGenerator
.generatorForShape(structureShape)
.isRustFieldRequired(structureShape, memberShape) &&
// TODO: This may be more correct than the current isRustFieldRequired in general
!(operationIndex.isOutputStructure(structureShape) &&
model.expectShape(memberShape.getTarget()).isListShape())
) {
validationBlocks.add(
evalTemplate(
"$memberValidationFunctionName:L(&Some(input.$fieldName:L.clone()))?;",
memberVariables
)
);
} else {
validationBlocks.add(
evalTemplate(
"$memberValidationFunctionName:L(&input.$fieldName:L)?;",
memberVariables
)
} else {
var isPositionalOutput =
(operation == null ||
operation.getOutputShape().equals(shape.getId())) &&
structureShape.hasTrait(PositionalTrait.class);
for (final var memberShape : structureShape
.getAllMembers()
.values()) {
final var memberVariables = structureMemberVariables(memberShape);
memberVariables.put(
"memberValidationFunctionName",
shapeValidationFunctionName(null, null, memberShape)
);

if (isPositionalOutput) {
validationBlocks.add(
evalTemplate(
"$memberValidationFunctionName:L(&Some(input.clone()))?;",
memberVariables
)
);
} else if (
mergedGenerator
.generatorForShape(structureShape)
.isRustFieldRequired(structureShape, memberShape) &&
// TODO: This may be more correct than the current isRustFieldRequired in general
!(operationIndex.isOutputStructure(structureShape) &&
model.expectShape(memberShape.getTarget()).isListShape())
) {
validationBlocks.add(
evalTemplate(
"$memberValidationFunctionName:L(&Some(input.$fieldName:L.clone()))?;",
memberVariables
)
);
} else {
validationBlocks.add(
evalTemplate(
"$memberValidationFunctionName:L(&input.$fieldName:L)?;",
memberVariables
)
);
}
}
}
} else if (shape instanceof UnionShape unionShape) {
Expand Down
Loading