Skip to content

--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

Closed
1 task done
shripadk opened this issue Nov 22, 2022 · 13 comments
Closed
1 task done

--path-params-as-types doesn't work #1003

shripadk opened this issue Nov 22, 2022 · 13 comments
Labels
openapi-ts Relevant to the openapi-typescript library stale

Comments

@shripadk
Copy link

shripadk commented Nov 22, 2022

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

@moisout
Copy link

moisout commented Dec 1, 2022

I ran into this issue aswell.
Here is a screenshot of the file paths-object.js, showing the issue.

image

@steevsachs
Copy link

@shripadk Just ran into this today--thank you for diagnosing and raising the issue! Are you planning on opening a PR to resolve this?

@connebs
Copy link

connebs commented Jan 27, 2023

Yep AFAICT this was introduced in the v6 re-write. @drwpow is --path-params-as-types working for you since v6?

@larkonesvk
Copy link

Same here, --path-params-as-types is not working.
I tried latest version, and 5.4.0 as well.

@rapidfixer
Copy link

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...

@mitchell-merry
Copy link
Contributor

Can somebody please provide expected vs actual output here?

@felixfbecker
Copy link

felixfbecker commented May 12, 2023

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 :/

@felixfbecker
Copy link

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).

@drwpow drwpow added the openapi-ts Relevant to the openapi-typescript library label May 22, 2023
@ElForastero
Copy link
Contributor

ElForastero commented Jul 10, 2023

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;
}

@drwpow
Copy link
Contributor

drwpow commented Jul 31, 2023

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! 🙏

@Hiieu
Copy link

Hiieu commented Apr 1, 2024

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 :(

Copy link
Contributor

github-actions bot commented Aug 6, 2024

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.

@github-actions github-actions bot added the stale label Aug 6, 2024
Copy link
Contributor

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.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openapi-ts Relevant to the openapi-typescript library stale
Projects
None yet
Development

No branches or pull requests