Skip to content

Commit

Permalink
fix: dont render unmarshal for property with const (#1799)
Browse files Browse the repository at this point in the history
Co-authored-by: Samriddhi <[email protected]>%0ACo-authored-by: Jonas Lagoni <[email protected]>
  • Loading branch information
kennethaasan and jonaslagoni authored Feb 15, 2024
1 parent 4da6cf8 commit 0a786ca
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/generators/typescript/presets/utils/UnmarshalFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions test/generators/typescript/preset/MarshallingPreset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const doc = {
type: 'string'
}
]
},
constTest: {
type: 'string',
const: 'TEST'
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, NestedTest | string>;
constructor(input: {
Expand Down Expand Up @@ -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<string, NestedTest | string> | undefined { return this._additionalProperties; }
set additionalProperties(additionalProperties: Map<string, NestedTest | string> | undefined) { this._additionalProperties = additionalProperties; }
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 0a786ca

Please sign in to comment.