23
23
import software .amazon .smithy .model .shapes .DocumentShape ;
24
24
import software .amazon .smithy .model .shapes .MapShape ;
25
25
import software .amazon .smithy .model .shapes .MemberShape ;
26
+ import software .amazon .smithy .model .shapes .NumberShape ;
26
27
import software .amazon .smithy .model .shapes .Shape ;
27
28
import software .amazon .smithy .model .shapes .StructureShape ;
28
29
import software .amazon .smithy .model .shapes .UnionShape ;
@@ -131,10 +132,16 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
131
132
.orElse (memberName );
132
133
Shape target = context .getModel ().expectShape (memberShape .getTarget ());
133
134
134
- writer .write ("$1L: (output.$2L !== undefined && output.$2L !== null)"
135
- + " ? $3L: undefined," , memberName , locationName ,
136
- // Dispatch to the output value provider for any additional handling.
137
- target .accept (getMemberVisitor ("output." + locationName )));
135
+ if (target .isBooleanShape () || target instanceof NumberShape ) {
136
+ // Booleans and numbers will call expectBoolean/expectNumber which will handle
137
+ // null/undefined properly.
138
+ writer .write ("$L: $L," , memberName , target .accept (getMemberVisitor ("output." + locationName )));
139
+ } else {
140
+ writer .write ("$1L: (output.$2L !== undefined && output.$2L !== null)"
141
+ + " ? $3L: undefined," , memberName , locationName ,
142
+ // Dispatch to the output value provider for any additional handling.
143
+ target .accept (getMemberVisitor ("output." + locationName )));
144
+ }
138
145
});
139
146
});
140
147
}
@@ -152,13 +159,23 @@ protected void deserializeUnion(GenerationContext context, UnionShape shape) {
152
159
String locationName = memberShape .getTrait (JsonNameTrait .class )
153
160
.map (JsonNameTrait ::getValue )
154
161
.orElse (memberName );
155
- writer .openBlock ("if (output.$L !== undefined && output.$L !== null) {" , "}" , locationName , locationName ,
156
- () -> {
157
- writer .openBlock ("return {" , "};" , () -> {
158
- // Dispatch to the output value provider for any additional handling.
159
- writer .write ("$L: $L" , memberName , target .accept (getMemberVisitor ("output." + locationName )));
162
+
163
+ String memberValue = target .accept (getMemberVisitor ("output." + locationName ));
164
+ if (target .isBooleanShape () || target instanceof NumberShape ) {
165
+ // Booleans and numbers will call expectBoolean/expectNumber which will handle
166
+ // null/undefined properly.
167
+ writer .openBlock ("if ((val = $L) !== undefined) {" , "}" , memberValue , () -> {
168
+ writer .write ("return { $L: val }" , memberName );
160
169
});
161
- });
170
+ } else {
171
+ writer .openBlock ("if (output.$L !== undefined && output.$L !== null) {" , "}" , locationName ,
172
+ locationName , () -> {
173
+ writer .openBlock ("return {" , "};" , () -> {
174
+ // Dispatch to the output value provider for any additional handling.
175
+ writer .write ("$L: $L" , memberName , memberValue );
176
+ });
177
+ });
178
+ }
162
179
});
163
180
// Or write to the unknown member the element in the output.
164
181
writer .write ("return { $$unknown: Object.entries(output)[0] };" );
0 commit comments