Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preventing unmarshal on primitive types #2144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's evidence that for an array of objects, unmarshal is still called.

if (obj[\\"primitiveArrayTest\\"] !== undefined) {
instance.primitiveArrayTest = obj[\\"primitiveArrayTest\\"];
}
Comment on lines +178 to +180
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's evidence that the code doesn't try to call unmarshal anymore for the array of primitive items.

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
Loading