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 allOf being generated as union instead of intersection in Zod plugins

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

+98 -40
+49 -20
packages/openapi-ts/src/plugins/zod/mini/plugin.ts
··· 69 69 }); 70 70 } else { 71 71 if (schema.logicalOperator === 'and') { 72 - // TODO: parser - handle intersection 73 - // return tsc.typeArrayNode( 74 - // tsc.typeIntersectionNode({ types: itemExpressions }), 75 - // ); 76 - } 77 - 78 - result.expression = tsc.callExpression({ 79 - functionName: tsc.propertyAccessExpression({ 80 - expression: zSymbol.placeholder, 81 - name: identifiers.array, 82 - }), 83 - parameters: [ 84 - tsc.callExpression({ 72 + const firstSchema = schema.items![0]!; 73 + // we want to add an intersection, but not every schema can use the same API. 74 + // if the first item contains another array or not an object, we cannot use 75 + // `.intersection()` as that does not exist on `.union()` and non-object schemas. 76 + let intersectionExpression: ts.Expression; 77 + if ( 78 + firstSchema.logicalOperator === 'or' || 79 + (firstSchema.type && firstSchema.type !== 'object') 80 + ) { 81 + intersectionExpression = tsc.callExpression({ 85 82 functionName: tsc.propertyAccessExpression({ 86 83 expression: zSymbol.placeholder, 87 - name: identifiers.union, 84 + name: identifiers.intersection, 88 85 }), 89 - parameters: [ 90 - tsc.arrayLiteralExpression({ 91 - elements: itemExpressions, 86 + parameters: itemExpressions, 87 + }); 88 + } else { 89 + intersectionExpression = itemExpressions[0]!; 90 + for (let i = 1; i < itemExpressions.length; i++) { 91 + intersectionExpression = tsc.callExpression({ 92 + functionName: tsc.propertyAccessExpression({ 93 + expression: intersectionExpression, 94 + name: identifiers.intersection, 92 95 }), 93 - ], 96 + parameters: [intersectionExpression, itemExpressions[i]!], 97 + }); 98 + } 99 + } 100 + 101 + result.expression = tsc.callExpression({ 102 + functionName, 103 + parameters: [intersectionExpression], 104 + }); 105 + } else { 106 + result.expression = tsc.callExpression({ 107 + functionName: tsc.propertyAccessExpression({ 108 + expression: zSymbol.placeholder, 109 + name: identifiers.array, 94 110 }), 95 - ], 96 - }); 111 + parameters: [ 112 + tsc.callExpression({ 113 + functionName: tsc.propertyAccessExpression({ 114 + expression: zSymbol.placeholder, 115 + name: identifiers.union, 116 + }), 117 + parameters: [ 118 + tsc.arrayLiteralExpression({ 119 + elements: itemExpressions, 120 + }), 121 + ], 122 + }), 123 + ], 124 + }); 125 + } 97 126 } 98 127 } 99 128
+49 -20
packages/openapi-ts/src/plugins/zod/v4/plugin.ts
··· 69 69 }); 70 70 } else { 71 71 if (schema.logicalOperator === 'and') { 72 - // TODO: parser - handle intersection 73 - // return tsc.typeArrayNode( 74 - // tsc.typeIntersectionNode({ types: itemExpressions }), 75 - // ); 76 - } 77 - 78 - result.expression = tsc.callExpression({ 79 - functionName: tsc.propertyAccessExpression({ 80 - expression: zSymbol.placeholder, 81 - name: identifiers.array, 82 - }), 83 - parameters: [ 84 - tsc.callExpression({ 72 + const firstSchema = schema.items![0]!; 73 + // we want to add an intersection, but not every schema can use the same API. 74 + // if the first item contains another array or not an object, we cannot use 75 + // `.and()` as that does not exist on `.union()` and non-object schemas. 76 + let intersectionExpression: ts.Expression; 77 + if ( 78 + firstSchema.logicalOperator === 'or' || 79 + (firstSchema.type && firstSchema.type !== 'object') 80 + ) { 81 + intersectionExpression = tsc.callExpression({ 85 82 functionName: tsc.propertyAccessExpression({ 86 83 expression: zSymbol.placeholder, 87 - name: identifiers.union, 84 + name: identifiers.intersection, 88 85 }), 89 - parameters: [ 90 - tsc.arrayLiteralExpression({ 91 - elements: itemExpressions, 86 + parameters: itemExpressions, 87 + }); 88 + } else { 89 + intersectionExpression = itemExpressions[0]!; 90 + for (let i = 1; i < itemExpressions.length; i++) { 91 + intersectionExpression = tsc.callExpression({ 92 + functionName: tsc.propertyAccessExpression({ 93 + expression: intersectionExpression, 94 + name: identifiers.and, 92 95 }), 93 - ], 96 + parameters: [itemExpressions[i]!], 97 + }); 98 + } 99 + } 100 + 101 + result.expression = tsc.callExpression({ 102 + functionName, 103 + parameters: [intersectionExpression], 104 + }); 105 + } else { 106 + result.expression = tsc.callExpression({ 107 + functionName: tsc.propertyAccessExpression({ 108 + expression: zSymbol.placeholder, 109 + name: identifiers.array, 94 110 }), 95 - ], 96 - }); 111 + parameters: [ 112 + tsc.callExpression({ 113 + functionName: tsc.propertyAccessExpression({ 114 + expression: zSymbol.placeholder, 115 + name: identifiers.union, 116 + }), 117 + parameters: [ 118 + tsc.arrayLiteralExpression({ 119 + elements: itemExpressions, 120 + }), 121 + ], 122 + }), 123 + ], 124 + }); 125 + } 97 126 } 98 127 } 99 128