openapi-typescript
openapi-typescript copied to clipboard
--path-params-as-types doesn't work
Description
Firstly, big thanks for creating this project @drwpow! Solved lot of my pain points.
--path-params-as-types
doesn't work because pathItemObject
in transformPathsObject
is an object made up of HTTP methods (get
, post
etc). However, the code directly references pathItemObject.parameters
instead of looping through all method keys and then referencing parameters
within it. Hence that block will always evaluate to false.
Reporting it here for now. Will open a PR if you are not taking it up or are busy with other stuff. So please let me know.
Name | Version |
---|---|
openapi-typescript |
6.0.3 |
Node.js | 16.14.1 |
OS + version | macOS 12.3.1 |
Expected result
Should generate dynamic string lookups
Checklist
- [x] I’m willing to open a PR (see CONTRIBUTING.md)
@shripadk Just ran into this today--thank you for diagnosing and raising the issue! Are you planning on opening a PR to resolve this?
Yep AFAICT this was introduced in the v6 re-write. @drwpow is --path-params-as-types working for you since v6?
Same here, --path-params-as-types is not working. I tried latest version, and 5.4.0 as well.
Hm, it runs conversion without errors, but dynamic type inference is not working for me. And 5.4.0 breaks with my API so cannot try it...
Can somebody please provide expected vs actual output here?
I can confirm --path-params-as-types
is not working in latest 6.2.4 but is working in 5.4.1.
In 6.2.4 the option has no effect, i.e. it will generate a plain string path:
interface paths {
"/users/{id}": {
// ...
}
"/posts/{id}" {
// ...
}
}
In 5.4.1 it generates a template literal string with the type of the parameter:
interface paths {
[key: `/users/${number}`]: {
// ...
}
[key: `/posts/${number}`] {
// ...
}
}
To me this is one of the most exciting features of openapi-typescript
so it's really sad it's not working :/
If anyone is looking for a workaround, you can use this mapped type:
import { paths } from './generated-types.ts'
// Workaround for https://github.com/drwpow/openapi-typescript/issues/1003
type PathPattern<Pattern extends string, Params> = Pattern extends `${infer Head}/${infer Rest}`
? // Handle recursion
`${PathPattern<Head, Params>}/${PathPattern<Rest, Params>}`
: // Single segment
Pattern extends `{${infer ParamName}}`
? // It's a param path segment
Params extends { [_ in ParamName]: infer ParamType extends string | number }
? `${ParamType}`
: string
: // It's a literal path segment
Pattern
type templatePaths = {
[P in keyof paths as PathPattern<
P,
paths[P][keyof paths[P]] extends { parameters: { path: infer P } } ? P : never
>]: paths[P]
}
Of course it would be a lot more efficient if we could codegen this directly, I have no idea what this may do to compile speeds (or if I have bugs in there).
If you use the latest v5 version, you can patch the package, as an option.
Replace this:
node_modules/openapi-typescript/dist/transform/paths.js
if (pathItem.parameters) {
params = pathItem.parameters;
}
With this
if (pathItem.parameters || pathItem.get?.parameters) {
params = pathItem.parameters ?? pathItem.get.parameters;
}
Semi-related: there were some bugs that were fixed in parameter generation (#1061) that may have been causing this.
But it’s also worth pointing out that this flag has always had caveats; there are instances where it can break. openapi-fetch
doesn’t use this FWIW.
This flag probably needs some more robust tests to catch some of the errors outlined here. Would love PRs from any users relying on this feature! 🙏
This flag doesn't work in version 6.7.5
. When I run
npx openapi-typescript --path-params-as-types http://localhost:8000/api/v1/schema -o types/openapi-schema.d.ts
It hangs forever. If I remove the flag, then it generates the file, however, without dynamic lookups :(
This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed.
This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem.