diff --git a/src/generators/typescript/presets/utils/UnmarshalFunction.ts b/src/generators/typescript/presets/utils/UnmarshalFunction.ts index 9e75d5729e..f4175febab 100644 --- a/src/generators/typescript/presets/utils/UnmarshalFunction.ts +++ b/src/generators/typescript/presets/utils/UnmarshalFunction.ts @@ -1,12 +1,14 @@ import { ClassRenderer } from '../../renderers/ClassRenderer'; import { getDictionary, getNormalProperties } from '../../../../helpers'; import { + ConstrainedArrayModel, ConstrainedDictionaryModel, ConstrainedEnumModel, ConstrainedMetaModel, ConstrainedObjectModel, ConstrainedObjectPropertyModel, - ConstrainedReferenceModel + ConstrainedReferenceModel, + ConstrainedUnionModel } from '../../../../models'; /** @@ -22,6 +24,16 @@ function renderUnmarshalProperty( ) { return `${model.type}.unmarshal(${modelInstanceVariable})`; } + + if ( + model instanceof ConstrainedArrayModel && + !(model.valueModel instanceof ConstrainedUnionModel) + ) { + return `${modelInstanceVariable} == null + ? null + : ${modelInstanceVariable}.map((item: any) => ${model.valueModel.type}.unmarshal(item))`; + } + return `${modelInstanceVariable}`; } diff --git a/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap b/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap index 6e075e4fb0..66ca9c668c 100644 --- a/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap +++ b/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap @@ -158,7 +158,9 @@ exports[`Marshalling preset should render un/marshal code 1`] = ` instance.unionArrayTest = obj[\\"unionArrayTest\\"]; } if (obj[\\"arrayTest\\"] !== undefined) { - instance.arrayTest = obj[\\"arrayTest\\"]; + instance.arrayTest = obj[\\"arrayTest\\"] == null + ? null + : obj[\\"arrayTest\\"].map((item: any) => NestedTest.unmarshal(item)); } if (obj[\\"tupleTest\\"] !== undefined) { instance.tupleTest = obj[\\"tupleTest\\"];