Skip to content

Commit 1062ef0

Browse files
committed
fix: preventing unmarshall on primitive types
1 parent 1feddbd commit 1062ef0

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ function renderUnmarshalProperty(
2727

2828
if (
2929
model instanceof ConstrainedArrayModel &&
30+
model.valueModel instanceof ConstrainedReferenceModel &&
31+
!(model.valueModel.ref instanceof ConstrainedEnumModel) &&
3032
!(model.valueModel instanceof ConstrainedUnionModel)
3133
) {
3234
return `${modelInstanceVariable} == null

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ const doc = {
5757
$ref: '#/definitions/NestedTest'
5858
}
5959
},
60+
primitiveArrayTest: {
61+
type: 'array',
62+
additionalItems: false,
63+
items: {
64+
type: 'string'
65+
}
66+
},
6067
tupleTest: {
6168
type: 'array',
6269
additionalItems: false,

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
99
private _unionTest?: NestedTest | string;
1010
private _unionArrayTest?: (NestedTest | string)[];
1111
private _arrayTest?: NestedTest[];
12+
private _primitiveArrayTest?: string[];
1213
private _tupleTest?: [NestedTest, string];
1314
private _constTest?: 'TEST' = 'TEST';
1415
private _additionalProperties?: Map<string, NestedTest | string>;
@@ -21,6 +22,7 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
2122
unionTest?: NestedTest | string,
2223
unionArrayTest?: (NestedTest | string)[],
2324
arrayTest?: NestedTest[],
25+
primitiveArrayTest?: string[],
2426
tupleTest?: [NestedTest, string],
2527
additionalProperties?: Map<string, NestedTest | string>,
2628
}) {
@@ -31,6 +33,7 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
3133
this._unionTest = input.unionTest;
3234
this._unionArrayTest = input.unionArrayTest;
3335
this._arrayTest = input.arrayTest;
36+
this._primitiveArrayTest = input.primitiveArrayTest;
3437
this._tupleTest = input.tupleTest;
3538
this._additionalProperties = input.additionalProperties;
3639
}
@@ -56,6 +59,9 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
5659
get arrayTest(): NestedTest[] | undefined { return this._arrayTest; }
5760
set arrayTest(arrayTest: NestedTest[] | undefined) { this._arrayTest = arrayTest; }
5861
62+
get primitiveArrayTest(): string[] | undefined { return this._primitiveArrayTest; }
63+
set primitiveArrayTest(primitiveArrayTest: string[] | undefined) { this._primitiveArrayTest = primitiveArrayTest; }
64+
5965
get tupleTest(): [NestedTest, string] | undefined { return this._tupleTest; }
6066
set tupleTest(tupleTest: [NestedTest, string] | undefined) { this._tupleTest = tupleTest; }
6167
@@ -103,6 +109,13 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
103109
}
104110
json += \`\\"arrayTest\\": [\${arrayTestJsonValues.join(',')}],\`;
105111
}
112+
if(this.primitiveArrayTest !== undefined) {
113+
let primitiveArrayTestJsonValues: any[] = [];
114+
for (const unionItem of this.primitiveArrayTest) {
115+
primitiveArrayTestJsonValues.push(\`\${typeof unionItem === 'number' || typeof unionItem === 'boolean' ? unionItem : JSON.stringify(unionItem)}\`);
116+
}
117+
json += \`\\"primitiveArrayTest\\": [\${primitiveArrayTestJsonValues.join(',')}],\`;
118+
}
106119
if(this.tupleTest !== undefined) {
107120
const serializedTuple = [];
108121
if(this.tupleTest[0]) {
@@ -123,7 +136,7 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
123136
if(this.additionalProperties !== undefined) {
124137
for (const [key, value] of this.additionalProperties.entries()) {
125138
//Only unwrap those that are not already a property in the JSON object
126-
if([\\"string prop\\",\\"enumProp\\",\\"numberProp\\",\\"nestedObject\\",\\"unionTest\\",\\"unionArrayTest\\",\\"arrayTest\\",\\"tupleTest\\",\\"constTest\\",\\"additionalProperties\\"].includes(String(key))) continue;
139+
if([\\"string prop\\",\\"enumProp\\",\\"numberProp\\",\\"nestedObject\\",\\"unionTest\\",\\"unionArrayTest\\",\\"arrayTest\\",\\"primitiveArrayTest\\",\\"tupleTest\\",\\"constTest\\",\\"additionalProperties\\"].includes(String(key))) continue;
127140
if(value instanceof NestedTest) {
128141
json += \`\\"\${key}\\": \${value.marshal()},\`;
129142
} else {
@@ -162,13 +175,16 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
162175
? null
163176
: obj[\\"arrayTest\\"].map((item: any) => NestedTest.unmarshal(item));
164177
}
178+
if (obj[\\"primitiveArrayTest\\"] !== undefined) {
179+
instance.primitiveArrayTest = obj[\\"primitiveArrayTest\\"];
180+
}
165181
if (obj[\\"tupleTest\\"] !== undefined) {
166182
instance.tupleTest = obj[\\"tupleTest\\"];
167183
}
168184
169185
170186
instance.additionalProperties = new Map();
171-
const propsToCheck = Object.entries(obj).filter((([key,]) => {return ![\\"string prop\\",\\"enumProp\\",\\"numberProp\\",\\"nestedObject\\",\\"unionTest\\",\\"unionArrayTest\\",\\"arrayTest\\",\\"tupleTest\\",\\"constTest\\",\\"additionalProperties\\"].includes(key);}));
187+
const propsToCheck = Object.entries(obj).filter((([key,]) => {return ![\\"string prop\\",\\"enumProp\\",\\"numberProp\\",\\"nestedObject\\",\\"unionTest\\",\\"unionArrayTest\\",\\"arrayTest\\",\\"primitiveArrayTest\\",\\"tupleTest\\",\\"constTest\\",\\"additionalProperties\\"].includes(key);}));
172188
for (const [key, value] of propsToCheck) {
173189
instance.additionalProperties.set(key, value as any);
174190
}

0 commit comments

Comments
 (0)