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 #2139 from hey-api/fix/parser-read-write-array

fix(parser): skip schema if it's an array or tuple and its items don't have any matching readable or writable scopes

authored by

Lubos and committed by
GitHub
e0306d12 3126e799

+73 -18
+5
.changeset/nice-badgers-stare.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + fix(parser): skip schema if it's an array or tuple and its items don't have any matching readable or writable scopes
+3 -3
packages/openapi-ts-tests/test/openapi-ts.config.ts
··· 93 93 // bundle: true, 94 94 // bundleSource_EXPERIMENTAL: true, 95 95 // exportFromIndex: true, 96 - // name: '@hey-api/client-fetch', 97 - name: 'legacy/fetch', 96 + name: '@hey-api/client-fetch', 97 + // name: 'legacy/fetch', 98 98 // strictBaseUrl: true, 99 99 }, 100 100 { 101 101 exportFromIndex: true, 102 - name: '@hey-api/schemas', 102 + // name: '@hey-api/schemas', 103 103 // type: 'json', 104 104 }, 105 105 {
+20 -3
packages/openapi-ts/src/ir/utils.ts
··· 1 + import { mergeSchemaAccessScopes } from '../openApi/shared/utils/schema'; 1 2 import type { IR } from './types'; 2 3 4 + const assignItems = ({ 5 + items, 6 + schema, 7 + }: { 8 + items: Array<IR.SchemaObject>; 9 + schema: IR.SchemaObject; 10 + }) => { 11 + for (const item of items) { 12 + schema.accessScopes = mergeSchemaAccessScopes( 13 + schema.accessScopes, 14 + item.accessScopes, 15 + ); 16 + } 17 + schema.items = items; 18 + }; 19 + 3 20 /** 4 21 * Simply adds `items` to the schema. Also handles setting the logical operator 5 22 * and avoids setting it for a single item or tuples. ··· 20 37 } 21 38 22 39 if (schema.type === 'tuple') { 23 - schema.items = items; 40 + assignItems({ items, schema }); 24 41 return schema; 25 42 } 26 43 27 44 if (items.length !== 1) { 28 - schema.items = items; 45 + assignItems({ items, schema }); 29 46 schema.logicalOperator = logicalOperator; 30 47 return schema; 31 48 } ··· 39 56 return schema; 40 57 } 41 58 42 - schema.items = items; 59 + assignItems({ items, schema }); 43 60 return schema; 44 61 };
+45 -12
packages/openapi-ts/src/plugins/@hey-api/typescript/plugin.ts
··· 79 79 }: { 80 80 schema: IR.SchemaObject; 81 81 state: State | undefined; 82 - }) => 83 - Boolean( 84 - state?.accessScope && 85 - ((schema.accessScope && state.accessScope !== schema.accessScope) || 86 - (schema.$ref && 87 - schema.accessScopes && 88 - !schema.accessScopes.includes(state.accessScope) && 89 - !schema.accessScopes.includes('both'))), 90 - ); 82 + }) => { 83 + const stateAccessScope = state?.accessScope; 84 + 85 + if (!stateAccessScope) { 86 + return false; 87 + } 88 + 89 + if (schema.accessScope && stateAccessScope !== schema.accessScope) { 90 + return true; 91 + } 92 + 93 + if ( 94 + schema.$ref && 95 + schema.accessScopes && 96 + !schema.accessScopes.includes(stateAccessScope) && 97 + !schema.accessScopes.includes('both') 98 + ) { 99 + return true; 100 + } 101 + 102 + if ( 103 + (schema.type === 'array' || schema.type === 'tuple') && 104 + schema.items && 105 + schema.items.every( 106 + (item) => 107 + item.accessScopes && 108 + !item.accessScopes.includes(stateAccessScope) && 109 + !item.accessScopes.includes('both'), 110 + ) 111 + ) { 112 + return true; 113 + } 114 + 115 + return false; 116 + }; 91 117 92 118 const addJavaScriptEnum = ({ 93 119 $ref, ··· 332 358 plugin: Plugin.Instance<Config>; 333 359 schema: SchemaWithType<'array'>; 334 360 state: State | undefined; 335 - }): ts.TypeNode => { 361 + }): ts.TypeNode | undefined => { 336 362 if (!schema.items) { 337 363 return compiler.typeArrayNode( 338 364 compiler.keywordTypeNode({ ··· 345 371 346 372 const itemTypes: Array<ts.TypeNode> = []; 347 373 348 - // at least one item is guaranteed (or at least was before read/write only) 349 374 for (const item of schema.items!) { 350 375 const type = schemaToType({ 351 376 context, ··· 358 383 if (type) { 359 384 itemTypes.push(type); 360 385 } 386 + } 387 + 388 + if (!itemTypes.length) { 389 + return; 361 390 } 362 391 363 392 if (itemTypes.length === 1) { ··· 674 703 plugin: Plugin.Instance<Config>; 675 704 schema: SchemaWithType<'tuple'>; 676 705 state: State | undefined; 677 - }): ts.TypeNode => { 706 + }): ts.TypeNode | undefined => { 678 707 let itemTypes: Array<ts.Expression | ts.TypeNode> = []; 679 708 680 709 if (schema.const && Array.isArray(schema.const)) { ··· 696 725 itemTypes.push(type); 697 726 } 698 727 } 728 + } 729 + 730 + if (!itemTypes.length) { 731 + return; 699 732 } 700 733 701 734 return compiler.typeTupleNode({