Skip to content

Commit

Permalink
Merge pull request #220 from hey-api/fix/enum-duplicate-export-2
Browse files Browse the repository at this point in the history
fix(enum): append index number on duplicate name
  • Loading branch information
mrlubos authored Apr 3, 2024
2 parents 60f1f73 + 01c1ac3 commit 441a636
Show file tree
Hide file tree
Showing 27 changed files with 187 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-mirrors-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix(enum): append index number on duplicate name
8 changes: 7 additions & 1 deletion packages/openapi-ts/src/openApi/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ export const parse = (openApi: OpenApi, options: Config): Client => {
const models = getModels(openApi);
const services = getServices(openApi, options);

return { models, server, services, version };
return {
enumNames: [],
models,
server,
services,
version,
};
};
1 change: 1 addition & 0 deletions packages/openapi-ts/src/openApi/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const parse = (openApi: OpenApi, options: Config): Client => {
const services = getServices(openApi, options);

return {
enumNames: [],
models,
server,
services,
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-ts/src/templates/partials/exportEnum.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type {{{name}}} = {{{enumUnionType enum}}};
{{/if}}

{{#equals @root.$config.enums 'javascript'}}
export const {{{enumName name }}} = {
export const {{{enumName name}}} = {
{{#each enum}}
{{#if x-enum-description}}
/**
Expand Down
2 changes: 2 additions & 0 deletions packages/openapi-ts/src/types/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Model, Service } from '../openApi';

export interface Client {
enumNames: string[];
models: Model[];
server: string;
services: Service[];
Expand Down
2 changes: 2 additions & 0 deletions packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('registerHandlebarHelpers', () => {
write: true,
},
{
enumNames: [],
models: [],
server: '',
services: [],
Expand Down Expand Up @@ -82,6 +83,7 @@ describe('registerHandlebarTemplates', () => {
write: true,
},
{
enumNames: [],
models: [],
server: '',
services: [],
Expand Down
18 changes: 15 additions & 3 deletions packages/openapi-ts/src/utils/enum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Enum } from '../openApi';
import type { Client } from '../types/client';
import type { Config } from '../types/config';
import { unescapeName } from './escapeName';
import { unique } from './unique';

Expand Down Expand Up @@ -41,12 +43,22 @@ export const enumKey = (value?: string | number, key?: string) => {
* already escaped, so we need to remove quotes around it.
* {@link https://github.com/ferdikoomen/openapi-typescript-codegen/issues/1969}
*/
export const enumName = (name?: string) => {
export const enumName = (config: Config, client: Client, name?: string) => {
if (!name) {
return name;
}
const escapedName = unescapeName(name).replace(/-([a-z])/gi, ($0, $1: string) => $1.toLocaleUpperCase());
return `${escapedName.charAt(0).toLocaleUpperCase()}${escapedName.slice(1)}Enum`;
const escapedName = unescapeName(name).replace(/[-_]([a-z])/gi, ($0, $1: string) => $1.toLocaleUpperCase());
let result = `${escapedName.charAt(0).toLocaleUpperCase() + escapedName.slice(1)}Enum`;
let index = 1;
while (client.enumNames.includes(result)) {
if (result.endsWith(index.toString())) {
result = result.slice(0, result.length - index.toString().length);
}
index += 1;
result = result + index.toString();
}
client.enumNames = [...client.enumNames, result];
return result;
};

export const enumUnionType = (enums: Enum[]) =>
Expand Down
7 changes: 5 additions & 2 deletions packages/openapi-ts/src/utils/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ const operationDataType = (config: Config, service: Service) => {
return output.join('\n');
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const registerHandlebarHelpers = (config: Config, client: Client): void => {
Handlebars.registerHelper('camelCase', camelCase);

Expand All @@ -193,7 +192,11 @@ export const registerHandlebarHelpers = (config: Config, client: Client): void =
});

Handlebars.registerHelper('enumKey', enumKey);
Handlebars.registerHelper('enumName', enumName);

Handlebars.registerHelper('enumName', function (name: string | undefined) {
return enumName(config, client, name);
});

Handlebars.registerHelper('enumUnionType', enumUnionType);
Handlebars.registerHelper('enumValue', enumValue);

Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/utils/postprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { unique } from './unique';
export function postProcessClient(client: Client): Client {
return {
...client,
enumNames: [],
models: client.models.map(model => postProcessModel(model)),
services: client.services.map(service => postProcessService(service)),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ vi.mock('node:fs');
describe('writeClientClass', () => {
it('should write to filesystem', async () => {
const client: Parameters<typeof writeClientClass>[0] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
services: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ vi.mock('node:fs');
describe('writeClient', () => {
it('should write to filesystem', async () => {
const client: Parameters<typeof writeClient>[0] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
services: [],
Expand Down
3 changes: 3 additions & 0 deletions packages/openapi-ts/src/utils/write/__tests__/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('writeClientCore', () => {

it('should write to filesystem', async () => {
const client: Parameters<typeof writeClientCore>[0] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
services: [],
Expand Down Expand Up @@ -58,6 +59,7 @@ describe('writeClientCore', () => {

it('uses client server value for base', async () => {
const client: Parameters<typeof writeClientCore>[0] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
services: [],
Expand Down Expand Up @@ -99,6 +101,7 @@ describe('writeClientCore', () => {

it('uses custom value for base', async () => {
const client: Parameters<typeof writeClientCore>[0] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
services: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ vi.mock('node:fs');
describe('writeClientIndex', () => {
it('should write to filesystem', async () => {
const client: Parameters<typeof writeClientIndex>[0] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
services: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ vi.mock('node:fs');
describe('writeClientModels', () => {
it('should write to filesystem', async () => {
const client: Parameters<typeof writeClientModels>[0] = {
enumNames: [],
models: [
{
$refs: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ vi.mock('node:fs');
describe('writeClientSchemas', () => {
it('should write to filesystem', async () => {
const client: Parameters<typeof writeClientSchemas>[0] = {
enumNames: [],
models: [
{
$refs: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ vi.mock('node:fs');
describe('writeClientServices', () => {
it('should write to filesystem', async () => {
const client: Parameters<typeof writeClientServices>[0] = {
enumNames: [],
models: [],
server: 'http://localhost:8080',
services: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,27 @@ export type ModelWithNullableString = {
* This is a simple string property
*/
nullableRequiredProp2: string | null;
/**
* This is a simple enum with strings
*/
'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串';
};

export const FooBarEnumEnum = {
SUCCESS: 'Success',
WARNING: 'Warning',
ERROR: 'Error',
ØÆÅ字符串: 'ØÆÅ字符串',
} as const;

/**
* This is a model with one enum
*/
export type ModelWithEnum = {
/**
* This is a simple enum with strings
*/
test?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串';
'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串';
/**
* These are the HTTP error code enums
*/
Expand All @@ -308,7 +319,7 @@ export type ModelWithEnum = {
bool?: boolean;
};

export const TestEnum = {
export const FooBarEnumEnum2 = {
SUCCESS: 'Success',
WARNING: 'Warning',
ERROR: 'Error',
Expand Down Expand Up @@ -353,8 +364,19 @@ export type ModelWithNestedEnums = {
dictionaryWithEnumFromDescription?: Record<string, number>;
arrayWithEnum?: Array<'Success' | 'Warning' | 'Error'>;
arrayWithDescription?: Array<number>;
/**
* This is a simple enum with strings
*/
'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串';
};

export const FooBarEnumEnum3 = {
SUCCESS: 'Success',
WARNING: 'Warning',
ERROR: 'Error',
ØÆÅ字符串: 'ØÆÅ字符串',
} as const;

/**
* This is a model with one property containing a reference
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,17 @@ export const $ModelWithNullableString = {
isRequired: true,
isNullable: true,
},
'foo_bar-enum': {
type: 'Enum',
enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'],
},
},
} as const;

export const $ModelWithEnum = {
description: `This is a model with one enum`,
properties: {
test: {
'foo_bar-enum': {
type: 'Enum',
enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'],
},
Expand Down Expand Up @@ -392,6 +396,10 @@ export const $ModelWithNestedEnums = {
description: `Success=1,Warning=2,Error=3`,
},
},
'foo_bar-enum': {
type: 'Enum',
enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'],
},
},
} as const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ export type ModelWithNullableString = {
* This is a simple string property
*/
nullableRequiredProp2: string | null;
/**
* This is a simple enum with strings
*/
'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串';
};

/**
Expand All @@ -247,7 +251,7 @@ export type ModelWithEnum = {
/**
* This is a simple enum with strings
*/
test?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串';
'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串';
/**
* These are the HTTP error code enums
*/
Expand Down Expand Up @@ -283,6 +287,10 @@ export type ModelWithNestedEnums = {
dictionaryWithEnumFromDescription?: Record<string, number>;
arrayWithEnum?: Array<'Success' | 'Warning' | 'Error'>;
arrayWithDescription?: Array<number>;
/**
* This is a simple enum with strings
*/
'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串';
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,17 @@ export const $ModelWithNullableString = {
isRequired: true,
isNullable: true,
},
'foo_bar-enum': {
type: 'Enum',
enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'],
},
},
} as const;

export const $ModelWithEnum = {
description: `This is a model with one enum`,
properties: {
test: {
'foo_bar-enum': {
type: 'Enum',
enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'],
},
Expand Down Expand Up @@ -392,6 +396,10 @@ export const $ModelWithNestedEnums = {
description: `Success=1,Warning=2,Error=3`,
},
},
'foo_bar-enum': {
type: 'Enum',
enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'],
},
},
} as const;

Expand Down
Loading

0 comments on commit 441a636

Please sign in to comment.