Skip to content

Commit

Permalink
Allow generating an array with all enum values
Browse files Browse the repository at this point in the history
Fixes #303
  • Loading branch information
Luis Fernando Planella Gonzalez committed Nov 17, 2023
1 parent f367cd5 commit 0e80a4c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 8 deletions.
16 changes: 12 additions & 4 deletions lib/model.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { OpenAPIObject, SchemaObject } from 'openapi3-ts';
import { EnumValue } from './enum-value';
import { GenType } from './gen-type';
import { tsComments, tsType, unqualifiedName } from './gen-utils';
import { fileName, tsComments, tsType, unqualifiedName } from './gen-utils';
import { Options } from './options';
import { Property } from './property';
import { upperCase } from 'lodash';


/**
Expand All @@ -19,6 +20,8 @@ export class Model extends GenType {
// Simple properties
simpleType: string;
enumValues: EnumValue[];
enumArrayName?: string;
enumArrayFileName?: string;

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

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

// When enumStyle is 'alias' it is handled as a simple type.
if (options.enumStyle !== 'alias' && (schema.enum || []).length > 0 && ['string', 'number', 'integer'].includes(type)) {
// Handle enums
if ((schema.enum || []).length > 0 && ['string', 'number', 'integer'].includes(type)) {
this.enumArrayName = upperCase(this.typeName).replace(/\s+/g, '_');
this.enumArrayFileName = fileName(this.typeName + '-array');

const names = schema['x-enumNames'] as string[] || [];
const descriptions = schema['x-enumDescriptions'] as string[] || [];
const values = schema.enum || [];
Expand All @@ -45,12 +51,14 @@ export class Model extends GenType {
const enumValue = new EnumValue(type, names[i], descriptions[i], values[i], options);
this.enumValues.push(enumValue);
}

// When enumStyle is 'alias' it is handled as a simple type.
this.isEnum = options.enumStyle !== 'alias';
}

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

if (this.isObject) {
Expand Down
4 changes: 3 additions & 1 deletion lib/ng-openapi-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export class NgOpenApiGen {
const models = [...this.models.values()];
for (const model of models) {
this.write('model', model, model.fileName, 'models');
if (this.options.enumArray && model.enumArrayFileName) {
this.write('enumArray', model, model.enumArrayFileName, 'models');
}
}

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


const modelIndex = this.globals.modelIndexFile || this.options.indexFile ? new ModelIndex(models, this.options) : null;
if (this.globals.modelIndexFile) {
this.write('modelIndex', { ...general, modelIndex }, this.globals.modelIndexFile);
Expand Down
5 changes: 5 additions & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export interface Options {
*/
enumStyle?: 'alias' | 'upper' | 'pascal' | 'ignorecase';

/**
* Should an array with all enum items of models be exported in a sibling file for enums?
*/
enumArray?: boolean;

/**
* Determines how to normalize line endings. Possible values are:
*
Expand Down
5 changes: 5 additions & 0 deletions ng-openapi-gen-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@
"ignorecase"
]
},
"enumArray": {
"description": "Should an array be exported for each model, in a file sibling to the enum definition?",
"default": false,
"type": "boolean"
},
"endOfLineStyle": {
"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'.",
"default": "auto",
Expand Down
4 changes: 2 additions & 2 deletions templates/enum.handlebars
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum {{typeName}} {
{{#enumValues}} {{name}} = {{{value}}}{{^@last}},
export enum {{{typeName}}} {
{{#enumValues}} {{{name}}} = {{{value}}}{{^@last}},
{{/@last}}{{/enumValues}}
}
11 changes: 11 additions & 0 deletions templates/enumArray.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* tslint:disable */
/* eslint-disable */
import { {{{typeName}}} } from './{{{fileName}}}';

/**
* Each possible value of `{{{typeName}}}`
*/
export const {{{enumArrayName}}}: {{{typeName}}}[] = [
{{#enumValues}} {{{value}}}{{^@last}},
{{/@last}}{{/enumValues}}
];
3 changes: 2 additions & 1 deletion test/all-types.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"$schema": "../ng-openapi-gen-schema.json",
"input": "all-types.json",
"output": "out/all-types/",
"indexFile": true
"indexFile": true,
"enumArray": true
}

0 comments on commit 0e80a4c

Please sign in to comment.