From 37c9a2a905e4a0fa6413a12773d07cdec0886178 Mon Sep 17 00:00:00 2001 From: Nick Lucas Date: Thu, 13 Jun 2024 12:01:10 +0100 Subject: [PATCH] Fix array iteration output --- packages/openapi-ts/src/compiler/transform.ts | 11 ++++- packages/openapi-ts/src/utils/write/types.ts | 41 +++++++++++++------ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/packages/openapi-ts/src/compiler/transform.ts b/packages/openapi-ts/src/compiler/transform.ts index bb2c5e8d8..c1e27b06b 100644 --- a/packages/openapi-ts/src/compiler/transform.ts +++ b/packages/openapi-ts/src/compiler/transform.ts @@ -91,7 +91,16 @@ export const createArrayTransformMutation = ({ ts.factory.createArrowFunction( undefined, undefined, - [], + [ + ts.factory.createParameterDeclaration( + undefined, + undefined, + ts.factory.createIdentifier('item'), + undefined, + undefined, + undefined, + ), + ], undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), ts.factory.createBlock(statements, true), diff --git a/packages/openapi-ts/src/utils/write/types.ts b/packages/openapi-ts/src/utils/write/types.ts index 3d57733ea..f29b2589d 100644 --- a/packages/openapi-ts/src/utils/write/types.ts +++ b/packages/openapi-ts/src/utils/write/types.ts @@ -219,22 +219,37 @@ const generateTransform = (client: Client, model: Model, onNode: OnNode) => { }; function generateForArray(localPath: string[], localModel: Model) { - const statements = mapTypeToTransformStatements( - localPath, - localModel, - true, - ); + if (localModel.export !== 'array') { + throw new Error( + 'generateForArray should only be called with array models', + ); + } - if (statements.length === 0) { - return []; + if (localModel.$refs.length === 1) { + const nextModel = client.models.find( + (m) => m.meta!.name === localModel.type, + ); + if (!nextModel) { + throw new Error( + `Model ${localModel.type} could not be founded when building array transform`, + ); + } + + const statements = mapTypeToTransformStatements(['item'], nextModel); + + if (statements.length === 0) { + return []; + } + + return [ + compiler.transform.arrayTransformMutation({ + path: localPath, + statements, + }), + ]; } - return [ - compiler.transform.arrayTransformMutation({ - path: localPath, - statements, - }), - ]; + throw new Error('Unsupported array type'); } function generateTransformStatements(