Skip to content

Commit b6644b7

Browse files
chore: clean up typing issues
1 parent 37a7c8f commit b6644b7

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonShapeDeserVisitor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected void deserializeCollection(GenerationContext context, CollectionShape
7474
writer.write("if (entry === null) { return null as any; }");
7575

7676
// Dispatch to the output value provider for any additional handling.
77-
writer.write("return $L;", target.accept(getMemberVisitor("entry")));
77+
writer.write("return $L$L;", target.accept(getMemberVisitor("entry")), usesExpect(target) ? " as any" : "");
7878
});
7979
}
8080

@@ -112,7 +112,8 @@ protected void deserializeMap(GenerationContext context, MapShape shape) {
112112
writer.openBlock("return {", "};", () -> {
113113
writer.write("...acc,");
114114
// Dispatch to the output value provider for any additional handling.
115-
writer.write("[key]: $L", target.accept(getMemberVisitor("value")));
115+
writer.write("[key]: $L$L", target.accept(getMemberVisitor("value")),
116+
usesExpect(target) ? " as any" : "");
116117
});
117118
}
118119
);
@@ -176,8 +177,8 @@ protected void deserializeUnion(GenerationContext context, UnionShape shape) {
176177
if (usesExpect(target)) {
177178
// Booleans and numbers will call expectBoolean/expectNumber which will handle
178179
// null/undefined properly.
179-
writer.openBlock("if ((val = $L) !== undefined) {", "}", memberValue, () -> {
180-
writer.write("return { $L: val }", memberName);
180+
writer.openBlock("if ($L !== undefined) {", "}", memberValue, () -> {
181+
writer.write("return { $L: $L as any }", memberName, memberValue);
181182
});
182183
} else {
183184
writer.openBlock("if (output.$L !== undefined && output.$L !== null) {", "}", locationName,

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/XmlShapeDeserVisitor.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
import software.amazon.smithy.model.shapes.Shape;
3030
import software.amazon.smithy.model.shapes.StructureShape;
3131
import software.amazon.smithy.model.shapes.UnionShape;
32+
import software.amazon.smithy.model.traits.MediaTypeTrait;
3233
import software.amazon.smithy.model.traits.SparseTrait;
3334
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
3435
import software.amazon.smithy.model.traits.XmlFlattenedTrait;
3536
import software.amazon.smithy.model.traits.XmlNameTrait;
37+
import software.amazon.smithy.typescript.codegen.CodegenUtils;
3638
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
3739
import software.amazon.smithy.typescript.codegen.integration.DocumentMemberDeserVisitor;
3840
import software.amazon.smithy.typescript.codegen.integration.DocumentShapeDeserVisitor;
@@ -78,7 +80,8 @@ protected void deserializeCollection(GenerationContext context, CollectionShape
7880
writer.write("if (entry === null) { return null as any; }");
7981

8082
String dataSource = getUnnamedTargetWrapper(context, target, "entry");
81-
writer.write("return $L;", target.accept(getMemberVisitor(dataSource)));
83+
writer.write("return $L$L;", target.accept(getMemberVisitor(dataSource)),
84+
usesExpect(target) ? " as any" : "");
8285
});
8386
}
8487

@@ -133,7 +136,8 @@ protected void deserializeMap(GenerationContext context, MapShape shape) {
133136
writer.openBlock("return {", "};", () -> {
134137
writer.write("...acc,");
135138
// Dispatch to the output value provider for any additional handling.
136-
writer.write("[pair[$S]]: $L", keyLocation, target.accept(getMemberVisitor(dataSource)));
139+
writer.write("[pair[$S]]: $L$L", keyLocation, target.accept(getMemberVisitor(dataSource)),
140+
usesExpect(target) ? " as any" : "");
137141
});
138142
});
139143
}
@@ -269,12 +273,22 @@ protected void deserializeUnion(GenerationContext context, UnionShape shape) {
269273
deserializeNamedMember(context, memberName, memberShape, "output", (dataSource, visitor) -> {
270274
writer.openBlock("return {", "};", () -> {
271275
// Dispatch to the output value provider for any additional handling.
272-
writer.write("$L: $L", memberName, target.accept(visitor));
276+
writer.write("$L: $L$L", memberName, target.accept(visitor), usesExpect(target) ? " as any" : "");
273277
});
274278
});
275279
});
276280

277281
// Or write output element to the unknown member.
278282
writer.write("return { $$unknown: Object.entries(output)[0] };");
279283
}
284+
285+
private boolean usesExpect(Shape shape) {
286+
if (shape.isStringShape()) {
287+
if (shape.hasTrait(MediaTypeTrait.class)) {
288+
return !CodegenUtils.isJsonMediaType(shape.expectTrait(MediaTypeTrait.class).getValue());
289+
}
290+
return true;
291+
}
292+
return false;
293+
}
280294
}

0 commit comments

Comments
 (0)