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 #1309 from hey-api/fix/invalid-type-handle

fix: gracefully handle invalid schema type in experimental parser

authored by

Lubos and committed by
GitHub
cb30269c a993b8ef

+92 -36
+5
.changeset/thirty-chefs-jam.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + fix: gracefully handle invalid schema type in experimental parser
+12 -1
packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts
··· 715 715 irSchema, 716 716 schema, 717 717 }); 718 + default: 719 + // gracefully handle invalid type 720 + return parseUnknown({ 721 + context, 722 + irSchema, 723 + schema, 724 + }); 718 725 } 719 726 }; 720 727 721 728 const parseUnknown = ({ 729 + irSchema, 722 730 schema, 723 731 }: SchemaContext & { 732 + irSchema?: IRSchemaObject; 724 733 schema: SchemaObject; 725 734 }): IRSchemaObject => { 726 - const irSchema = initIrSchema({ schema }); 735 + if (!irSchema) { 736 + irSchema = initIrSchema({ schema }); 737 + } 727 738 728 739 irSchema.type = 'unknown'; 729 740
+12 -1
packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts
··· 684 684 irSchema, 685 685 schema, 686 686 }); 687 + default: 688 + // gracefully handle invalid type 689 + return parseUnknown({ 690 + context, 691 + irSchema, 692 + schema, 693 + }); 687 694 } 688 695 }; 689 696 ··· 766 773 }; 767 774 768 775 const parseUnknown = ({ 776 + irSchema, 769 777 schema, 770 778 }: SchemaContext & { 779 + irSchema?: IRSchemaObject; 771 780 schema: SchemaObject; 772 781 }): IRSchemaObject => { 773 - const irSchema = initIrSchema({ schema }); 782 + if (!irSchema) { 783 + irSchema = initIrSchema({ schema }); 784 + } 774 785 775 786 irSchema.type = 'unknown'; 776 787
+11 -4
packages/openapi-ts/src/plugins/@hey-api/types/plugin.ts
··· 997 997 const method = _method as keyof IRPathItemObject; 998 998 const operation = pathItem[method]!; 999 999 1000 - operationToType({ 1001 - context, 1002 - operation, 1003 - }); 1000 + try { 1001 + operationToType({ 1002 + context, 1003 + operation, 1004 + }); 1005 + } catch (error) { 1006 + console.error( 1007 + `🔥 Failed to process operation ${method} ${path}\nschema: ${JSON.stringify(operation, null, 2)}`, 1008 + ); 1009 + throw error; 1010 + } 1004 1011 } 1005 1012 } 1006 1013 };
+7 -6
packages/openapi-ts/test/3.0.x.spec.ts
··· 102 102 config: createConfig({ 103 103 input: 'operation-204.json', 104 104 output: 'operation-204', 105 - plugins: [ 106 - { 107 - name: '@hey-api/types', 108 - tree: true, 109 - }, 110 - ], 111 105 }), 112 106 description: 'handles empty response status codes', 113 107 }, ··· 118 112 plugins: ['@hey-api/services'], 119 113 }), 120 114 description: 'handles non-exploded array query parameters', 115 + }, 116 + { 117 + config: createConfig({ 118 + input: 'type-invalid.json', 119 + output: 'type-invalid', 120 + }), 121 + description: 'gracefully handles invalid type', 121 122 }, 122 123 ]; 123 124
+7 -24
packages/openapi-ts/test/3.1.x.spec.ts
··· 140 140 config: createConfig({ 141 141 input: 'operation-204.json', 142 142 output: 'operation-204', 143 - plugins: [ 144 - { 145 - name: '@hey-api/types', 146 - tree: true, 147 - }, 148 - ], 149 143 }), 150 144 description: 'handles empty response status codes', 151 145 }, ··· 161 155 config: createConfig({ 162 156 input: 'required-all-of-ref.json', 163 157 output: 'required-all-of-ref', 164 - plugins: [ 165 - { 166 - name: '@hey-api/types', 167 - tree: false, 168 - }, 169 - ], 170 158 }), 171 159 description: 'sets allOf composition ref model properties as required', 172 160 }, ··· 174 162 config: createConfig({ 175 163 input: 'required-any-of-ref.json', 176 164 output: 'required-any-of-ref', 177 - plugins: [ 178 - { 179 - name: '@hey-api/types', 180 - tree: false, 181 - }, 182 - ], 183 165 }), 184 166 description: 185 167 'does not set anyOf composition ref model properties as required', ··· 188 170 config: createConfig({ 189 171 input: 'required-one-of-ref.json', 190 172 output: 'required-one-of-ref', 191 - plugins: [ 192 - { 193 - name: '@hey-api/types', 194 - tree: false, 195 - }, 196 - ], 197 173 }), 198 174 description: 199 175 'does not set oneOf composition ref model properties as required', 176 + }, 177 + { 178 + config: createConfig({ 179 + input: 'type-invalid.json', 180 + output: 'type-invalid', 181 + }), 182 + description: 'gracefully handles invalid type', 200 183 }, 201 184 ]; 202 185
+2
packages/openapi-ts/test/__snapshots__/3.0.x/type-invalid/index.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + export * from './types.gen';
+3
packages/openapi-ts/test/__snapshots__/3.0.x/type-invalid/types.gen.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + export type Foo = unknown;
+2
packages/openapi-ts/test/__snapshots__/3.1.x/type-invalid/index.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + export * from './types.gen';
+3
packages/openapi-ts/test/__snapshots__/3.1.x/type-invalid/types.gen.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + export type Foo = unknown;
+14
packages/openapi-ts/test/spec/3.0.x/type-invalid.json
··· 1 + { 2 + "openapi": "3.0.0", 3 + "info": { 4 + "title": "OpenAPI 3.0.0 type invalid example", 5 + "version": "1" 6 + }, 7 + "components": { 8 + "schemas": { 9 + "Foo": { 10 + "type": "invalid" 11 + } 12 + } 13 + } 14 + }
+14
packages/openapi-ts/test/spec/3.1.x/type-invalid.json
··· 1 + { 2 + "openapi": "3.1.0", 3 + "info": { 4 + "title": "OpenAPI 3.1.0 type invalid example", 5 + "version": "1" 6 + }, 7 + "components": { 8 + "schemas": { 9 + "Foo": { 10 + "type": "invalid" 11 + } 12 + } 13 + } 14 + }