Skip to content

Commit

Permalink
fix: preventing unmarshall on primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
lsmacedo committed Jan 2, 2025
1 parent 1feddbd commit 1062ef0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/generators/typescript/presets/utils/UnmarshalFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ function renderUnmarshalProperty(

if (
model instanceof ConstrainedArrayModel &&
model.valueModel instanceof ConstrainedReferenceModel &&
!(model.valueModel.ref instanceof ConstrainedEnumModel) &&
!(model.valueModel instanceof ConstrainedUnionModel)
) {
return `${modelInstanceVariable} == null
Expand Down
7 changes: 7 additions & 0 deletions test/generators/typescript/preset/MarshallingPreset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ const doc = {
$ref: '#/definitions/NestedTest'
}
},
primitiveArrayTest: {
type: 'array',
additionalItems: false,
items: {
type: 'string'
}
},
tupleTest: {
type: 'array',
additionalItems: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
private _unionTest?: NestedTest | string;
private _unionArrayTest?: (NestedTest | string)[];
private _arrayTest?: NestedTest[];
private _primitiveArrayTest?: string[];
private _tupleTest?: [NestedTest, string];
private _constTest?: 'TEST' = 'TEST';
private _additionalProperties?: Map<string, NestedTest | string>;
Expand All @@ -21,6 +22,7 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
unionTest?: NestedTest | string,
unionArrayTest?: (NestedTest | string)[],
arrayTest?: NestedTest[],
primitiveArrayTest?: string[],
tupleTest?: [NestedTest, string],
additionalProperties?: Map<string, NestedTest | string>,
}) {
Expand All @@ -31,6 +33,7 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
this._unionTest = input.unionTest;
this._unionArrayTest = input.unionArrayTest;
this._arrayTest = input.arrayTest;
this._primitiveArrayTest = input.primitiveArrayTest;
this._tupleTest = input.tupleTest;
this._additionalProperties = input.additionalProperties;
}
Expand All @@ -56,6 +59,9 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
get arrayTest(): NestedTest[] | undefined { return this._arrayTest; }
set arrayTest(arrayTest: NestedTest[] | undefined) { this._arrayTest = arrayTest; }
get primitiveArrayTest(): string[] | undefined { return this._primitiveArrayTest; }
set primitiveArrayTest(primitiveArrayTest: string[] | undefined) { this._primitiveArrayTest = primitiveArrayTest; }
get tupleTest(): [NestedTest, string] | undefined { return this._tupleTest; }
set tupleTest(tupleTest: [NestedTest, string] | undefined) { this._tupleTest = tupleTest; }
Expand Down Expand Up @@ -103,6 +109,13 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
}
json += \`\\"arrayTest\\": [\${arrayTestJsonValues.join(',')}],\`;
}
if(this.primitiveArrayTest !== undefined) {
let primitiveArrayTestJsonValues: any[] = [];
for (const unionItem of this.primitiveArrayTest) {
primitiveArrayTestJsonValues.push(\`\${typeof unionItem === 'number' || typeof unionItem === 'boolean' ? unionItem : JSON.stringify(unionItem)}\`);
}
json += \`\\"primitiveArrayTest\\": [\${primitiveArrayTestJsonValues.join(',')}],\`;
}
if(this.tupleTest !== undefined) {
const serializedTuple = [];
if(this.tupleTest[0]) {
Expand All @@ -123,7 +136,7 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
if(this.additionalProperties !== undefined) {
for (const [key, value] of this.additionalProperties.entries()) {
//Only unwrap those that are not already a property in the JSON object
if([\\"string prop\\",\\"enumProp\\",\\"numberProp\\",\\"nestedObject\\",\\"unionTest\\",\\"unionArrayTest\\",\\"arrayTest\\",\\"tupleTest\\",\\"constTest\\",\\"additionalProperties\\"].includes(String(key))) continue;
if([\\"string prop\\",\\"enumProp\\",\\"numberProp\\",\\"nestedObject\\",\\"unionTest\\",\\"unionArrayTest\\",\\"arrayTest\\",\\"primitiveArrayTest\\",\\"tupleTest\\",\\"constTest\\",\\"additionalProperties\\"].includes(String(key))) continue;
if(value instanceof NestedTest) {
json += \`\\"\${key}\\": \${value.marshal()},\`;
} else {
Expand Down Expand Up @@ -162,13 +175,16 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
? null
: obj[\\"arrayTest\\"].map((item: any) => NestedTest.unmarshal(item));
}
if (obj[\\"primitiveArrayTest\\"] !== undefined) {
instance.primitiveArrayTest = obj[\\"primitiveArrayTest\\"];
}
if (obj[\\"tupleTest\\"] !== undefined) {
instance.tupleTest = obj[\\"tupleTest\\"];
}
instance.additionalProperties = new Map();
const propsToCheck = Object.entries(obj).filter((([key,]) => {return ![\\"string prop\\",\\"enumProp\\",\\"numberProp\\",\\"nestedObject\\",\\"unionTest\\",\\"unionArrayTest\\",\\"arrayTest\\",\\"tupleTest\\",\\"constTest\\",\\"additionalProperties\\"].includes(key);}));
const propsToCheck = Object.entries(obj).filter((([key,]) => {return ![\\"string prop\\",\\"enumProp\\",\\"numberProp\\",\\"nestedObject\\",\\"unionTest\\",\\"unionArrayTest\\",\\"arrayTest\\",\\"primitiveArrayTest\\",\\"tupleTest\\",\\"constTest\\",\\"additionalProperties\\"].includes(key);}));
for (const [key, value] of propsToCheck) {
instance.additionalProperties.set(key, value as any);
}
Expand Down

0 comments on commit 1062ef0

Please sign in to comment.