Skip to content

Commit 0e80a4c

Browse files
author
Luis Fernando Planella Gonzalez
committed
Allow generating an array with all enum values
Fixes #303
1 parent f367cd5 commit 0e80a4c

7 files changed

+40
-8
lines changed

lib/model.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { OpenAPIObject, SchemaObject } from 'openapi3-ts';
22
import { EnumValue } from './enum-value';
33
import { GenType } from './gen-type';
4-
import { tsComments, tsType, unqualifiedName } from './gen-utils';
4+
import { fileName, tsComments, tsType, unqualifiedName } from './gen-utils';
55
import { Options } from './options';
66
import { Property } from './property';
7+
import { upperCase } from 'lodash';
78

89

910
/**
@@ -19,6 +20,8 @@ export class Model extends GenType {
1920
// Simple properties
2021
simpleType: string;
2122
enumValues: EnumValue[];
23+
enumArrayName?: string;
24+
enumArrayFileName?: string;
2225

2326
// Array properties
2427
elementType: string;
@@ -35,8 +38,11 @@ export class Model extends GenType {
3538

3639
const type = schema.type || 'any';
3740

38-
// When enumStyle is 'alias' it is handled as a simple type.
39-
if (options.enumStyle !== 'alias' && (schema.enum || []).length > 0 && ['string', 'number', 'integer'].includes(type)) {
41+
// Handle enums
42+
if ((schema.enum || []).length > 0 && ['string', 'number', 'integer'].includes(type)) {
43+
this.enumArrayName = upperCase(this.typeName).replace(/\s+/g, '_');
44+
this.enumArrayFileName = fileName(this.typeName + '-array');
45+
4046
const names = schema['x-enumNames'] as string[] || [];
4147
const descriptions = schema['x-enumDescriptions'] as string[] || [];
4248
const values = schema.enum || [];
@@ -45,12 +51,14 @@ export class Model extends GenType {
4551
const enumValue = new EnumValue(type, names[i], descriptions[i], values[i], options);
4652
this.enumValues.push(enumValue);
4753
}
54+
55+
// When enumStyle is 'alias' it is handled as a simple type.
56+
this.isEnum = options.enumStyle !== 'alias';
4857
}
4958

5059
const hasAllOf = schema.allOf && schema.allOf.length > 0;
5160
const hasOneOf = schema.oneOf && schema.oneOf.length > 0;
5261
this.isObject = (type === 'object' || !!schema.properties) && !schema.nullable && !hasAllOf && !hasOneOf;
53-
this.isEnum = (this.enumValues || []).length > 0;
5462
this.isSimple = !this.isObject && !this.isEnum;
5563

5664
if (this.isObject) {

lib/ng-openapi-gen.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ export class NgOpenApiGen {
7878
const models = [...this.models.values()];
7979
for (const model of models) {
8080
this.write('model', model, model.fileName, 'models');
81+
if (this.options.enumArray && model.enumArrayFileName) {
82+
this.write('enumArray', model, model.enumArrayFileName, 'models');
83+
}
8184
}
8285

8386
// Generate each service and function
@@ -114,7 +117,6 @@ export class NgOpenApiGen {
114117
this.write('module', general, this.globals.moduleFile);
115118
}
116119

117-
118120
const modelIndex = this.globals.modelIndexFile || this.options.indexFile ? new ModelIndex(models, this.options) : null;
119121
if (this.globals.modelIndexFile) {
120122
this.write('modelIndex', { ...general, modelIndex }, this.globals.modelIndexFile);

lib/options.ts

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ export interface Options {
8383
*/
8484
enumStyle?: 'alias' | 'upper' | 'pascal' | 'ignorecase';
8585

86+
/**
87+
* Should an array with all enum items of models be exported in a sibling file for enums?
88+
*/
89+
enumArray?: boolean;
90+
8691
/**
8792
* Determines how to normalize line endings. Possible values are:
8893
*

ng-openapi-gen-schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@
148148
"ignorecase"
149149
]
150150
},
151+
"enumArray": {
152+
"description": "Should an array be exported for each model, in a file sibling to the enum definition?",
153+
"default": false,
154+
"type": "boolean"
155+
},
151156
"endOfLineStyle": {
152157
"description": "Determines how to normalize line endings. Possible values are: `crlf`: normalize line endings to CRLF (Windows, DOS) => \\r\\n. `lf` normalize line endings to LF (Unix, OS X) => \\n. `cr` normalize line endings to CR (Mac OS) => \\r. `auto` normalize line endings for the current operating system. Defaults to 'auto'.",
153158
"default": "auto",

templates/enum.handlebars

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export enum {{typeName}} {
2-
{{#enumValues}} {{name}} = {{{value}}}{{^@last}},
1+
export enum {{{typeName}}} {
2+
{{#enumValues}} {{{name}}} = {{{value}}}{{^@last}},
33
{{/@last}}{{/enumValues}}
44
}

templates/enumArray.handlebars

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
import { {{{typeName}}} } from './{{{fileName}}}';
4+
5+
/**
6+
* Each possible value of `{{{typeName}}}`
7+
*/
8+
export const {{{enumArrayName}}}: {{{typeName}}}[] = [
9+
{{#enumValues}} {{{value}}}{{^@last}},
10+
{{/@last}}{{/enumValues}}
11+
];

test/all-types.config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"$schema": "../ng-openapi-gen-schema.json",
33
"input": "all-types.json",
44
"output": "out/all-types/",
5-
"indexFile": true
5+
"indexFile": true,
6+
"enumArray": true
67
}

0 commit comments

Comments
 (0)