openapi-ts-generator 9.28.4 → 9.31.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@ import { IImportType } from './template-data';
4
4
  import { IEnumValue } from './enum-value';
5
5
  export interface IEntity {
6
6
  isEnum?: boolean;
7
+ isCharEnum: boolean;
7
8
  enumValues: (string | IEnumValue)[];
8
9
  name: string;
9
10
  kebabCasedName: string;
@@ -1,5 +1,5 @@
1
1
  export interface IEnumValue {
2
- key?: number;
2
+ key?: number | string;
3
3
  name: string;
4
4
  titleName: string;
5
5
  snakeCaseName: string;
@@ -7,6 +7,7 @@ export declare class SchemaWrapperInfo {
7
7
  propertySchemaObject: SchemaObject;
8
8
  propertyReferenceObject: ReferenceObject;
9
9
  isEnum?: boolean;
10
+ isCharEnum?: boolean;
10
11
  readonly enumValues: (string | IEnumValue)[];
11
12
  readonly componentSchemaObject: SchemaObject;
12
13
  readonly valueProperties: IValueProperty[];
@@ -6,6 +6,7 @@ class SchemaWrapperInfo {
6
6
  constructor(schemaItem) {
7
7
  this.propertySchemaObject = {};
8
8
  this.propertyReferenceObject = { $ref: '' };
9
+ this.isCharEnum = false;
9
10
  this.componentSchemaObject = schemaItem;
10
11
  this.description = schemaItem.description;
11
12
  this.valueProperties = [];
@@ -48,11 +48,12 @@ class OpenApiDocConverter {
48
48
  schemaWrapperInfo.updateReferenceProperties(this.options);
49
49
  const entity = {
50
50
  isEnum: schemaWrapperInfo.isEnum,
51
+ isCharEnum: schemaWrapperInfo.isCharEnum || false,
51
52
  enumValues: schemaWrapperInfo.enumValues.map((t) => typeof t === 'string' || t instanceof String
52
53
  ? t
53
54
  : {
54
55
  ...t,
55
- key: t.key ?? 0,
56
+ key: schemaWrapperInfo.isCharEnum ? t.key : +t.key,
56
57
  }),
57
58
  name: schemaName,
58
59
  kebabCasedName: (0, lodash_1.kebabCase)(schemaName),
@@ -70,16 +71,28 @@ class OpenApiDocConverter {
70
71
  }
71
72
  buildSchemaWrapperInfoForEnum(schemaWrapperInfo) {
72
73
  schemaWrapperInfo.isEnum = true;
73
- schemaWrapperInfo.enumValues.push(...(schemaWrapperInfo.componentSchemaObject.enum || []).map((x) => {
74
- const key = this.startNumberregex.exec(x)?.at(0);
75
- const name = this.endAlphaNumRegex.exec(x)?.at(0) ?? '';
76
- return {
77
- key: key ? +key : 0,
78
- name,
79
- titleName: (0, lodash_1.startCase)(name),
80
- snakeCaseName: (0, lodash_1.snakeCase)(name).toUpperCase(),
81
- };
82
- }));
74
+ let enumValues = [...(schemaWrapperInfo.componentSchemaObject.enum || []).map((x) => {
75
+ const key = this.startNumberregex.exec(x)?.at(0);
76
+ const name = this.endAlphaNumRegex.exec(x)?.at(0) ?? '';
77
+ return {
78
+ key: key ? +key : 0,
79
+ name,
80
+ titleName: (0, lodash_1.startCase)(name),
81
+ snakeCaseName: (0, lodash_1.snakeCase)(name).toUpperCase(),
82
+ };
83
+ })];
84
+ schemaWrapperInfo.isCharEnum = enumValues.length > 0 &&
85
+ enumValues
86
+ .filter((enumVal) => typeof enumVal !== 'string' && typeof enumVal.key === 'number')
87
+ .map((enumVal) => enumVal.key)
88
+ .every((val) => val >= 65 && val <= 90); // A-Z ASCII range
89
+ if (schemaWrapperInfo.isCharEnum) {
90
+ enumValues = enumValues.map(enumvalue => ({
91
+ ...enumvalue,
92
+ key: String.fromCharCode(enumvalue.key)
93
+ }));
94
+ }
95
+ schemaWrapperInfo.enumValues.push(...enumValues);
83
96
  }
84
97
  buildSchemaWrapperInfo(parentTypeName, schemaWrapperInfo) {
85
98
  for (const propertyName in schemaWrapperInfo.componentSchemaObject.properties) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-generator",
3
- "version": "9.28.4",
3
+ "version": "9.31.2",
4
4
  "description": "Based on swagger-ts-generator, this is a type script model generator specifically for services with OpenApi spec documentation.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -11,12 +11,21 @@ import { IEnumValue } from 'openapi-ts-generator/enums';
11
11
  * {{description}}
12
12
  */
13
13
  {{/if}}export enum {{{name}}} {
14
+ {{#if isCharEnum}}
15
+ {{#enumValues}}{{#if description}}/**
16
+ * {{description}}
17
+ */
18
+ {{/if}}
19
+ {{name}} = '{{key}}',
20
+ {{/enumValues}}
21
+ {{else}}
14
22
  {{#enumValues}}{{#if description}}/**
15
23
  * {{description}}
16
24
  */
17
25
  {{/if}}
18
26
  {{name}} = {{key}},
19
27
  {{/enumValues}}
28
+ {{/if}}
20
29
  }
21
30
 
22
31
  {{#if description}}/**
@@ -31,8 +40,15 @@ import { IEnumValue } from 'openapi-ts-generator/enums';
31
40
  {{/enumValues}}
32
41
  }
33
42
 
43
+ {{#if isCharEnum}}
44
+ export const {{{camelSingularName}}}Values : IEnumValue[] = [
45
+ {{#enumValues}}
46
+ { key: '{{key}}', name: '{{name}}', displayText: '{{titleName}}'},
47
+ {{/enumValues}}
48
+ {{else}}
34
49
  export const {{{camelSingularName}}}Values : IEnumValue[] = [
35
50
  {{#enumValues}}
36
51
  { key: {{key}}, name: '{{name}}', displayText: '{{titleName}}'},
37
52
  {{/enumValues}}
53
+ {{/if}}
38
54
  ];
@@ -11,7 +11,7 @@ import { {{name}}Properties } from './{{kebabCasedName}}.properties';
11
11
 
12
12
  export function createTest{{name}}() {
13
13
  return { {{#valueProperties}}
14
- [{{../name}}Properties.{{snakeCaseName}}]: {{{initialTestValue}}},{{/valueProperties}} {{#referenceProperties}}
14
+ [{{../name}}Properties.{{snakeCaseName}}]: {{{initialTestValue}}},{{/valueProperties}}{{#referenceProperties}}
15
15
  [{{../name}}Properties.{{snakeCaseName}}]: {{{initialTestValue}}},{{/referenceProperties}}
16
16
  };
17
17
  }