Skip to content

Commit 105b312

Browse files
committed
Generate a valid identifier for integer enums
Fixes #40
1 parent eb01f79 commit 105b312

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

lib/gen-utils.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,16 @@ export function typeName(name: string): string {
6868
* Returns the name of the enum constant for a given value
6969
*/
7070
export function enumName(value: string, options: Options): string {
71-
const name = toBasicChars(value, true);
71+
let name = toBasicChars(value, true);
7272
if (options.enumStyle === 'upper') {
73-
return upperCase(name).replace(/\s+/g, '_');
73+
name = upperCase(name).replace(/\s+/g, '_');
7474
} else {
75-
return upperFirst(camelCase(name));
75+
name = upperFirst(camelCase(name));
7676
}
77+
if (/^\d/.test(name)) {
78+
name = '$' + name;
79+
}
80+
return name;
7781
}
7882

7983
/**

test/all-types.json

+27
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@
9494
"valueC"
9595
]
9696
},
97+
"RefIntEnum": {
98+
"type": "integer",
99+
"enum": [
100+
100,
101+
200,
102+
300
103+
]
104+
},
97105
"a.b.RefObject": {
98106
"type": "object",
99107
"required": [
@@ -125,6 +133,9 @@
125133
{
126134
"$ref": "#/components/schemas/RefEnum"
127135
},
136+
{
137+
"$ref": "#/components/schemas/RefIntEnum"
138+
},
128139
{
129140
"$ref": "#/components/schemas/Container"
130141
}
@@ -324,6 +335,22 @@
324335
"$ref": "#/components/schemas/x.y.RefObject"
325336
}
326337
},
338+
"stringEnumProp": {
339+
"type": "string",
340+
"enum": [
341+
"a",
342+
"b",
343+
"c"
344+
]
345+
},
346+
"intEnumProp": {
347+
"type": "integer",
348+
"enum": [
349+
1,
350+
2,
351+
3
352+
]
353+
},
327354
"nestedObject": {
328355
"type": "object",
329356
"properties": {

test/all-types.spec.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ it('Api', done => {
3232

3333
describe('Generation tests using all-types.json', () => {
3434
it('RefEnum model', done => {
35-
const refString = gen.models.get('RefEnum');
36-
const ts = gen.templates.apply('model', refString);
35+
const ref = gen.models.get('RefEnum');
36+
const ts = gen.templates.apply('model', ref);
3737
const parser = new TypescriptParser();
3838
parser.parseSource(ts).then(ast => {
3939
expect(ast.imports.length).toBe(0);
@@ -49,6 +49,24 @@ describe('Generation tests using all-types.json', () => {
4949
});
5050
});
5151

52+
it('RefIntEnum model', done => {
53+
const ref = gen.models.get('RefIntEnum');
54+
const ts = gen.templates.apply('model', ref);
55+
const parser = new TypescriptParser();
56+
parser.parseSource(ts).then(ast => {
57+
expect(ast.imports.length).toBe(0);
58+
expect(ast.declarations.length).toBe(1);
59+
expect(ast.declarations[0]).toEqual(jasmine.any(EnumDeclaration));
60+
const decl = ast.declarations[0] as EnumDeclaration;
61+
expect(decl.name).toBe('RefIntEnum');
62+
expect(decl.members.length).toBe(3);
63+
expect(decl.members[0]).toBe('$100');
64+
expect(decl.members[1]).toBe('$200');
65+
expect(decl.members[2]).toBe('$300');
66+
done();
67+
});
68+
});
69+
5270
it('a.b.RefObject model', done => {
5371
const refObject = gen.models.get('a.b.RefObject');
5472
const ts = gen.templates.apply('model', refObject);
@@ -96,7 +114,7 @@ describe('Generation tests using all-types.json', () => {
96114
const decl = ast.declarations[0] as TypeAliasDeclaration;
97115
expect(decl.name).toBe('Union');
98116
const text = ts.substring(decl.start || 0, decl.end || ts.length);
99-
expect(text).toBe('export type Union = { [key: string]: any } | RefEnum | Container;');
117+
expect(text).toBe('export type Union = { [key: string]: any } | RefEnum | RefIntEnum | Container;');
100118
done();
101119
});
102120
});
@@ -236,7 +254,7 @@ describe('Generation tests using all-types.json', () => {
236254
expect(ast.declarations[0]).toEqual(jasmine.any(InterfaceDeclaration));
237255
const decl = ast.declarations[0] as InterfaceDeclaration;
238256
expect(decl.name).toBe('Container');
239-
expect(decl.properties.length).toBe(19);
257+
expect(decl.properties.length).toBe(21);
240258

241259
// Assert the simple types
242260
function assertProperty(name: string, type: string, required = false) {
@@ -267,6 +285,8 @@ describe('Generation tests using all-types.json', () => {
267285
assertProperty('nestedObject', '{ \'p1\': string, \'p2\': number, ' +
268286
'\'deeper\': { \'d1\': ABRefObject, \'d2\': string | Array<ABRefObject> | number } }');
269287
assertProperty('dynamic', '{ [key: string]: XYRefObject }');
288+
assertProperty('stringEnumProp', '\'a\' | \'b\' | \'c\'');
289+
assertProperty('intEnumProp', '1 | 2 | 3');
270290

271291
done();
272292
});

0 commit comments

Comments
 (0)