Skip to content

Commit ca1177a

Browse files
authored
feat: add support for typed array unmarshalling in TypeScript (#2131)
1 parent 8374586 commit ca1177a

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/generators/typescript/presets/utils/UnmarshalFunction.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { ClassRenderer } from '../../renderers/ClassRenderer';
22
import { getDictionary, getNormalProperties } from '../../../../helpers';
33
import {
4+
ConstrainedArrayModel,
45
ConstrainedDictionaryModel,
56
ConstrainedEnumModel,
67
ConstrainedMetaModel,
78
ConstrainedObjectModel,
89
ConstrainedObjectPropertyModel,
9-
ConstrainedReferenceModel
10+
ConstrainedReferenceModel,
11+
ConstrainedUnionModel
1012
} from '../../../../models';
1113

1214
/**
@@ -22,6 +24,16 @@ function renderUnmarshalProperty(
2224
) {
2325
return `${model.type}.unmarshal(${modelInstanceVariable})`;
2426
}
27+
28+
if (
29+
model instanceof ConstrainedArrayModel &&
30+
!(model.valueModel instanceof ConstrainedUnionModel)
31+
) {
32+
return `${modelInstanceVariable} == null
33+
? null
34+
: ${modelInstanceVariable}.map((item: any) => ${model.valueModel.type}.unmarshal(item))`;
35+
}
36+
2537
return `${modelInstanceVariable}`;
2638
}
2739

test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
158158
instance.unionArrayTest = obj[\\"unionArrayTest\\"];
159159
}
160160
if (obj[\\"arrayTest\\"] !== undefined) {
161-
instance.arrayTest = obj[\\"arrayTest\\"];
161+
instance.arrayTest = obj[\\"arrayTest\\"] == null
162+
? null
163+
: obj[\\"arrayTest\\"].map((item: any) => NestedTest.unmarshal(item));
162164
}
163165
if (obj[\\"tupleTest\\"] !== undefined) {
164166
instance.tupleTest = obj[\\"tupleTest\\"];

0 commit comments

Comments
 (0)