Skip to content

Commit

Permalink
feat(enums): custom enum key names via custom x-enumNames property
Browse files Browse the repository at this point in the history
  • Loading branch information
vmasek committed Jun 28, 2019
1 parent 092a8d5 commit 9cf1c8f
Show file tree
Hide file tree
Showing 6 changed files with 565 additions and 505 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,26 @@ StaticInjectorError(AppModule)[APIClient -> HttpClient]:
NullInjectorError: No provider for HttpClient!
```

Fix:
**Fix:**
- add `HttpClientModule` to your root module (see NgModule imports in [usage](https://github.com/flowup/api-client-generator#how-to-use-generated-client))

### Numeric Enums keys generated as plane number

If some of your numeric enums look like this, the problem might be that in the swagger file you are not describing the keys properly.

```
export enum MyEnum {
0 = 0,
1 = 1,
2 = 2,
}
```

**Fix**
We currently support two options:
- formatting description into array of `['1 Foo', '2 Bar']`
- using `'x-enumNames'` custom property that should be in format `['Foo', 'Bar']`

# Problem reporting and contributions

Please report any problems you have any issues you find so they can be resolved.
Expand Down
14 changes: 11 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type ExtendedParameter = (SwaggerParameter) & {
schema: Schema;
type: 'string' | 'integer';
required: boolean;
'x-enumNames'?: string[];
};

interface Definitions {
Expand Down Expand Up @@ -189,7 +190,13 @@ function parseDefinitions(

function defineEnumOrInterface(key: string, definition: Schema | ExtendedParameter): Definition {
return definition.enum && definition.enum.length !== 0
? defineEnum(definition.enum, key, definition.type === 'integer', definition.description)
? defineEnum(
definition.enum,
key,
definition.type === 'integer',
definition.description,
(definition as ExtendedParameter)['x-enumNames'],
)
: defineInterface(('schema' in definition ? definition.schema : definition) || {}, key);
}

Expand All @@ -198,6 +205,7 @@ function defineEnum(
definitionKey: string,
isNumeric: boolean = false,
enumDesc: string = '',
xEnumNames: string[] = [],
): Definition {
const splitDesc = enumDesc.split('\n');
const descKeys: { [key: string]: string } | null = splitDesc.length > 1
Expand All @@ -212,10 +220,10 @@ function defineEnum(

return {
name: typeName(definitionKey),
properties: enumSchema && enumSchema.map((val) => ({
properties: enumSchema && enumSchema.map((val, index) => ({
name: (
isNumeric
? descKeys ? descKeys[val.toString()] : val.toString()
? descKeys ? descKeys[val.toString()] : xEnumNames[index] || `${val}`
: val.toString()
).replace(/[\W\s]+/, '_'),
value: val.toString(),
Expand Down
1 change: 1 addition & 0 deletions tests/with-all-tags/api/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export { DummySelectorViewModel } from './dummy-selector-view-model.model';
export { DummyViewModel } from './dummy-view-model.model';
export { ProjectTypeViewModel } from './project-type-view-model.model';
export { RowModel } from './row-model.model';
export { StatusSeverity } from './status-severity.enum';
export { WidgetModel } from './widget-model.model';
export { WidgetTypeViewModel } from './widget-type-view-model.model';
9 changes: 9 additions & 0 deletions tests/with-all-tags/api/models/status-severity.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* tslint:disable */

export enum StatusSeverity {
Unknown = 0,
OK = 1,
Warning = 2,
Error = 3,
Critical = 4,
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* tslint:disable */
import {
StatusSeverity,
} from '.';

export interface WidgetTypeViewModel {
Beschreibung?: string;
Expand All @@ -9,6 +12,7 @@ export interface WidgetTypeViewModel {
Order?: number;
OrganizerTaskId?: number;
RefSystem?: { [key: string]: string };
Severity?: StatusSeverity;
SysApplicationId?: number;
Titel?: string;
}
Loading

0 comments on commit 9cf1c8f

Please sign in to comment.