Skip to content

Commit

Permalink
Fix array iteration output
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Lucas committed Jun 13, 2024
1 parent 44aaaa1 commit 37c9a2a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
11 changes: 10 additions & 1 deletion packages/openapi-ts/src/compiler/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
41 changes: 28 additions & 13 deletions packages/openapi-ts/src/utils/write/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 37c9a2a

Please sign in to comment.