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.

fix: simplify schemaNameTransform logic and add prettier-ignore to docs

Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>

+111 -16
+5 -9
docs/openapi-ts/configuration/parser.md
··· 453 453 454 454 ::: code-group 455 455 456 + <!-- prettier-ignore-start --> 456 457 ```js [function] 457 458 export default { 458 459 input: 'hey-api/backend', // sign up at app.heyapi.dev 459 460 output: 'src/client', 460 461 parser: { 461 462 transforms: { 462 - schemaName: (name) => { 463 - // [!code ++] 463 + schemaName: (name) => { // [!code ++] 464 464 // Strip version markers: ServiceRoot_v1_20_0_ServiceRoot → ServiceRoot // [!code ++] 465 - let clean = name.replace( 466 - /([A-Za-z\d]+)_v\d+_\d+_\d+_([A-Za-z\d]*)/g, 467 - ( 468 - _, 469 - p1, 470 - p2, // [!code ++] 471 - ) => (p2.startsWith(p1) ? p2 : p1 + p2), // [!code ++] 465 + let clean = name.replace(/([A-Za-z\d]+)_v\d+_\d+_\d+_([A-Za-z\d]*)/g, (_, p1, p2) => // [!code ++] 466 + p2.startsWith(p1) ? p2 : p1 + p2, // [!code ++] 472 467 ); // [!code ++] 473 468 // Deduplicate prefixes: Foo_Foo → Foo // [!code ++] 474 469 const m = clean.match(/^([A-Za-z\d]+)_\1([A-Za-z\d]*)$/); // [!code ++] ··· 479 474 }, 480 475 }; 481 476 ``` 477 + <!-- prettier-ignore-end --> 482 478 483 479 ```js [template] 484 480 export default {
+17
packages/openapi-ts-tests/main/test/3.0.x.test.ts
··· 614 614 }, 615 615 { 616 616 config: createConfig({ 617 + input: 'transforms-schemas-name-collision.yaml', 618 + output: 'transforms-schemas-name-collision', 619 + parser: { 620 + transforms: { 621 + schemaName: (name: string) => 622 + // Try to rename all _vX_User schemas to "User" 623 + // This should cause collisions since "User" already exists 624 + name.replace(/_v\d+_User$/, '') 625 + , 626 + }, 627 + }, 628 + plugins: ['@hey-api/typescript'], 629 + }), 630 + description: 'handles schema name collision prevention', 631 + }, 632 + { 633 + config: createConfig({ 617 634 input: 'security-api-key.yaml', 618 635 output: 'security-api-key', 619 636 plugins: [
+3
packages/openapi-ts-tests/main/test/__snapshots__/3.0.x/transforms-schemas-name-collision/index.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + export type { ClientOptions, GetTestData, GetTestResponse, GetTestResponses, User, UserV1User, UserV2User } from './types.gen';
+39
packages/openapi-ts-tests/main/test/__snapshots__/3.0.x/transforms-schemas-name-collision/types.gen.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + export type ClientOptions = { 4 + baseUrl: `${string}://${string}` | (string & {}); 5 + }; 6 + 7 + export type UserV1User = { 8 + id?: string; 9 + version?: 'v1'; 10 + }; 11 + 12 + export type User = { 13 + name?: string; 14 + }; 15 + 16 + export type UserV2User = { 17 + email?: string; 18 + version?: 'v2'; 19 + }; 20 + 21 + export type GetTestData = { 22 + body?: never; 23 + path?: never; 24 + query?: never; 25 + url: '/test'; 26 + }; 27 + 28 + export type GetTestResponses = { 29 + /** 30 + * Success 31 + */ 32 + 200: { 33 + user1?: UserV1User; 34 + user2?: User; 35 + user3?: UserV2User; 36 + }; 37 + }; 38 + 39 + export type GetTestResponse = GetTestResponses[keyof GetTestResponses];
+3 -7
packages/shared/src/openApi/shared/transforms/schemas.ts
··· 75 75 76 76 // Skip if name doesn't change 77 77 if (newName === oldName) { 78 - newNames.add(oldName); 79 78 continue; 80 79 } 81 80 82 - // Skip if new name collides with an existing schema or another renamed schema 83 - if (oldName in schemasObj && newName in schemasObj && oldName !== newName) { 84 - // Collision with existing schema - skip rename 85 - newNames.add(oldName); 81 + // Skip if new name collides with an existing schema 82 + if (newName in schemasObj) { 86 83 continue; 87 84 } 88 85 86 + // Skip if new name collides with another renamed schema 89 87 if (newNames.has(newName)) { 90 - // Collision with another renamed schema - skip rename 91 - newNames.add(oldName); 92 88 continue; 93 89 } 94 90
+44
specs/3.0.x/transforms-schemas-name-collision.yaml
··· 1 + openapi: 3.0.3 2 + info: 3 + title: Schema Name Collision Test 4 + version: 1.0.0 5 + paths: 6 + /test: 7 + get: 8 + responses: 9 + '200': 10 + description: Success 11 + content: 12 + application/json: 13 + schema: 14 + type: object 15 + properties: 16 + user1: 17 + $ref: '#/components/schemas/User_v1_User' 18 + user2: 19 + $ref: '#/components/schemas/User' 20 + user3: 21 + $ref: '#/components/schemas/User_v2_User' 22 + components: 23 + schemas: 24 + User_v1_User: 25 + type: object 26 + properties: 27 + id: 28 + type: string 29 + version: 30 + type: string 31 + enum: ['v1'] 32 + User: 33 + type: object 34 + properties: 35 + name: 36 + type: string 37 + User_v2_User: 38 + type: object 39 + properties: 40 + email: 41 + type: string 42 + version: 43 + type: string 44 + enum: ['v2']