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: typescript marshalling preset not caring about external models #927

Merged
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
8 changes: 4 additions & 4 deletions src/generators/typescript/presets/CommonPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function realizePropertyFactory(prop: string) {

function renderMarshalProperty(modelInstanceVariable: string, model: ConstrainedMetaModel) {
if (model instanceof ConstrainedReferenceModel && !(model.ref instanceof ConstrainedEnumModel)) {
return `$\{${model.type}.marshal()}`;
return `$\{${modelInstanceVariable}.marshal()}`;
}
return realizePropertyFactory(modelInstanceVariable);
}
Expand Down Expand Up @@ -45,7 +45,7 @@ function renderMarshalProperties(model: ConstrainedObjectModel) {

const marshalUnwrapDictionaryProperties = unwrapDictionaryProperties.map(([prop, propModel]) => {
const modelInstanceVariable = 'value';
const patternPropertyMarshalCode = renderMarshalProperty(modelInstanceVariable, propModel.property);
const patternPropertyMarshalCode = renderMarshalProperty(modelInstanceVariable, (propModel.property as ConstrainedDictionaryModel).value);
const marshalCode = `json += \`"$\{key}": ${patternPropertyMarshalCode},\`;`;
return `if(this.${prop} !== undefined) {
for (const [key, value] of this.${prop}.entries()) {
Expand Down Expand Up @@ -79,7 +79,7 @@ ${renderer.indent(renderMarshalProperties(model))}

function renderUnmarshalProperty(modelInstanceVariable: string, model: ConstrainedMetaModel) {
if (model instanceof ConstrainedReferenceModel && !(model.ref instanceof ConstrainedEnumModel)) {
return `$\{${model.type}.marshal()}`;
return `$\{${model.type}.unmarshal(${modelInstanceVariable})}`;
}
return `${modelInstanceVariable}`;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ function renderUnmarshalProperties(model: ConstrainedObjectModel) {
const unmarshalDictionaryProperties = [];
for (const [prop, propModel] of unwrapDictionaryProperties) {
const modelInstanceVariable = 'value as any';
const unmarshalCode = renderUnmarshalProperty(modelInstanceVariable, propModel.property);
const unmarshalCode = renderUnmarshalProperty(modelInstanceVariable, (propModel.property as ConstrainedDictionaryModel).value);
setDictionaryProperties.push(`if (instance.${prop} === undefined) {instance.${prop} = new Map();}`);
unmarshalDictionaryProperties.push(`instance.${prop}.set(key, ${unmarshalCode});`);
}
Expand Down
4 changes: 2 additions & 2 deletions test/generators/typescript/preset/MarshallingPreset.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable */

import { TypeScriptGenerator, TS_COMMON_PRESET } from '../../../../src/generators';
import Ajv from 'ajv';
const doc = {
definitions: {
'NestedTest': {
Expand All @@ -15,7 +14,8 @@ const doc = {
properties: {
'string prop': { type: 'string' },
enumProp: { $id: 'EnumTest', enum: ['Some enum String', true, {test: 'test'}, 2]},
numberProp: { type: 'number' }
numberProp: { type: 'number' },
nestedObject: {$ref: '#/definitions/NestedTest'}
}
};
describe('Marshalling preset', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
private _stringProp: string;
private _enumProp?: EnumTest;
private _numberProp?: number;
private _nestedObject?: NestedTest;
private _additionalProperties?: Map<string, NestedTest>;

constructor(input: {
stringProp: string,
enumProp?: EnumTest,
numberProp?: number,
nestedObject?: NestedTest,
additionalProperties?: Map<string, NestedTest>,
}) {
this._stringProp = input.stringProp;
this._enumProp = input.enumProp;
this._numberProp = input.numberProp;
this._nestedObject = input.nestedObject;
this._additionalProperties = input.additionalProperties;
}

Expand All @@ -28,6 +31,9 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
get numberProp(): number | undefined { return this._numberProp; }
set numberProp(numberProp: number | undefined) { this._numberProp = numberProp; }

get nestedObject(): NestedTest | undefined { return this._nestedObject; }
set nestedObject(nestedObject: NestedTest | undefined) { this._nestedObject = nestedObject; }

get additionalProperties(): Map<string, NestedTest> | undefined { return this._additionalProperties; }
set additionalProperties(additionalProperties: Map<string, NestedTest> | undefined) { this._additionalProperties = additionalProperties; }

Expand All @@ -42,11 +48,14 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
if(this.numberProp !== undefined) {
json += \`\\"numberProp\\": \${typeof this.numberProp === 'number' || typeof this.numberProp === 'boolean' ? this.numberProp : JSON.stringify(this.numberProp)},\`;
}
if(this.nestedObject !== undefined) {
json += \`\\"nestedObject\\": \${this.nestedObject.marshal()},\`;
}
if(this.additionalProperties !== undefined) {
for (const [key, value] of this.additionalProperties.entries()) {
//Only unwrap those who are not already a property in the JSON object
if(Object.keys(this).includes(String(key))) continue;
json += \`\\"\${key}\\": \${typeof value === 'number' || typeof value === 'boolean' ? value : JSON.stringify(value)},\`;
json += \`\\"\${key}\\": \${value.marshal()},\`;
}
}

Expand All @@ -67,10 +76,13 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
if (obj[\\"numberProp\\"] !== undefined) {
instance.numberProp = obj[\\"numberProp\\"];
}
if (obj[\\"nestedObject\\"] !== undefined) {
instance.nestedObject = \${NestedTest.unmarshal(obj[\\"nestedObject\\"])};
}

if (instance.additionalProperties === undefined) {instance.additionalProperties = new Map();}
for (const [key, value] of Object.entries(obj).filter((([key,]) => {return ![\\"stringProp\\",\\"enumProp\\",\\"numberProp\\",\\"additionalProperties\\"].includes(key);}))) {
instance.additionalProperties.set(key, value as any);
for (const [key, value] of Object.entries(obj).filter((([key,]) => {return ![\\"stringProp\\",\\"enumProp\\",\\"numberProp\\",\\"nestedObject\\",\\"additionalProperties\\"].includes(key);}))) {
instance.additionalProperties.set(key, \${NestedTest.unmarshal(value as any)});
}

return instance;
Expand Down