Skip to content

feat(codegen): reject null in non-sparse collections #2771

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

Merged
merged 1 commit into from
Sep 9, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,6 @@ private static boolean filterMalformedRequestTests(
if (testCase.getId().startsWith("RestJsonMalformedUnion")) {
return true;
}
//TODO: we don't do any list validation
if (testCase.getId().startsWith("RestJsonBodyMalformedList")) {
return true;
}
//TODO: we don't validate map values
if (testCase.getId().equals("RestJsonBodyMalformedMapNullValue")) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import software.amazon.smithy.model.traits.SparseTrait;
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
import software.amazon.smithy.typescript.codegen.CodegenUtils;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings.ArtifactType;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.integration.DocumentMemberDeserVisitor;
import software.amazon.smithy.typescript.codegen.integration.DocumentShapeDeserVisitor;
Expand Down Expand Up @@ -62,10 +63,11 @@ private DocumentMemberDeserVisitor getMemberVisitor(MemberShape memberShape, Str
protected void deserializeCollection(GenerationContext context, CollectionShape shape) {
TypeScriptWriter writer = context.getWriter();
Shape target = context.getModel().expectShape(shape.getMember().getTarget());
ArtifactType artifactType = context.getSettings().getArtifactType();

// Filter out null entries if we don't have the sparse trait.
String potentialFilter = "";
if (!shape.hasTrait(SparseTrait.ID)) {
if (!shape.hasTrait(SparseTrait.ID) && !artifactType.equals(ArtifactType.SSDK)) {
potentialFilter = ".filter((e: any) => e != null)";
}

Expand All @@ -75,7 +77,15 @@ protected void deserializeCollection(GenerationContext context, CollectionShape

writer.openBlock("return (output || [])$L.map((entry: any) => {", "});", potentialFilter, () -> {
// Short circuit null values from serialization.
writer.write("if (entry === null) { return null as any; }");
writer.openBlock("if (entry === null) {", "}", () -> {
// In the SSDK we want to be very strict about not accepting nulls in non-sparse lists.
if (!shape.hasTrait(SparseTrait.ID) && artifactType.equals(ArtifactType.SSDK)) {
writer.write("throw new TypeError('All elements of the non-sparse list $S must be non-null.');",
shape.getId());
} else {
writer.write("return null as any;");
}
});

if (shape.isSetShape()) {
writer.write("const parsedEntry = $L$L;",
Expand Down