Skip to content

Conversation

@yanxue-22
Copy link
Contributor

@yanxue-22 yanxue-22 commented Oct 22, 2025

Ticket: DX-1799

Problem

The generator couldn't resolve CallExpressions where the callee is an arrow function. When it looked up BooleanFromNullableWithFallback and found an arrow function, it had no logic to parse the function body to extract the codec it returns, falling back to an empty schema.

Example of the issue can be found in the ticket description.


Fix:

  • Adds parseFunctionBody() to extract and parse arrow function return values
  • Detects when CallExpression callees resolve to arrow functions
  • Uses findSymbolInitializer() for cross-file lookup of imported functions
  • Supports expression body (() => expr)

Validation:

Test covers the bug scenario: calling an arrow function BooleanFromNullableWithFallback()within a codec property definition.

Note:

Block statement arrow functions were not part of the bug, but its valid TypeScript that the parser is not supporting.
An example of one is: block statement (() => { return expr }) .

I added that in the error message and have created a new ticket DX-2182 to address this.

@yanxue-22 yanxue-22 force-pushed the DX-1799-Openapi-Generator-Fails-At-Reading-Utils branch 2 times, most recently from dee2852 to e39ea36 Compare October 22, 2025 19:23
@yanxue-22 yanxue-22 changed the title fix: support codec factory functions in OpenAPI generator fix: support arrow functions in OpenAPI generator Oct 23, 2025
@yanxue-22 yanxue-22 force-pushed the DX-1799-Openapi-Generator-Fails-At-Reading-Utils branch from e39ea36 to 04aa45a Compare October 23, 2025 07:25
When arrow functions that return codecs were
imported from a utils module, the OpenAPI generator would output empty
schemas ({}) instead of the correct type definitions.

Example that broke:
```typescript
// utils.ts
export const BooleanFromNullableWithFallback = () =>
  fromNullable(t.union([BooleanFromString, t.boolean]), false);

// schema.ts
import { BooleanFromNullableWithFallback } from './utils';
const Wallet = t.type({
  hasLargeNumberOfAddresses: BooleanFromNullableWithFallback()  // INCORRECTLY Generates: {}
});
```

The generator couldn't resolve CallExpressions where the callee is an
arrow function. When it looked up the identifier and found an arrow
function, it had no logic to parse the function body, falling back to
an empty schema.

This fix:
- Adds parseFunctionBody() to extract and parse arrow function return values
- Detects when CallExpression callees resolve to arrow functions
- Uses findSymbolInitializer() for cross-file lookup of imported functions

Test covers the bug scenario: calling an arrow function factory
(BooleanFromNullableWithFallback()) within a codec property definition.
@yanxue-22 yanxue-22 force-pushed the DX-1799-Openapi-Generator-Fails-At-Reading-Utils branch from 04aa45a to cec1e8b Compare October 23, 2025 07:38
@yanxue-22 yanxue-22 marked this pull request as ready for review October 23, 2025 18:48
@yanxue-22 yanxue-22 requested a review from a team as a code owner October 23, 2025 18:48
@yanxue-22 yanxue-22 assigned yanxue-22 and unassigned yanxue-22 Oct 23, 2025
Copy link
Contributor

@shenbenson shenbenson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yanxue-22 yanxue-22 merged commit 8b539fe into master Oct 23, 2025
6 checks passed
@yanxue-22 yanxue-22 deleted the DX-1799-Openapi-Generator-Fails-At-Reading-Utils branch October 23, 2025 20:31
@github-actions
Copy link

🎉 This PR is included in version @api-ts/[email protected] 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants