Skip to content

fix: support nested path parameters in --path-params-as-types #1130

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

Merged
merged 1 commit into from
May 23, 2023

Conversation

barakalon
Copy link
Contributor

@barakalon barakalon commented May 23, 2023

Changes

The existing code for --path-params-as-types assumes path parameters are defined on the PathItemObject:

{
  openapi: "3.1",
  info: { title: "Test", version: "1.0" },
  paths: {
    "/user/{user_id}": {
      parameters: [{ name: "user_id", in: "path" }],
    },
  },
}

But its valid to define them on the OperationObject:

{
  openapi: "3.1",
  info: { title: "Test", version: "1.0" },
  paths: {
    "/user/{user_id}": {
      get: {
        parameters: [{ name: "user_id", in: "path" }],
      },
      put: {
        parameters: [{ name: "user_id", in: "path" }],
      }
    },
  },
}

FastAPI does it this way.

How to Review

This is both my first openapi-typescript PR and my first typescript PR. Don't hold back.

Checklist

  • Unit tests updated
  • README updated
  • examples/ directory updated (only applicable for openapi-typescript)

@changeset-bot
Copy link

changeset-bot bot commented May 23, 2023

🦋 Changeset detected

Latest commit: ed0ab47

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openapi-typescript Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great tests, and great code, thank you!

@drwpow drwpow merged commit 7d09c3b into openapi-ts:main May 23, 2023
drwpow added a commit that referenced this pull request May 23, 2023
drwpow added a commit that referenced this pull request May 23, 2023
drwpow added a commit that referenced this pull request May 23, 2023
drwpow added a commit that referenced this pull request May 23, 2023
* Add changesets CI action

* Update changeset for #1130

* Speed up monorepo build

* Disable npm token for debugging

* Update examples
@mkosir
Copy link

mkosir commented May 24, 2023

This change made majority of query keys optional, which is unwanted behaviour since I can't access query parameters anymore.

@drwpow
Copy link
Contributor

drwpow commented May 25, 2023

This change made majority of query keys optional, which is unwanted behaviour since I can't access query parameters anymore.

query keys being optional should only happen now when they are all optional. It shouldn’t happen if even one is required. This is actually more consistent with other types generated by this library, so I’m in favor of it.

However, if you have even one parameter that is required, then query should be required as well. If you find that happening, then please file a new issue (and no need for a reproduction; if that’s happening it should be easy to reproduce).

If you need to access that query object in types, you can always use the NonNullable<> built-in TS generic.

@mkosir
Copy link

mkosir commented May 26, 2023

Not sure if I'm in favour of this approach, but thanks for clarifying it.
Yes I use Required<>/NonNullable <>, find it a bit cumbersome to access parameter types.

vdawg-git pushed a commit to vdawg-git/openapi-typescript that referenced this pull request Jun 14, 2023
* Add changesets CI action

* Update changeset for openapi-ts#1130

* Speed up monorepo build

* Disable npm token for debugging

* Update examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants