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.

Merge pull request #1767 from Schroedi/fix/transformer-returns

fix: nested transformers wrong returns

authored by

Lubos and committed by
GitHub
8248f966 fae5e53b

+109 -36
+5
.changeset/strange-laws-marry.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + fix: handle nested dates in transformers
+32 -35
packages/openapi-ts/src/plugins/@hey-api/transformers/plugin.ts
··· 243 243 }); 244 244 245 245 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 + 246 255 return [ 247 256 typeof dataExpression === 'string' 248 257 ? callExpression ··· 267 276 ? [] 268 277 : processSchemaType({ 269 278 context, 279 + dataExpression: 'item', 270 280 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 + }, 275 287 }); 276 288 277 289 if (!nodes.length) { 278 290 return []; 291 + } 292 + 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 + ); 279 305 } 280 306 281 307 return [ ··· 295 321 type: 'any', 296 322 }, 297 323 ], 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, 308 325 }), 309 326 ], 310 327 }), ··· 344 361 } 345 362 } 346 363 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 - 358 364 return nodes; 359 365 } 360 366 ··· 406 412 compiler.ifStatement({ 407 413 expression: identifierItem, 408 414 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), 419 416 }), 420 417 }), 421 418 compiler.returnStatement({ expression: identifierItem }),
+15 -1
packages/openapi-ts/test/__snapshots__/3.1.x/transformers-any-of-null/transformers.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { GetFooResponse } from './types.gen'; 3 + import type { GetFooResponse, NestedDateObjectResponse } from './types.gen'; 4 4 5 5 const fooSchemaResponseTransformer = (data: any) => { 6 6 if (data.foo) { ··· 19 19 data = data.map((item: any) => { 20 20 return fooSchemaResponseTransformer(item); 21 21 }); 22 + 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); 22 36 return data; 23 37 };
+25
packages/openapi-ts/test/__snapshots__/3.1.x/transformers-any-of-null/types.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 + /** 4 + * Object with a nested date structure 5 + */ 6 + export type NestedDateObject = { 7 + foo?: { 8 + bar?: Date; 9 + }; 10 + }; 11 + 3 12 export type Foo = { 4 13 foo?: Date; 5 14 bar?: Date | null; ··· 21 30 }; 22 31 23 32 export type GetFooResponse = GetFooResponses[keyof GetFooResponses]; 33 + 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]; 24 49 25 50 export type ClientOptions = { 26 51 baseUrl: `${string}://${string}` | (string & {});
+32
packages/openapi-ts/test/spec/3.1.x/transformers-any-of-null.json
··· 23 23 } 24 24 } 25 25 } 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 + } 26 43 } 27 44 }, 28 45 "components": { 29 46 "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 + }, 30 62 "Foo": { 31 63 "type": "object", 32 64 "properties": {