Skip to content

Commit

Permalink
fix: enum keys cannot have a number as first character (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Sep 14, 2021
1 parent 571c538 commit a0d0adc
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/generators/csharp/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ export const CSHARP_DEFAULT_ENUM_PRESET: EnumPreset<EnumRenderer> = {
itemName = `Number_${itemName}`;
} else if (typeof item === 'object') {
itemName = `${JSON.stringify(item)}`;
}
} else if (!(/^[a-zA-Z]+$/).test(itemName.charAt(0))) {
itemName = `String_${itemName}`;
}

return pascalCase(itemName);
},
Expand Down
19 changes: 15 additions & 4 deletions src/generators/java/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,30 @@ ${this.indent(this.renderBlock(content, 2))}
}

normalizeKey(value: any): string {
let key;
switch (typeof value) {
case 'bigint':
case 'number': {
return FormatHelpers.toConstantCase(`number ${value}`);
key = `number_${value}`;
break;
}
case 'boolean': {
return FormatHelpers.toConstantCase(`boolean ${value}`);
key = `boolean_${value}`;
break;
}
case 'object': {
return FormatHelpers.toConstantCase(JSON.stringify(value));
key = JSON.stringify(value);
break;
}
default: return FormatHelpers.toConstantCase(String(value));
default: {
key = String(value);
//Ensure no special char can be the beginning letter
if (!(/^[a-zA-Z]+$/).test(key.charAt(0))) {
key = `string_${key}`;
}
}
}
return FormatHelpers.toConstantCase(key);
}

normalizeValue(value: any): string {
Expand Down
6 changes: 5 additions & 1 deletion src/generators/typescript/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ${this.indent(this.renderBlock(content, 2))}
switch (typeof value) {
case 'bigint':
case 'number': {
key = `number ${value}`;
key = `number_${value}`;
break;
}
case 'object': {
Expand All @@ -51,6 +51,10 @@ ${this.indent(this.renderBlock(content, 2))}
}
default: {
key = String(value);
//Ensure no special char can be the beginning letter
if (!(/^[a-zA-Z]+$/).test(key.charAt(0))) {
key = `String_${key}`;
}
}
}
return FormatHelpers.toConstantCase(key);
Expand Down
2 changes: 1 addition & 1 deletion test/generators/csharp/CSharpGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('CSharpGenerator', () => {
name: 'with enums of mixed types',
doc: {
$id: 'Things',
enum: ['Texas', 1, false, {test: 'test'}],
enum: ['Texas', '1', 1, false, {test: 'test'}],
}
},
])('should render `enum` type $name', ({doc}) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,15 @@ public static class StatesExtensions {
exports[`CSharpGenerator should render \`enum\` type $name should not be empty 3`] = `
"public enum Things {
Texas, Number_1, False, TestTest
Texas, String_1, Number_1, False, TestTest
}
public static class ThingsExtensions {
public static dynamic GetValue(this Things enumValue)
{
switch (enumValue)
{
case Things.Texas: return \\"Texas\\";
case Things.String_1: return \\"1\\";
case Things.Number_1: return 1;
case Things.False: return false;
case Things.TestTest: return \\"{\\\\\\"test\\\\\\":\\\\\\"test\\\\\\"}\\";
Expand All @@ -272,6 +273,7 @@ public static class ThingsExtensions {
switch (value)
{
case \\"Texas\\": return Things.Texas;
case \\"1\\": return Things.String_1;
case 1: return Things.Number_1;
case false: return Things.False;
case \\"{\\\\\\"test\\\\\\":\\\\\\"test\\\\\\"}\\": return Things.TestTest;
Expand All @@ -285,14 +287,15 @@ public static class ThingsExtensions {
exports[`CSharpGenerator should render \`enum\` type $name should not be empty 4`] = `
"public enum Things {
Texas, Number_1, False, TestTest
Texas, String_1, Number_1, False, TestTest
}
public static class ThingsExtensions {
public static dynamic GetValue(this Things enumValue)
{
switch (enumValue)
{
case Things.Texas: return \\"Texas\\";
case Things.String_1: return \\"1\\";
case Things.Number_1: return 1;
case Things.False: return false;
case Things.TestTest: return \\"{\\\\\\"test\\\\\\":\\\\\\"test\\\\\\"}\\";
Expand All @@ -305,6 +308,7 @@ public static class ThingsExtensions {
switch (value)
{
case \\"Texas\\": return Things.Texas;
case \\"1\\": return Things.String_1;
case 1: return Things.Number_1;
case false: return Things.False;
case \\"{\\\\\\"test\\\\\\":\\\\\\"test\\\\\\"}\\": return Things.TestTest;
Expand Down
2 changes: 1 addition & 1 deletion test/generators/go/GoGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type States string`,
name: 'with enums of mixed types',
doc: {
$id: 'Things',
enum: ['Texas', 1, false, {test: 'test'}],
enum: ['Texas', 1, '1', false, {test: 'test'}],
},
expected: `// Things represents an enum of mixed types.
type Things interface{}`,
Expand Down
4 changes: 2 additions & 2 deletions test/generators/java/JavaGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ describe('JavaGenerator', () => {
const doc = {
$id: 'Union',
type: ['string', 'integer', 'boolean'],
enum: ['Texas', 'Alabama', 0, 1, true, {test: 'test'}],
enum: ['Texas', 'Alabama', 0, 1, '1', true, {test: 'test'}],
};
const expected = `public enum Union {
TEXAS("Texas"), ALABAMA("Alabama"), NUMBER_0(0), NUMBER_1(1), BOOLEAN_TRUE(true), TEST_TEST("{\\"test\\":\\"test\\"}");
TEXAS("Texas"), ALABAMA("Alabama"), NUMBER_0(0), NUMBER_1(1), STRING_1("1"), BOOLEAN_TRUE(true), TEST_TEST("{\\"test\\":\\"test\\"}");
private Object value;
Expand Down
3 changes: 2 additions & 1 deletion test/generators/typescript/TypeScriptGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,11 @@ ${content}`;
test('should render union `enum` values', async () => {
const doc = {
$id: 'States',
enum: [2, 'test', true, {test: 'test'}]
enum: [2, '2', 'test', true, {test: 'test'}]
};
const expected = `export enum States {
NUMBER_2 = 2,
STRING_2 = "2",
TEST = "test",
TRUE = "true",
TEST_TEST = '{"test":"test"}',
Expand Down

0 comments on commit a0d0adc

Please sign in to comment.