Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(openapi-generator): suggest resolution for unknown codec types #974

Conversation

ericcrosson-bitgo
Copy link
Contributor

The Problem

If you try to import a codec from io-ts-types like this:

import { DateFromISOString} from 'io-ts-types/DateFromISOString';

you will see an error like this:

node_modules/@api-ts/openapi-generator/dist/src/codec.js:287
    if (init.type === 'Identifier' || init.type === 'MemberExpression') {
             ^

TypeError: Cannot read properties of null (reading 'type')

This is not a helpful error message, as it does not indicate

  1. What went wrong?
  2. What does the user need to do to fix it?

Additional Context

Since codecs from io-ts-types are frequently used, @api-ts/openapi-generator ships with knowledge of io-ts-types codecs1. However, these defaults only match against the user's code when the user imports from the top-level io-ts-types package, like this:

import { DateFromISOString} from 'io-ts-types';

This Change

This diff detects when @api-ts/openapi-generator encounters a codec for which it cannot determine encode/decode types. This happens when a codec is declared with t.Type instead of the io-ts combinators, which happens in io-ts-types and in some third-party modules.

In this case, the generator will now print a message informing the user what went wrong, and provides a suggestion on how the user should resolve the issue:

[ERROR] Could not determine encode/decode types for codec 'MyCustomCodec' in '~/workspace/node_modules/my-custom-types/lib/MyCustomType.d.ts'
Consider defining a custom codec for this type.

https://github.com/BitGo/api-ts/tree/master/packages/openapi-generator#4-defining-custom-codecs

Additionally, when the path defining the codec comes from io-ts-types, the message is even more specific:

[ERROR] Could not determine encode/decode types for codec 'DateFromISOString' in '~/workspace/node_modules/io-ts-types/lib/DateFromISOString.d.ts'
It looks like this codec comes from io-ts-types. Try importing directly from io-ts-types instead:

import { DateFromISOString } from 'io-ts-types';

Considerations

I was not able to easily write any tests for this functionality, since our test harness does not currently handle failure cases and asserting against given error messages.

Footnotes

  1. https://github.com/BitGo/api-ts/blob/b214578caa8272e8370025ae3ef6fd7bf45d514c/packages/openapi-generator/src/knownImports.ts#L334

The Problem
-----------

If you try to import a codec from `io-ts-types` like this:

```typescript
import { DateFromISOString} from 'io-ts-types/DateFromISOString';
```

you will see an error like this:

```
node_modules/@api-ts/openapi-generator/dist/src/codec.js:287
    if (init.type === 'Identifier' || init.type === 'MemberExpression') {
             ^

TypeError: Cannot read properties of null (reading 'type')
```

This is not a helpful error message, as it does not indicate

1. What went wrong?
2. What does the user need to do to fix it?

Additional Context
------------------

Since codecs from `io-ts-types` are frequently used,
@api-ts/openapi-generator ships with knowledge of `io-ts-types`
codecs[^1]. However, these defaults only match against the user's code
when the user imports from the top-level `io-ts-types` package, like
this:

```typescript
import { DateFromISOString} from 'io-ts-types';
```

This Change
-----------

This diff detects when @api-ts/openapi-generator encounters a codec
for which it cannot determine encode/decode types. This happens when a
codec is declared with `t.Type` instead of the io-ts combinators, which
happens in `io-ts-types` and in some third-party modules.

In this case, the generator will now print a message informing the
user what went wrong, and provides a suggestion on how the user should
resolve the issue:

> [ERROR] Could not determine encode/decode types for codec 'MyCustomCodec' in '~/workspace/node_modules/my-custom-types/lib/MyCustomType.d.ts'
> Consider defining a custom codec for this type.
>
> https://github.com/BitGo/api-ts/tree/master/packages/openapi-generator#4-defining-custom-codecs

Additionally, when the path defining the codec comes from `io-ts-types`,
the message is even more specific:

> [ERROR] Could not determine encode/decode types for codec 'DateFromISOString' in '~/workspace/node_modules/io-ts-types/lib/DateFromISOString.d.ts'
> It looks like this codec comes from io-ts-types. Try importing directly from io-ts-types instead:
>
> ```
> import { DateFromISOString } from 'io-ts-types';
> ```

Considerations
--------------

I was not able to easily write any tests for this functionality, since
our test harness does not currently handle failure cases and asserting
against given error messages.

[^1]: https://github.com/BitGo/api-ts/blob/b214578caa8272e8370025ae3ef6fd7bf45d514c/packages/openapi-generator/src/knownImports.ts#L334
@ericcrosson-bitgo ericcrosson-bitgo marked this pull request as ready for review December 23, 2024 21:31
@ericcrosson-bitgo ericcrosson-bitgo requested a review from a team as a code owner December 23, 2024 21:31
@ericcrosson-bitgo ericcrosson-bitgo merged commit a3d388b into BitGo:master Dec 24, 2024
6 checks passed
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.

2 participants