Skip to content

Commit 8248f96

Browse files
authored
Merge pull request #1767 from Schroedi/fix/transformer-returns
fix: nested transformers wrong returns
2 parents fae5e53 + b5a972b commit 8248f96

File tree

5 files changed

+109
-36
lines changed

5 files changed

+109
-36
lines changed

.changeset/strange-laws-marry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
fix: handle nested dates in transformers

packages/openapi-ts/src/plugins/@hey-api/transformers/plugin.ts

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ const processSchemaType = ({
243243
});
244244

245245
if (dataExpression) {
246+
// In a map callback, the item needs to be returned, not just the transformation result
247+
if (typeof dataExpression === 'string' && dataExpression === 'item') {
248+
return [
249+
compiler.returnStatement({
250+
expression: callExpression,
251+
}),
252+
];
253+
}
254+
246255
return [
247256
typeof dataExpression === 'string'
248257
? callExpression
@@ -267,17 +276,34 @@ const processSchemaType = ({
267276
? []
268277
: processSchemaType({
269278
context,
279+
dataExpression: 'item',
270280
plugin,
271-
schema: {
272-
...schema,
273-
type: undefined,
274-
},
281+
schema: schema.items?.[0]
282+
? schema.items[0]
283+
: {
284+
...schema,
285+
type: undefined,
286+
},
275287
});
276288

277289
if (!nodes.length) {
278290
return [];
279291
}
280292

293+
// Ensure the map callback has a return statement for the item
294+
const mapCallbackStatements = ensureStatements(nodes);
295+
const hasReturnStatement = mapCallbackStatements.some((stmt) =>
296+
isNodeReturnStatement({ node: stmt }),
297+
);
298+
299+
if (!hasReturnStatement) {
300+
mapCallbackStatements.push(
301+
compiler.returnStatement({
302+
expression: compiler.identifier({ text: 'item' }),
303+
}),
304+
);
305+
}
306+
281307
return [
282308
compiler.assignment({
283309
left: dataExpression,
@@ -295,16 +321,7 @@ const processSchemaType = ({
295321
type: 'any',
296322
},
297323
],
298-
statements:
299-
nodes.length === 1
300-
? ts.isStatement(nodes[0]!)
301-
? []
302-
: [
303-
compiler.returnStatement({
304-
expression: nodes[0],
305-
}),
306-
]
307-
: ensureStatements(nodes),
324+
statements: mapCallbackStatements,
308325
}),
309326
],
310327
}),
@@ -344,17 +361,6 @@ const processSchemaType = ({
344361
}
345362
}
346363

347-
if (nodes.length) {
348-
nodes.push(
349-
compiler.returnStatement({
350-
expression:
351-
typeof dataExpression === 'string'
352-
? compiler.identifier({ text: dataExpression })
353-
: dataExpression,
354-
}),
355-
);
356-
}
357-
358364
return nodes;
359365
}
360366

@@ -406,16 +412,7 @@ const processSchemaType = ({
406412
compiler.ifStatement({
407413
expression: identifierItem,
408414
thenStatement: compiler.block({
409-
statements:
410-
nodes.length === 1
411-
? ts.isStatement(nodes[0]!)
412-
? []
413-
: [
414-
compiler.returnStatement({
415-
expression: nodes[0],
416-
}),
417-
]
418-
: ensureStatements(nodes),
415+
statements: ensureStatements(nodes),
419416
}),
420417
}),
421418
compiler.returnStatement({ expression: identifierItem }),

packages/openapi-ts/test/__snapshots__/3.1.x/transformers-any-of-null/transformers.gen.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

3-
import type { GetFooResponse } from './types.gen';
3+
import type { GetFooResponse, NestedDateObjectResponse } from './types.gen';
44

55
const fooSchemaResponseTransformer = (data: any) => {
66
if (data.foo) {
@@ -20,4 +20,18 @@ export const getFooResponseTransformer = async (data: any): Promise<GetFooRespon
2020
return fooSchemaResponseTransformer(item);
2121
});
2222
return data;
23+
};
24+
25+
const nestedDateObjectSchemaResponseTransformer = (data: any) => {
26+
if (data.foo) {
27+
if (data.foo.bar) {
28+
data.foo.bar = new Date(data.foo.bar);
29+
}
30+
}
31+
return data;
32+
};
33+
34+
export const nestedDateObjectResponseTransformer = async (data: any): Promise<NestedDateObjectResponse> => {
35+
data = nestedDateObjectSchemaResponseTransformer(data);
36+
return data;
2337
};

packages/openapi-ts/test/__snapshots__/3.1.x/transformers-any-of-null/types.gen.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

3+
/**
4+
* Object with a nested date structure
5+
*/
6+
export type NestedDateObject = {
7+
foo?: {
8+
bar?: Date;
9+
};
10+
};
11+
312
export type Foo = {
413
foo?: Date;
514
bar?: Date | null;
@@ -22,6 +31,22 @@ export type GetFooResponses = {
2231

2332
export type GetFooResponse = GetFooResponses[keyof GetFooResponses];
2433

34+
export type NestedDateObjectData = {
35+
body?: never;
36+
path?: never;
37+
query?: never;
38+
url: '/api/nested-date-object';
39+
};
40+
41+
export type NestedDateObjectResponses = {
42+
/**
43+
* Object with nested date
44+
*/
45+
200: NestedDateObject;
46+
};
47+
48+
export type NestedDateObjectResponse = NestedDateObjectResponses[keyof NestedDateObjectResponses];
49+
2550
export type ClientOptions = {
2651
baseUrl: `${string}://${string}` | (string & {});
2752
};

packages/openapi-ts/test/spec/3.1.x/transformers-any-of-null.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,42 @@
2323
}
2424
}
2525
}
26+
},
27+
"/api/nested-date-object": {
28+
"get": {
29+
"operationId": "nestedDateObject",
30+
"responses": {
31+
"200": {
32+
"description": "Object with nested date",
33+
"content": {
34+
"application/json": {
35+
"schema": {
36+
"$ref": "#/components/schemas/NestedDateObject"
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
2643
}
2744
},
2845
"components": {
2946
"schemas": {
47+
"NestedDateObject": {
48+
"description": "Object with a nested date structure",
49+
"type": "object",
50+
"properties": {
51+
"foo": {
52+
"type": "object",
53+
"properties": {
54+
"bar": {
55+
"type": "string",
56+
"format": "date-time"
57+
}
58+
}
59+
}
60+
}
61+
},
3062
"Foo": {
3163
"type": "object",
3264
"properties": {

0 commit comments

Comments
 (0)