-
-
Notifications
You must be signed in to change notification settings - Fork 532
--path-params-as-types doesn't work #1003
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
Comments
I ran into this issue aswell. |
@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. |
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 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 |
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:
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. 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
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. |
Description
Firstly, big thanks for creating this project @drwpow! Solved lot of my pain points.
--path-params-as-types
doesn't work becausepathItemObject
intransformPathsObject
is an object made up of HTTP methods (get
,post
etc). However, the code directly referencespathItemObject.parameters
instead of looping through all method keys and then referencingparameters
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.
openapi-typescript
6.0.3
16.14.1
macOS 12.3.1
Expected result
Should generate dynamic string lookups
Checklist
The text was updated successfully, but these errors were encountered: