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.

refactor: rename to schemaNameTransform, use config param, fix docs style

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

+121 -36
+17 -18
docs/openapi-ts/configuration/parser.md
··· 449 449 450 450 ### Schema Name 451 451 452 - Sometimes your schema names are auto-generated or follow a naming convention that produces verbose or awkward type names. The `schemaName` transform allows you to rename schema component keys throughout the specification, automatically updating all `$ref` pointers. 453 - 454 - This is useful for: 455 - 456 - - Stripping version markers from schema names 457 - - Removing vendor prefixes 458 - - Converting naming conventions 459 - - Shortening deeply qualified names 452 + Sometimes your schema names are auto-generated or follow a naming convention that produces verbose or awkward type names. You can rename schema component keys throughout the specification, automatically updating all `$ref` pointers. For example, stripping version markers from schema names, removing vendor prefixes, converting naming conventions, or shortening deeply qualified names. 460 453 461 454 ::: code-group 462 455 ··· 467 460 parser: { 468 461 transforms: { 469 462 schemaName: (name) => { 470 - // Strip version markers: ServiceRoot_v1_20_0_ServiceRoot → ServiceRoot 471 - let clean = name.replace(/([A-Za-z\d]+)_v\d+_\d+_\d+_([A-Za-z\d]*)/g, (_, p1, p2) => 472 - p2.startsWith(p1) ? p2 : p1 + p2, 473 - ); 474 - // Deduplicate prefixes: Foo_Foo → Foo 475 - const m = clean.match(/^([A-Za-z\d]+)_\1([A-Za-z\d]*)$/); 476 - if (m) clean = m[1] + m[2]; 477 - return clean; 478 - }, 463 + // [!code ++] 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 ++] 472 + ); // [!code ++] 473 + // Deduplicate prefixes: Foo_Foo → Foo // [!code ++] 474 + const m = clean.match(/^([A-Za-z\d]+)_\1([A-Za-z\d]*)$/); // [!code ++] 475 + if (m) clean = m[1] + m[2]; // [!code ++] 476 + return clean; // [!code ++] 477 + }, // [!code ++] 479 478 }, 480 479 }, 481 480 }; ··· 487 486 output: 'src/client', 488 487 parser: { 489 488 transforms: { 490 - schemaName: 'Api{{name}}', // Add "Api" prefix to all schemas 489 + schemaName: 'Api{{name}}', // [!code ++] 491 490 }, 492 491 }, 493 492 };
+22
packages/openapi-ts-tests/main/test/3.1.x.test.ts
··· 694 694 }, 695 695 { 696 696 config: createConfig({ 697 + input: 'transforms-schemas-name.yaml', 698 + output: 'transforms-schemas-name', 699 + parser: { 700 + transforms: { 701 + schemaName: (name: string) => { 702 + // Strip version markers: User_v1_0_0_User → User 703 + let clean = name.replace(/([A-Za-z\d]+)_v\d+_\d+_\d+_([A-Za-z\d]*)/g, (_, p1, p2) => 704 + p2.startsWith(p1) ? p2 : p1 + p2, 705 + ); 706 + // Deduplicate prefixes: Foo_Foo → Foo 707 + const m = clean.match(/^([A-Za-z\d]+)_\1([A-Za-z\d]*)$/); 708 + if (m) clean = m[1] + m[2]; 709 + return clean; 710 + }, 711 + }, 712 + }, 713 + plugins: ['@hey-api/typescript'], 714 + }), 715 + description: 'handles schema name transforms', 716 + }, 717 + { 718 + config: createConfig({ 697 719 input: 'transforms-read-write-nested.yaml', 698 720 output: 'transforms-read-write-nested', 699 721 plugins: ['@hey-api/typescript'],
+3 -8
packages/shared/src/config/parser/types.ts
··· 176 176 }; 177 177 }; 178 178 /** 179 - * Rename schema component keys and automatically update all `$ref` pointers 180 - * throughout the specification. 181 - * 182 - * This is useful for: 183 - * - Stripping version markers from schema names 184 - * - Removing vendor prefixes 185 - * - Converting naming conventions 186 - * - Shortening verbose auto-generated names 179 + * Sometimes your schema names are auto-generated or follow a naming convention 180 + * that produces verbose or awkward type names. You can rename schema component 181 + * keys throughout the specification, automatically updating all `$ref` pointers. 187 182 * 188 183 * @example 189 184 * ```ts
+3 -3
packages/shared/src/openApi/shared/transforms/index.ts
··· 2 2 import { enumsTransform } from './enums'; 3 3 import { propertiesRequiredByDefaultTransform } from './propertiesRequiredByDefault'; 4 4 import { readWriteTransform } from './readWrite'; 5 - import { schemasTransform } from './schemas'; 5 + import { schemaNameTransform } from './schemas'; 6 6 7 7 export const transformOpenApiSpec = ({ context }: { context: Context }) => { 8 8 const { logger } = context; 9 9 const eventTransformOpenApiSpec = logger.timeEvent('transform-openapi-spec'); 10 10 11 11 if (context.config.parser.transforms.schemaName) { 12 - schemasTransform({ 13 - schemaName: context.config.parser.transforms.schemaName, 12 + schemaNameTransform({ 13 + config: context.config.parser.transforms.schemaName, 14 14 spec: context.spec, 15 15 }); 16 16 }
+6 -7
packages/shared/src/openApi/shared/transforms/schemas.ts
··· 38 38 * 4. Renames schema keys in the schemas object 39 39 * 5. Updates all $ref pointers throughout the spec to use the new names 40 40 * 41 - * @param schemaName - The schema name transformer 41 + * @param config - The schema name transformer 42 42 * @param spec - The OpenAPI spec object to transform 43 43 */ 44 - export const schemasTransform = ({ 45 - schemaName, 44 + export const schemaNameTransform = ({ 45 + config, 46 46 spec, 47 47 }: { 48 - schemaName: SchemaNameConfig; 48 + config: SchemaNameConfig; 49 49 spec: unknown; 50 50 }) => { 51 - if (!schemaName) { 51 + if (!config) { 52 52 return; 53 53 } 54 54 ··· 67 67 const newNames = new Set<string>(); 68 68 69 69 // Create a simple config object for applyNaming 70 - const namingConfig = 71 - typeof schemaName === 'function' ? { name: schemaName } : { name: schemaName }; 70 + const namingConfig = typeof config === 'function' ? { name: config } : { name: config }; 72 71 73 72 // First pass: compute all new names and check for collisions 74 73 for (const oldName of Object.keys(schemasObj)) {
+70
specs/3.1.x/transforms-schemas-name.yaml
··· 1 + openapi: 3.1.0 2 + info: 3 + title: Schema Name Transform Test 4 + version: 1.0.0 5 + paths: 6 + /users: 7 + get: 8 + summary: Get users 9 + responses: 10 + '200': 11 + description: Success 12 + content: 13 + application/json: 14 + schema: 15 + $ref: '#/components/schemas/User_v1_0_0_User' 16 + /posts: 17 + post: 18 + summary: Create post 19 + requestBody: 20 + content: 21 + application/json: 22 + schema: 23 + $ref: '#/components/schemas/Post_v2_1_3_Post' 24 + responses: 25 + '201': 26 + description: Created 27 + content: 28 + application/json: 29 + schema: 30 + $ref: '#/components/schemas/Post_v2_1_3_Post' 31 + components: 32 + schemas: 33 + User_v1_0_0_User: 34 + type: object 35 + properties: 36 + id: 37 + type: string 38 + name: 39 + type: string 40 + profile: 41 + $ref: '#/components/schemas/UserProfile_v1_0_0_UserProfile' 42 + UserProfile_v1_0_0_UserProfile: 43 + type: object 44 + properties: 45 + bio: 46 + type: string 47 + avatar: 48 + type: string 49 + Post_v2_1_3_Post: 50 + type: object 51 + properties: 52 + id: 53 + type: string 54 + title: 55 + type: string 56 + author: 57 + $ref: '#/components/schemas/User_v1_0_0_User' 58 + comments: 59 + type: array 60 + items: 61 + $ref: '#/components/schemas/Comment_v1_5_2_Comment' 62 + Comment_v1_5_2_Comment: 63 + type: object 64 + properties: 65 + id: 66 + type: string 67 + text: 68 + type: string 69 + author: 70 + $ref: '#/components/schemas/User_v1_0_0_User'