fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #220 from hey-api/fix/enum-duplicate-export-2

fix(enum): append index number on duplicate name

authored by

Lubos and committed by
GitHub
441a636d 60f1f737

+187 -20
+5
.changeset/tidy-mirrors-do.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + fix(enum): append index number on duplicate name
+7 -1
packages/openapi-ts/src/openApi/v2/index.ts
··· 18 18 const models = getModels(openApi); 19 19 const services = getServices(openApi, options); 20 20 21 - return { models, server, services, version }; 21 + return { 22 + enumNames: [], 23 + models, 24 + server, 25 + services, 26 + version, 27 + }; 22 28 };
+1
packages/openapi-ts/src/openApi/v3/index.ts
··· 19 19 const services = getServices(openApi, options); 20 20 21 21 return { 22 + enumNames: [], 22 23 models, 23 24 server, 24 25 services,
+1 -1
packages/openapi-ts/src/templates/partials/exportEnum.hbs
··· 30 30 {{/if}} 31 31 32 32 {{#equals @root.$config.enums 'javascript'}} 33 - export const {{{enumName name }}} = { 33 + export const {{{enumName name}}} = { 34 34 {{#each enum}} 35 35 {{#if x-enum-description}} 36 36 /**
+2
packages/openapi-ts/src/types/client.ts
··· 1 1 import { Model, Service } from '../openApi'; 2 + 2 3 export interface Client { 4 + enumNames: string[]; 3 5 models: Model[]; 4 6 server: string; 5 7 services: Service[];
+2
packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts
··· 28 28 write: true, 29 29 }, 30 30 { 31 + enumNames: [], 31 32 models: [], 32 33 server: '', 33 34 services: [], ··· 82 83 write: true, 83 84 }, 84 85 { 86 + enumNames: [], 85 87 models: [], 86 88 server: '', 87 89 services: [],
+15 -3
packages/openapi-ts/src/utils/enum.ts
··· 1 1 import type { Enum } from '../openApi'; 2 + import type { Client } from '../types/client'; 3 + import type { Config } from '../types/config'; 2 4 import { unescapeName } from './escapeName'; 3 5 import { unique } from './unique'; 4 6 ··· 41 43 * already escaped, so we need to remove quotes around it. 42 44 * {@link https://github.com/ferdikoomen/openapi-typescript-codegen/issues/1969} 43 45 */ 44 - export const enumName = (name?: string) => { 46 + export const enumName = (config: Config, client: Client, name?: string) => { 45 47 if (!name) { 46 48 return name; 47 49 } 48 - const escapedName = unescapeName(name).replace(/-([a-z])/gi, ($0, $1: string) => $1.toLocaleUpperCase()); 49 - return `${escapedName.charAt(0).toLocaleUpperCase()}${escapedName.slice(1)}Enum`; 50 + const escapedName = unescapeName(name).replace(/[-_]([a-z])/gi, ($0, $1: string) => $1.toLocaleUpperCase()); 51 + let result = `${escapedName.charAt(0).toLocaleUpperCase() + escapedName.slice(1)}Enum`; 52 + let index = 1; 53 + while (client.enumNames.includes(result)) { 54 + if (result.endsWith(index.toString())) { 55 + result = result.slice(0, result.length - index.toString().length); 56 + } 57 + index += 1; 58 + result = result + index.toString(); 59 + } 60 + client.enumNames = [...client.enumNames, result]; 61 + return result; 50 62 }; 51 63 52 64 export const enumUnionType = (enums: Enum[]) =>
+5 -2
packages/openapi-ts/src/utils/handlebars.ts
··· 184 184 return output.join('\n'); 185 185 }; 186 186 187 - // eslint-disable-next-line @typescript-eslint/no-unused-vars 188 187 export const registerHandlebarHelpers = (config: Config, client: Client): void => { 189 188 Handlebars.registerHelper('camelCase', camelCase); 190 189 ··· 193 192 }); 194 193 195 194 Handlebars.registerHelper('enumKey', enumKey); 196 - Handlebars.registerHelper('enumName', enumName); 195 + 196 + Handlebars.registerHelper('enumName', function (name: string | undefined) { 197 + return enumName(config, client, name); 198 + }); 199 + 197 200 Handlebars.registerHelper('enumUnionType', enumUnionType); 198 201 Handlebars.registerHelper('enumValue', enumValue); 199 202
+1
packages/openapi-ts/src/utils/postprocess.ts
··· 10 10 export function postProcessClient(client: Client): Client { 11 11 return { 12 12 ...client, 13 + enumNames: [], 13 14 models: client.models.map(model => postProcessModel(model)), 14 15 services: client.services.map(service => postProcessService(service)), 15 16 };
+1
packages/openapi-ts/src/utils/write/__tests__/class.spec.ts
··· 10 10 describe('writeClientClass', () => { 11 11 it('should write to filesystem', async () => { 12 12 const client: Parameters<typeof writeClientClass>[0] = { 13 + enumNames: [], 13 14 models: [], 14 15 server: 'http://localhost:8080', 15 16 services: [],
+1
packages/openapi-ts/src/utils/write/__tests__/client.spec.ts
··· 10 10 describe('writeClient', () => { 11 11 it('should write to filesystem', async () => { 12 12 const client: Parameters<typeof writeClient>[0] = { 13 + enumNames: [], 13 14 models: [], 14 15 server: 'http://localhost:8080', 15 16 services: [],
+3
packages/openapi-ts/src/utils/write/__tests__/core.spec.ts
··· 16 16 17 17 it('should write to filesystem', async () => { 18 18 const client: Parameters<typeof writeClientCore>[0] = { 19 + enumNames: [], 19 20 models: [], 20 21 server: 'http://localhost:8080', 21 22 services: [], ··· 58 59 59 60 it('uses client server value for base', async () => { 60 61 const client: Parameters<typeof writeClientCore>[0] = { 62 + enumNames: [], 61 63 models: [], 62 64 server: 'http://localhost:8080', 63 65 services: [], ··· 99 101 100 102 it('uses custom value for base', async () => { 101 103 const client: Parameters<typeof writeClientCore>[0] = { 104 + enumNames: [], 102 105 models: [], 103 106 server: 'http://localhost:8080', 104 107 services: [],
+1
packages/openapi-ts/src/utils/write/__tests__/index.spec.ts
··· 11 11 describe('writeClientIndex', () => { 12 12 it('should write to filesystem', async () => { 13 13 const client: Parameters<typeof writeClientIndex>[0] = { 14 + enumNames: [], 14 15 models: [], 15 16 server: 'http://localhost:8080', 16 17 services: [],
+1
packages/openapi-ts/src/utils/write/__tests__/models.spec.ts
··· 11 11 describe('writeClientModels', () => { 12 12 it('should write to filesystem', async () => { 13 13 const client: Parameters<typeof writeClientModels>[0] = { 14 + enumNames: [], 14 15 models: [ 15 16 { 16 17 $refs: [],
+1
packages/openapi-ts/src/utils/write/__tests__/schemas.spec.ts
··· 11 11 describe('writeClientSchemas', () => { 12 12 it('should write to filesystem', async () => { 13 13 const client: Parameters<typeof writeClientSchemas>[0] = { 14 + enumNames: [], 14 15 models: [ 15 16 { 16 17 $refs: [],
+1
packages/openapi-ts/src/utils/write/__tests__/services.spec.ts
··· 10 10 describe('writeClientServices', () => { 11 11 it('should write to filesystem', async () => { 12 12 const client: Parameters<typeof writeClientServices>[0] = { 13 + enumNames: [], 13 14 models: [], 14 15 server: 'http://localhost:8080', 15 16 services: [
+24 -2
packages/openapi-ts/test/__snapshots__/test/generated/v3/models.ts.snap
··· 288 288 * This is a simple string property 289 289 */ 290 290 nullableRequiredProp2: string | null; 291 + /** 292 + * This is a simple enum with strings 293 + */ 294 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 291 295 }; 292 296 297 + export const FooBarEnumEnum = { 298 + SUCCESS: 'Success', 299 + WARNING: 'Warning', 300 + ERROR: 'Error', 301 + ØÆÅ字符串: 'ØÆÅ字符串', 302 + } as const; 303 + 293 304 /** 294 305 * This is a model with one enum 295 306 */ ··· 297 308 /** 298 309 * This is a simple enum with strings 299 310 */ 300 - test?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 311 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 301 312 /** 302 313 * These are the HTTP error code enums 303 314 */ ··· 308 319 bool?: boolean; 309 320 }; 310 321 311 - export const TestEnum = { 322 + export const FooBarEnumEnum2 = { 312 323 SUCCESS: 'Success', 313 324 WARNING: 'Warning', 314 325 ERROR: 'Error', ··· 353 364 dictionaryWithEnumFromDescription?: Record<string, number>; 354 365 arrayWithEnum?: Array<'Success' | 'Warning' | 'Error'>; 355 366 arrayWithDescription?: Array<number>; 367 + /** 368 + * This is a simple enum with strings 369 + */ 370 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 356 371 }; 372 + 373 + export const FooBarEnumEnum3 = { 374 + SUCCESS: 'Success', 375 + WARNING: 'Warning', 376 + ERROR: 'Error', 377 + ØÆÅ字符串: 'ØÆÅ字符串', 378 + } as const; 357 379 358 380 /** 359 381 * This is a model with one property containing a reference
+9 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.ts.snap
··· 319 319 isRequired: true, 320 320 isNullable: true, 321 321 }, 322 + 'foo_bar-enum': { 323 + type: 'Enum', 324 + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 325 + }, 322 326 }, 323 327 } as const; 324 328 325 329 export const $ModelWithEnum = { 326 330 description: `This is a model with one enum`, 327 331 properties: { 328 - test: { 332 + 'foo_bar-enum': { 329 333 type: 'Enum', 330 334 enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 331 335 }, ··· 391 395 type: 'number', 392 396 description: `Success=1,Warning=2,Error=3`, 393 397 }, 398 + }, 399 + 'foo_bar-enum': { 400 + type: 'Enum', 401 + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 394 402 }, 395 403 }, 396 404 } as const;
+9 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/models.ts.snap
··· 238 238 * This is a simple string property 239 239 */ 240 240 nullableRequiredProp2: string | null; 241 + /** 242 + * This is a simple enum with strings 243 + */ 244 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 241 245 }; 242 246 243 247 /** ··· 247 251 /** 248 252 * This is a simple enum with strings 249 253 */ 250 - test?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 254 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 251 255 /** 252 256 * These are the HTTP error code enums 253 257 */ ··· 283 287 dictionaryWithEnumFromDescription?: Record<string, number>; 284 288 arrayWithEnum?: Array<'Success' | 'Warning' | 'Error'>; 285 289 arrayWithDescription?: Array<number>; 290 + /** 291 + * This is a simple enum with strings 292 + */ 293 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 286 294 }; 287 295 288 296 /**
+9 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.ts.snap
··· 319 319 isRequired: true, 320 320 isNullable: true, 321 321 }, 322 + 'foo_bar-enum': { 323 + type: 'Enum', 324 + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 325 + }, 322 326 }, 323 327 } as const; 324 328 325 329 export const $ModelWithEnum = { 326 330 description: `This is a model with one enum`, 327 331 properties: { 328 - test: { 332 + 'foo_bar-enum': { 329 333 type: 'Enum', 330 334 enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 331 335 }, ··· 391 395 type: 'number', 392 396 description: `Success=1,Warning=2,Error=3`, 393 397 }, 398 + }, 399 + 'foo_bar-enum': { 400 + type: 'Enum', 401 + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 394 402 }, 395 403 }, 396 404 } as const;
+24 -2
packages/openapi-ts/test/__snapshots__/test/generated/v3_client/models.ts.snap
··· 288 288 * This is a simple string property 289 289 */ 290 290 nullableRequiredProp2: string | null; 291 + /** 292 + * This is a simple enum with strings 293 + */ 294 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 291 295 }; 292 296 297 + export const FooBarEnumEnum = { 298 + SUCCESS: 'Success', 299 + WARNING: 'Warning', 300 + ERROR: 'Error', 301 + ØÆÅ字符串: 'ØÆÅ字符串', 302 + } as const; 303 + 293 304 /** 294 305 * This is a model with one enum 295 306 */ ··· 297 308 /** 298 309 * This is a simple enum with strings 299 310 */ 300 - test?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 311 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 301 312 /** 302 313 * These are the HTTP error code enums 303 314 */ ··· 308 319 bool?: boolean; 309 320 }; 310 321 311 - export const TestEnum = { 322 + export const FooBarEnumEnum2 = { 312 323 SUCCESS: 'Success', 313 324 WARNING: 'Warning', 314 325 ERROR: 'Error', ··· 353 364 dictionaryWithEnumFromDescription?: Record<string, number>; 354 365 arrayWithEnum?: Array<'Success' | 'Warning' | 'Error'>; 355 366 arrayWithDescription?: Array<number>; 367 + /** 368 + * This is a simple enum with strings 369 + */ 370 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 356 371 }; 372 + 373 + export const FooBarEnumEnum3 = { 374 + SUCCESS: 'Success', 375 + WARNING: 'Warning', 376 + ERROR: 'Error', 377 + ØÆÅ字符串: 'ØÆÅ字符串', 378 + } as const; 357 379 358 380 /** 359 381 * This is a model with one property containing a reference
+9 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/models.ts.snap
··· 274 274 * This is a simple string property 275 275 */ 276 276 nullableRequiredProp2: string | null; 277 + /** 278 + * This is a simple enum with strings 279 + */ 280 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 277 281 }; 278 282 279 283 /** ··· 283 287 /** 284 288 * This is a simple enum with strings 285 289 */ 286 - test?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 290 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 287 291 /** 288 292 * These are the HTTP error code enums 289 293 */ ··· 319 323 dictionaryWithEnumFromDescription?: Record<string, number>; 320 324 arrayWithEnum?: Array<'Success' | 'Warning' | 'Error'>; 321 325 arrayWithDescription?: Array<number>; 326 + /** 327 + * This is a simple enum with strings 328 + */ 329 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 322 330 }; 323 331 324 332 /**
+9 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/schemas.ts.snap
··· 319 319 isRequired: true, 320 320 isNullable: true, 321 321 }, 322 + 'foo_bar-enum': { 323 + type: 'Enum', 324 + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 325 + }, 322 326 }, 323 327 } as const; 324 328 325 329 export const $ModelWithEnum = { 326 330 description: `This is a model with one enum`, 327 331 properties: { 328 - test: { 332 + 'foo_bar-enum': { 329 333 type: 'Enum', 330 334 enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 331 335 }, ··· 391 395 type: 'number', 392 396 description: `Success=1,Warning=2,Error=3`, 393 397 }, 398 + }, 399 + 'foo_bar-enum': { 400 + type: 'Enum', 401 + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 394 402 }, 395 403 }, 396 404 } as const;
+9 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_experimental/models.ts.snap
··· 238 238 * This is a simple string property 239 239 */ 240 240 nullableRequiredProp2: string | null; 241 + /** 242 + * This is a simple enum with strings 243 + */ 244 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 241 245 }; 242 246 243 247 /** ··· 247 251 /** 248 252 * This is a simple enum with strings 249 253 */ 250 - test?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 254 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 251 255 /** 252 256 * These are the HTTP error code enums 253 257 */ ··· 283 287 dictionaryWithEnumFromDescription?: Record<string, number>; 284 288 arrayWithEnum?: Array<'Success' | 'Warning' | 'Error'>; 285 289 arrayWithDescription?: Array<number>; 290 + /** 291 + * This is a simple enum with strings 292 + */ 293 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 286 294 }; 287 295 288 296 /**
+9 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_experimental/schemas.ts.snap
··· 319 319 isRequired: true, 320 320 isNullable: true, 321 321 }, 322 + 'foo_bar-enum': { 323 + type: 'Enum', 324 + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 325 + }, 322 326 }, 323 327 } as const; 324 328 325 329 export const $ModelWithEnum = { 326 330 description: `This is a model with one enum`, 327 331 properties: { 328 - test: { 332 + 'foo_bar-enum': { 329 333 type: 'Enum', 330 334 enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 331 335 }, ··· 391 395 type: 'number', 392 396 description: `Success=1,Warning=2,Error=3`, 393 397 }, 398 + }, 399 + 'foo_bar-enum': { 400 + type: 'Enum', 401 + enum: ['Success', 'Warning', 'Error', 'ØÆÅ字符串'], 394 402 }, 395 403 }, 396 404 } as const;
+9 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_models/models.ts.snap
··· 238 238 * This is a simple string property 239 239 */ 240 240 nullableRequiredProp2: string | null; 241 + /** 242 + * This is a simple enum with strings 243 + */ 244 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 241 245 }; 242 246 243 247 /** ··· 247 251 /** 248 252 * This is a simple enum with strings 249 253 */ 250 - test?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 254 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 251 255 /** 252 256 * These are the HTTP error code enums 253 257 */ ··· 283 287 dictionaryWithEnumFromDescription?: Record<string, number>; 284 288 arrayWithEnum?: Array<'Success' | 'Warning' | 'Error'>; 285 289 arrayWithDescription?: Array<number>; 290 + /** 291 + * This is a simple enum with strings 292 + */ 293 + 'foo_bar-enum'?: 'Success' | 'Warning' | 'Error' | 'ØÆÅ字符串'; 286 294 }; 287 295 288 296 /**
+19 -1
packages/openapi-ts/test/spec/v3.json
··· 2055 2055 "string", 2056 2056 "null" 2057 2057 ] 2058 + }, 2059 + "foo_bar-enum": { 2060 + "description": "This is a simple enum with strings", 2061 + "enum": [ 2062 + "Success", 2063 + "Warning", 2064 + "Error", 2065 + "ØÆÅ字符串" 2066 + ] 2058 2067 } 2059 2068 } 2060 2069 }, ··· 2062 2071 "description": "This is a model with one enum", 2063 2072 "type": "object", 2064 2073 "properties": { 2065 - "test": { 2074 + "foo_bar-enum": { 2066 2075 "description": "This is a simple enum with strings", 2067 2076 "enum": [ 2068 2077 "Success", ··· 2152 2161 "type": "integer", 2153 2162 "description": "Success=1,Warning=2,Error=3" 2154 2163 } 2164 + }, 2165 + "foo_bar-enum": { 2166 + "description": "This is a simple enum with strings", 2167 + "enum": [ 2168 + "Success", 2169 + "Warning", 2170 + "Error", 2171 + "ØÆÅ字符串" 2172 + ] 2155 2173 } 2156 2174 } 2157 2175 },