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

Merge pull request #1266 from hey-api/fix/array-items-one-of-length-1

fix: correctly generate array when items are a oneOf array with length 1

authored by

Lubos and committed by
GitHub
04c88dd4 f31d4e27

+113 -10
+5
.changeset/big-trees-hide.md
···
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + fix: correctly generate array when items are a oneOf array with length 1
+3 -4
packages/openapi-ts/src/openApi/3.0.x/parser/schema.ts
··· 81 if ('$ref' in schema.items) { 82 schemaItems.push(irItemsSchema); 83 } else { 84 - const isComposedSchema = Boolean( 85 - schema.items.allOf || schema.items.anyOf || schema.items.oneOf, 86 - ); 87 - if (isComposedSchema) { 88 // bring composition up to avoid incorrectly nested arrays 89 irSchema = { 90 ...irSchema,
··· 81 if ('$ref' in schema.items) { 82 schemaItems.push(irItemsSchema); 83 } else { 84 + const ofArray = 85 + schema.items.allOf || schema.items.anyOf || schema.items.oneOf; 86 + if (ofArray && ofArray.length > 1 && !schema.items.nullable) { 87 // bring composition up to avoid incorrectly nested arrays 88 irSchema = { 89 ...irSchema,
+7 -4
packages/openapi-ts/src/openApi/3.1.x/parser/schema.ts
··· 120 ) { 121 schemaItems = Array(schema.maxItems).fill(irItemsSchema); 122 } else { 123 - const isComposedSchema = Boolean( 124 - schema.items.allOf || schema.items.anyOf || schema.items.oneOf, 125 - ); 126 - if (isComposedSchema) { 127 // bring composition up to avoid incorrectly nested arrays 128 irSchema = { 129 ...irSchema,
··· 120 ) { 121 schemaItems = Array(schema.maxItems).fill(irItemsSchema); 122 } else { 123 + const ofArray = 124 + schema.items.allOf || schema.items.anyOf || schema.items.oneOf; 125 + if ( 126 + ofArray && 127 + ofArray.length > 1 && 128 + !getSchemaTypes({ schema: schema.items }).includes('null') 129 + ) { 130 // bring composition up to avoid incorrectly nested arrays 131 irSchema = { 132 ...irSchema,
+8
packages/openapi-ts/test/3.0.x.spec.ts
··· 50 }, 51 { 52 config: createConfig({ 53 input: 'enum-escape.json', 54 output: 'enum-escape', 55 }),
··· 50 }, 51 { 52 config: createConfig({ 53 + input: 'array-items-one-of-length-1.json', 54 + output: 'array-items-one-of-length-1', 55 + }), 56 + description: 57 + 'generates correct array when items are oneOf array with single item', 58 + }, 59 + { 60 + config: createConfig({ 61 input: 'enum-escape.json', 62 output: 'enum-escape', 63 }),
+8
packages/openapi-ts/test/3.1.x.spec.ts
··· 50 }, 51 { 52 config: createConfig({ 53 input: 'duplicate-null.json', 54 output: 'duplicate-null', 55 }),
··· 50 }, 51 { 52 config: createConfig({ 53 + input: 'array-items-one-of-length-1.json', 54 + output: 'array-items-one-of-length-1', 55 + }), 56 + description: 57 + 'generates correct array when items are oneOf array with single item', 58 + }, 59 + { 60 + config: createConfig({ 61 input: 'duplicate-null.json', 62 output: 'duplicate-null', 63 }),
+2
packages/openapi-ts/test/__snapshots__/3.0.x/array-items-one-of-length-1/index.ts
···
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + export * from './types.gen';
+7
packages/openapi-ts/test/__snapshots__/3.0.x/array-items-one-of-length-1/types.gen.ts
···
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + export type Foo = { 4 + foo?: Array<Bar>; 5 + }; 6 + 7 + export type Bar = string;
+2
packages/openapi-ts/test/__snapshots__/3.1.x/array-items-one-of-length-1/index.ts
···
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + export * from './types.gen';
+7
packages/openapi-ts/test/__snapshots__/3.1.x/array-items-one-of-length-1/types.gen.ts
···
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + export type Foo = { 4 + foo?: Array<Bar>; 5 + }; 6 + 7 + export type Bar = string;
+2 -2
packages/openapi-ts/test/sample.cjs
··· 13 input: { 14 // include: 15 // '^(#/components/schemas/import|#/paths/api/v{api-version}/simple/options)$', 16 - path: './test/spec/3.0.x/parameter-explode-false.json', 17 // path: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json', 18 }, 19 // name: 'foo', ··· 30 { 31 // asClass: true, 32 // include... 33 - name: '@hey-api/services', 34 // serviceNameBuilder: '^Parameters', 35 }, 36 {
··· 13 input: { 14 // include: 15 // '^(#/components/schemas/import|#/paths/api/v{api-version}/simple/options)$', 16 + path: './test/spec/3.1.x/array-items-one-of-length-1.json', 17 // path: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json', 18 }, 19 // name: 'foo', ··· 30 { 31 // asClass: true, 32 // include... 33 + // name: '@hey-api/services', 34 // serviceNameBuilder: '^Parameters', 35 }, 36 {
+31
packages/openapi-ts/test/spec/3.0.x/array-items-one-of-length-1.json
···
··· 1 + { 2 + "openapi": "3.0.2", 3 + "info": { 4 + "title": "OpenAPI 3.0.2 array items oneOf length 1 example", 5 + "version": "1" 6 + }, 7 + "components": { 8 + "schemas": { 9 + "Foo": { 10 + "type": "object", 11 + "properties": { 12 + "foo": { 13 + "type": "array", 14 + "items": { 15 + "oneOf": [ 16 + { 17 + "$ref": "#/components/schemas/Bar" 18 + } 19 + ] 20 + }, 21 + "maxItems": 2147483647, 22 + "minItems": 1 23 + } 24 + } 25 + }, 26 + "Bar": { 27 + "type": "string" 28 + } 29 + } 30 + } 31 + }
+31
packages/openapi-ts/test/spec/3.1.x/array-items-one-of-length-1.json
···
··· 1 + { 2 + "openapi": "3.1.0", 3 + "info": { 4 + "title": "OpenAPI 3.1.0 array items oneOf length 1 example", 5 + "version": "1" 6 + }, 7 + "components": { 8 + "schemas": { 9 + "Foo": { 10 + "type": "object", 11 + "properties": { 12 + "foo": { 13 + "type": "array", 14 + "items": { 15 + "oneOf": [ 16 + { 17 + "$ref": "#/components/schemas/Bar" 18 + } 19 + ] 20 + }, 21 + "maxItems": 2147483647, 22 + "minItems": 1 23 + } 24 + } 25 + }, 26 + "Bar": { 27 + "type": "string" 28 + } 29 + } 30 + } 31 + }