From e101bb6ead675ab829f71f785045dc99436895fc Mon Sep 17 00:00:00 2001 From: Lucas Macedo Date: Fri, 24 Jan 2025 10:52:47 -0300 Subject: [PATCH 1/3] fix: preventing unmarshal on primitive types (#2144) --- .../presets/utils/UnmarshalFunction.ts | 2 ++ .../preset/MarshallingPreset.spec.ts | 7 +++++++ .../MarshallingPreset.spec.ts.snap | 20 +++++++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/generators/typescript/presets/utils/UnmarshalFunction.ts b/src/generators/typescript/presets/utils/UnmarshalFunction.ts index f4175febab..c8aea0e57e 100644 --- a/src/generators/typescript/presets/utils/UnmarshalFunction.ts +++ b/src/generators/typescript/presets/utils/UnmarshalFunction.ts @@ -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 diff --git a/test/generators/typescript/preset/MarshallingPreset.spec.ts b/test/generators/typescript/preset/MarshallingPreset.spec.ts index 68a121045a..b247d3d20b 100644 --- a/test/generators/typescript/preset/MarshallingPreset.spec.ts +++ b/test/generators/typescript/preset/MarshallingPreset.spec.ts @@ -57,6 +57,13 @@ const doc = { $ref: '#/definitions/NestedTest' } }, + primitiveArrayTest: { + type: 'array', + additionalItems: false, + items: { + type: 'string' + } + }, tupleTest: { type: 'array', additionalItems: false, diff --git a/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap b/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap index 66ca9c668c..eb725d2811 100644 --- a/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap +++ b/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap @@ -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; @@ -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, }) { @@ -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; } @@ -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; } @@ -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]) { @@ -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 { @@ -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); } From 2c435473c87599d2008d742d003b7f232be18ace Mon Sep 17 00:00:00 2001 From: asyncapi-bot Date: Sat, 25 Jan 2025 10:31:12 +0100 Subject: [PATCH 2/3] chore(release): v3.10.1 (#2153) Co-authored-by: asyncapi-bot --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0b753154ac..1b5436ec7e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@asyncapi/modelina", - "version": "3.10.0", + "version": "3.10.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@asyncapi/modelina", - "version": "3.10.0", + "version": "3.10.1", "license": "Apache-2.0", "dependencies": { "@apidevtools/json-schema-ref-parser": "^11.1.0", diff --git a/package.json b/package.json index dbcf6b0736..7abde35010 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@asyncapi/modelina", - "version": "3.10.0", + "version": "3.10.1", "description": "Library for generating data models based on inputs such as AsyncAPI, OpenAPI, or JSON Schema documents", "license": "Apache-2.0", "homepage": "https://www.modelina.org", From 565f92d3ed07163c622944787b94349023574268 Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Sat, 25 Jan 2025 10:33:16 +0100 Subject: [PATCH 3/3] docs: add Shriya-Chauhan as a contributor for code (#2149) * update README.md * update .all-contributorsrc --------- Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> Co-authored-by: Jonas Lagoni --- .all-contributorsrc | 9 +++++++++ README.md | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 5156214448..5fa9614469 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -1052,6 +1052,15 @@ "bug", "test" ] + }, + { + "login": "Shriya-Chauhan", + "name": "Shriya Chauhan", + "avatar_url": "https://avatars.githubusercontent.com/u/78415084?v=4", + "profile": "https://github.com/Shriya-Chauhan", + "contributions": [ + "code" + ] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index 939836b8d3..110ad38dac 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ [![Discussions](https://img.shields.io/github/discussions/asyncapi/modelina)](https://github.com/asyncapi/modelina/discussions) [![Website](https://img.shields.io/website?label=website&url=https%3A%2F%2Fwww.modelina.org)](https://www.modelina.org) [![Playground](https://img.shields.io/website?label=playground&url=https%3A%2F%2Fwww.modelina.org%2Fplayground)](https://www.modelina.org/playground) -[![All Contributors](https://img.shields.io/badge/all_contributors-97-orange.svg?style=flat-square)](#contributors-) +[![All Contributors](https://img.shields.io/badge/all_contributors-98-orange.svg?style=flat-square)](#contributors-) Your one-stop tool for generating accurate and well-tested models for representing the message payloads. Use it as a tool in your development workflow, or a library in a larger integrations, entirely in your control. @@ -447,6 +447,7 @@ Thanks go out to these wonderful people ([emoji key](https://allcontributors.org maxplatov
maxplatov

👀 Emmanuel Ferdman
Emmanuel Ferdman

📖 Jonjo McKay
Jonjo McKay

💻 🐛 ⚠️ + Shriya Chauhan
Shriya Chauhan

💻