Skip to content

Commit

Permalink
feat: Add support for typed array unmarshalling in TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjomckay committed Nov 29, 2024
1 parent 12ea332 commit 1d70296
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/generators/typescript/presets/utils/UnmarshalFunction.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand All @@ -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}`;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\\"];
Expand Down

0 comments on commit 1d70296

Please sign in to comment.