feat(openapi-generator): suggest resolution for unknown codec types #974
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The Problem
If you try to import a codec from
io-ts-types
like this:you will see an error like this:
This is not a helpful error message, as it does not indicate
Additional Context
Since codecs from
io-ts-types
are frequently used, @api-ts/openapi-generator ships with knowledge ofio-ts-types
codecs1. However, these defaults only match against the user's code when the user imports from the top-levelio-ts-types
package, like this: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 inio-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:
Additionally, when the path defining the codec comes from
io-ts-types
, the message is even more specific: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
https://github.com/BitGo/api-ts/blob/b214578caa8272e8370025ae3ef6fd7bf45d514c/packages/openapi-generator/src/knownImports.ts#L334 ↩