28
28
import software .amazon .smithy .model .shapes .StructureShape ;
29
29
import software .amazon .smithy .model .shapes .UnionShape ;
30
30
import software .amazon .smithy .model .traits .JsonNameTrait ;
31
+ import software .amazon .smithy .model .traits .MediaTypeTrait ;
31
32
import software .amazon .smithy .model .traits .SparseTrait ;
32
33
import software .amazon .smithy .model .traits .TimestampFormatTrait .Format ;
34
+ import software .amazon .smithy .typescript .codegen .CodegenUtils ;
33
35
import software .amazon .smithy .typescript .codegen .TypeScriptWriter ;
34
36
import software .amazon .smithy .typescript .codegen .integration .DocumentMemberDeserVisitor ;
35
37
import software .amazon .smithy .typescript .codegen .integration .DocumentShapeDeserVisitor ;
@@ -132,7 +134,7 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
132
134
.orElse (memberName );
133
135
Shape target = context .getModel ().expectShape (memberShape .getTarget ());
134
136
135
- if (target . isBooleanShape () || target instanceof NumberShape ) {
137
+ if (usesExpect ( target ) ) {
136
138
// Booleans and numbers will call expectBoolean/expectNumber which will handle
137
139
// null/undefined properly.
138
140
writer .write ("$L: $L," , memberName , target .accept (getMemberVisitor ("output." + locationName )));
@@ -146,6 +148,16 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
146
148
});
147
149
}
148
150
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
+
149
161
@ Override
150
162
protected void deserializeUnion (GenerationContext context , UnionShape shape ) {
151
163
TypeScriptWriter writer = context .getWriter ();
@@ -161,7 +173,7 @@ protected void deserializeUnion(GenerationContext context, UnionShape shape) {
161
173
.orElse (memberName );
162
174
163
175
String memberValue = target .accept (getMemberVisitor ("output." + locationName ));
164
- if (target . isBooleanShape () || target instanceof NumberShape ) {
176
+ if (usesExpect ( target ) ) {
165
177
// Booleans and numbers will call expectBoolean/expectNumber which will handle
166
178
// null/undefined properly.
167
179
writer .openBlock ("if ((val = $L) !== undefined) {" , "}" , memberValue , () -> {
0 commit comments