diff --git a/src/generators/typescript/presets/utils/UnmarshalFunction.ts b/src/generators/typescript/presets/utils/UnmarshalFunction.ts index 373db05dba..9e75d5729e 100644 --- a/src/generators/typescript/presets/utils/UnmarshalFunction.ts +++ b/src/generators/typescript/presets/utils/UnmarshalFunction.ts @@ -29,6 +29,10 @@ function renderUnmarshalProperty( * Render the code for unmarshalling of regular properties */ function unmarshalRegularProperty(propModel: ConstrainedObjectPropertyModel) { + if (propModel.property.options.const) { + return undefined; + } + const modelInstanceVariable = `obj["${propModel.unconstrainedPropertyName}"]`; const unmarshalCode = renderUnmarshalProperty( modelInstanceVariable, diff --git a/test/generators/typescript/preset/MarshallingPreset.spec.ts b/test/generators/typescript/preset/MarshallingPreset.spec.ts index fa1b2d90f3..68a121045a 100644 --- a/test/generators/typescript/preset/MarshallingPreset.spec.ts +++ b/test/generators/typescript/preset/MarshallingPreset.spec.ts @@ -68,6 +68,10 @@ const doc = { type: 'string' } ] + }, + constTest: { + type: 'string', + const: 'TEST' } } }; diff --git a/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap b/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap index 763b32ab54..6e075e4fb0 100644 --- a/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap +++ b/test/generators/typescript/preset/__snapshots__/MarshallingPreset.spec.ts.snap @@ -10,6 +10,7 @@ exports[`Marshalling preset should render un/marshal code 1`] = ` private _unionArrayTest?: (NestedTest | string)[]; private _arrayTest?: NestedTest[]; private _tupleTest?: [NestedTest, string]; + private _constTest?: 'TEST' = 'TEST'; private _additionalProperties?: Map; constructor(input: { @@ -58,6 +59,8 @@ exports[`Marshalling preset should render un/marshal code 1`] = ` get tupleTest(): [NestedTest, string] | undefined { return this._tupleTest; } set tupleTest(tupleTest: [NestedTest, string] | undefined) { this._tupleTest = tupleTest; } + get constTest(): 'TEST' | undefined { return this._constTest; } + get additionalProperties(): Map | undefined { return this._additionalProperties; } set additionalProperties(additionalProperties: Map | undefined) { this._additionalProperties = additionalProperties; } @@ -114,10 +117,13 @@ exports[`Marshalling preset should render un/marshal code 1`] = ` } json += \`\\"tupleTest\\": [\${serializedTuple.join(',')}],\`; } + if(this.constTest !== undefined) { + json += \`\\"constTest\\": \${typeof this.constTest === 'number' || typeof this.constTest === 'boolean' ? this.constTest : JSON.stringify(this.constTest)},\`; + } 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\\",\\"additionalProperties\\"].includes(String(key))) continue; + if([\\"string prop\\",\\"enumProp\\",\\"numberProp\\",\\"nestedObject\\",\\"unionTest\\",\\"unionArrayTest\\",\\"arrayTest\\",\\"tupleTest\\",\\"constTest\\",\\"additionalProperties\\"].includes(String(key))) continue; if(value instanceof NestedTest) { json += \`\\"\${key}\\": \${value.marshal()},\`; } else { @@ -157,9 +163,10 @@ exports[`Marshalling preset should render un/marshal code 1`] = ` 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\\",\\"additionalProperties\\"].includes(key);})); + const propsToCheck = Object.entries(obj).filter((([key,]) => {return ![\\"string prop\\",\\"enumProp\\",\\"numberProp\\",\\"nestedObject\\",\\"unionTest\\",\\"unionArrayTest\\",\\"arrayTest\\",\\"tupleTest\\",\\"constTest\\",\\"additionalProperties\\"].includes(key);})); for (const [key, value] of propsToCheck) { instance.additionalProperties.set(key, value as any); }