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

feat: exclude underscore from TS key enum rendering #613

Merged
merged 5 commits into from
Feb 8, 2022
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/generators/typescript/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ${this.indent(this.renderBlock(content, 2))}
break;
}
default: {
key = FormatHelpers.replaceSpecialCharacters(String(value), { exclude: [' '], separator: '_' });
key = FormatHelpers.replaceSpecialCharacters(String(value), { exclude: [' ','_'], separator: '_' });
//Ensure no special char can be the beginning letter
if (!(/^[a-zA-Z]+$/).test(key.charAt(0))) {
key = `String_${key}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,59 +40,59 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
marshal(){
let json = '{'
if(this.stringProp !== undefined) {
json += `"string prop": ${typeof this.stringProp === 'number' || typeof this.stringProp === 'boolean' ? this.stringProp : JSON.stringify(this.stringProp)},`;
json += \`\\"string prop\\": \${typeof this.stringProp === 'number' || typeof this.stringProp === 'boolean' ? this.stringProp : JSON.stringify(this.stringProp)},\`;
}
if(this.numberProp !== undefined) {
json += `"numberProp": ${typeof this.numberProp === 'number' || typeof this.numberProp === 'boolean' ? this.numberProp : JSON.stringify(this.numberProp)},`;
json += \`\\"numberProp\\": \${typeof this.numberProp === 'number' || typeof this.numberProp === 'boolean' ? this.numberProp : JSON.stringify(this.numberProp)},\`;
}
if(this.objectProp !== undefined) {
json += `"objectProp": ${this.objectProp.marshal()},`;
json += \`\\"objectProp\\": \${this.objectProp.marshal()},\`;
}
if(this.sTestPatternProperties !== undefined) {
for (const [key, value] of this.sTestPatternProperties.entries()) {
//Only render pattern properties which are not already a property
if(Object.keys(this).includes(String(key))) continue;
json += `"${key}": ${typeof value === 'number' || typeof value === 'boolean' ? value : JSON.stringify(value)},`;
json += \`\\"\${key}\\": \${typeof value === 'number' || typeof value === 'boolean' ? value : JSON.stringify(value)},\`;
}
}if(this.sAnotherTestPatternProperties !== undefined) {
for (const [key, value] of this.sAnotherTestPatternProperties.entries()) {
//Only render pattern properties which are not already a property
if(Object.keys(this).includes(String(key))) continue;
json += `"${key}": ${value.marshal()},`;
json += \`\\"\${key}\\": \${value.marshal()},\`;
}
}
if(this.additionalProperties !== undefined) {
for (const [key, value] of this.additionalProperties.entries()) {
//Only render additionalProperties which are not already a property
if(Object.keys(this).includes(String(key))) continue;
json += `"${key}": ${value.marshal()},`;
json += \`\\"\${key}\\": \${value.marshal()},\`;
}
}

//Remove potential last comma
return `${json.charAt(json.length-1) === ',' ? json.slice(0, json.length-1) : json}}`;
return \`\${json.charAt(json.length-1) === ',' ? json.slice(0, json.length-1) : json}}\`;
}

unmarshal(json){
const obj = typeof json === "object" ? json : JSON.parse(json);
const obj = typeof json === \\"object\\" ? json : JSON.parse(json);
const instance = new Test({});

if (obj["string prop"] !== undefined) {
instance.stringProp = obj["string prop"];
if (obj[\\"string prop\\"] !== undefined) {
instance.stringProp = obj[\\"string prop\\"];
}
if (obj["numberProp"] !== undefined) {
instance.numberProp = obj["numberProp"];
if (obj[\\"numberProp\\"] !== undefined) {
instance.numberProp = obj[\\"numberProp\\"];
}
if (obj["objectProp"] !== undefined) {
instance.objectProp = NestedTest.unmarshal(obj["objectProp"]);
if (obj[\\"objectProp\\"] !== undefined) {
instance.objectProp = NestedTest.unmarshal(obj[\\"objectProp\\"]);
}

//Not part of core properties
if (instance.sTestPatternProperties === undefined) {instance.sTestPatternProperties = new Map();}
if (instance.sAnotherTestPatternProperties === undefined) {instance.sAnotherTestPatternProperties = new Map();}

if (instance.additionalProperties === undefined) {instance.additionalProperties = new Map();}
for (const [key, value] of Object.entries(obj).filter((([key,]) => {return !["string prop","numberProp","objectProp"].includes(key);}))) {
for (const [key, value] of Object.entries(obj).filter((([key,]) => {return ![\\"string prop\\",\\"numberProp\\",\\"objectProp\\"].includes(key);}))) {
//Check all pattern properties
if (key.match(new RegExp('^S(.?)test'))) {
instance.sTestPatternProperties.set(key, value);
Expand Down Expand Up @@ -129,33 +129,33 @@ exports[`Marshalling preset should render un/marshal code 2`] = `
marshal(){
let json = '{'
if(this.stringProp !== undefined) {
json += `"stringProp": ${typeof this.stringProp === 'number' || typeof this.stringProp === 'boolean' ? this.stringProp : JSON.stringify(this.stringProp)},`;
json += \`\\"stringProp\\": \${typeof this.stringProp === 'number' || typeof this.stringProp === 'boolean' ? this.stringProp : JSON.stringify(this.stringProp)},\`;
}

if(this.additionalProperties !== undefined) {
for (const [key, value] of this.additionalProperties.entries()) {
//Only render additionalProperties which are not already a property
if(Object.keys(this).includes(String(key))) continue;
json += `"${key}": ${typeof value === 'number' || typeof value === 'boolean' ? value : JSON.stringify(value)},`;
json += \`\\"\${key}\\": \${typeof value === 'number' || typeof value === 'boolean' ? value : JSON.stringify(value)},\`;
}
}

//Remove potential last comma
return `${json.charAt(json.length-1) === ',' ? json.slice(0, json.length-1) : json}}`;
return \`\${json.charAt(json.length-1) === ',' ? json.slice(0, json.length-1) : json}}\`;
}

unmarshal(json){
const obj = typeof json === "object" ? json : JSON.parse(json);
const obj = typeof json === \\"object\\" ? json : JSON.parse(json);
const instance = new NestedTest({});

if (obj["stringProp"] !== undefined) {
instance.stringProp = obj["stringProp"];
if (obj[\\"stringProp\\"] !== undefined) {
instance.stringProp = obj[\\"stringProp\\"];
}

//Not part of core properties

if (instance.additionalProperties === undefined) {instance.additionalProperties = new Map();}
for (const [key, value] of Object.entries(obj).filter((([key,]) => {return !["stringProp"].includes(key);}))) {
for (const [key, value] of Object.entries(obj).filter((([key,]) => {return ![\\"stringProp\\"].includes(key);}))) {

instance.additionalProperties.set(key, value);
}
Expand Down
21 changes: 21 additions & 0 deletions test/generators/typescript/renderers/EnumRenderer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { TypeScriptGenerator } from '../../../../src/generators';
import { EnumRenderer } from '../../../../src/generators/typescript/renderers/EnumRenderer';
import { CommonInputModel, CommonModel } from '../../../../src/models';

describe('EnumRenderer', () => {
let renderer: EnumRenderer;
beforeEach(() => {
renderer = new EnumRenderer(TypeScriptGenerator.defaultOptions, new TypeScriptGenerator(), [], new CommonModel(), new CommonInputModel());
});

describe('normalizeKey()', () => {
test('should correctly format " " to correct key', () => {
const key = renderer.normalizeKey('something something');
expect(key).toEqual('SOMETHING_SOMETHING');
});
test('should correctly format "_" to correct key', () => {
const key = renderer.normalizeKey('something_something');
expect(key).toEqual('SOMETHING_SOMETHING');
});
});
});