openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

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

Open shripadk opened this issue 2 years ago • 11 comments

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

shripadk avatar Nov 22 '22 11:11 shripadk

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

image

moisout avatar Dec 01 '22 12:12 moisout

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

steevsachs avatar Dec 15 '22 17:12 steevsachs

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

acnebs avatar Jan 27 '23 01:01 acnebs

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

larkonesvk avatar Feb 27 '23 07:02 larkonesvk

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

rapidfixer avatar Mar 01 '23 12:03 rapidfixer

Can somebody please provide expected vs actual output here?

mitchell-merry avatar Mar 13 '23 08:03 mitchell-merry

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 avatar May 12 '23 02:05 felixfbecker

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

felixfbecker avatar May 12 '23 03:05 felixfbecker

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

ElForastero avatar Jul 10 '23 20:07 ElForastero

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

drwpow avatar Jul 31 '23 22:07 drwpow

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

Hiieu avatar Apr 01 '24 10:04 Hiieu

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[bot] avatar Aug 06 '24 12:08 github-actions[bot]

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[bot] avatar Aug 15 '24 02:08 github-actions[bot]