Skip to content

Commit e958194

Browse files
chore: reduce generated code for strings
1 parent 998e09c commit e958194

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
import software.amazon.smithy.model.shapes.StructureShape;
2929
import software.amazon.smithy.model.shapes.UnionShape;
3030
import software.amazon.smithy.model.traits.JsonNameTrait;
31+
import software.amazon.smithy.model.traits.MediaTypeTrait;
3132
import software.amazon.smithy.model.traits.SparseTrait;
3233
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
34+
import software.amazon.smithy.typescript.codegen.CodegenUtils;
3335
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
3436
import software.amazon.smithy.typescript.codegen.integration.DocumentMemberDeserVisitor;
3537
import software.amazon.smithy.typescript.codegen.integration.DocumentShapeDeserVisitor;
@@ -132,7 +134,7 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
132134
.orElse(memberName);
133135
Shape target = context.getModel().expectShape(memberShape.getTarget());
134136

135-
if (target.isBooleanShape() || target instanceof NumberShape) {
137+
if (usesExpect(target)) {
136138
// Booleans and numbers will call expectBoolean/expectNumber which will handle
137139
// null/undefined properly.
138140
writer.write("$L: $L,", memberName, target.accept(getMemberVisitor("output." + locationName)));
@@ -146,6 +148,16 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
146148
});
147149
}
148150

151+
private boolean usesExpect(Shape shape) {
152+
if (shape.isStringShape()) {
153+
if (shape.hasTrait(MediaTypeTrait.class)) {
154+
return !CodegenUtils.isJsonMediaType(shape.expectTrait(MediaTypeTrait.class).getValue());
155+
}
156+
return true;
157+
}
158+
return shape.isBooleanShape() || shape instanceof NumberShape;
159+
}
160+
149161
@Override
150162
protected void deserializeUnion(GenerationContext context, UnionShape shape) {
151163
TypeScriptWriter writer = context.getWriter();
@@ -161,7 +173,7 @@ protected void deserializeUnion(GenerationContext context, UnionShape shape) {
161173
.orElse(memberName);
162174

163175
String memberValue = target.accept(getMemberVisitor("output." + locationName));
164-
if (target.isBooleanShape() || target instanceof NumberShape) {
176+
if (usesExpect(target)) {
165177
// Booleans and numbers will call expectBoolean/expectNumber which will handle
166178
// null/undefined properly.
167179
writer.openBlock("if ((val = $L) !== undefined) {", "}", memberValue, () -> {

packages/smithy-client/src/parse-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function expectBoolean(value: any): boolean | undefined {
2222
* @returns The value if it's a number, undefined if it's null/undefined,
2323
* otherwise an error is thrown.
2424
*/
25-
export function expectNumber(value: any): number {
25+
export function expectNumber(value: any): number | undefined {
2626
if (value === null || value === undefined) {
2727
return undefined;
2828
}
@@ -39,7 +39,7 @@ export function expectNumber(value: any): number {
3939
* @returns The value if it's a string, undefined if it's null/undefined,
4040
* otherwise an error is thrown.
4141
*/
42-
export function expectString(value: any): string {
42+
export function expectString(value: any): string | undefined {
4343
if (value === null || value === undefined) {
4444
return undefined;
4545
}

0 commit comments

Comments
 (0)