Skip to content
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

fix: Ensure that getShortType regex wildcard is non-greedy #135

Merged
merged 2 commits into from
Aug 7, 2024

Conversation

collink
Copy link
Contributor

@collink collink commented Aug 1, 2024

Hello! First, let me thank you for writing this library! Please enjoy a cup of coffee on me!

We use your library for a project at my company and it's made our lives a lot easier! We've been using v0.5.3 for a while, but as part of another effort, I'm updating to v1.4.1. I noticed that some of our OAPI documents are tripping up the react query codegen and wanted to investigate.

I forked your repo and built it locally (which btw required some liberal use of any casting to get the project to build on my M3 mac--node 20, TS 5.5.x--but I removed all of those before committing my fix). I threw in some console.logs in createUseMutation, and in the methodParameters section, refParam.type.getText(param) was returning something in the following shape (paths, param names and type names edited, of course):

'{ type1?: import("/path/to/project/requests/types.gen").Type1; type2: import("/path/to/project/requests/types.gen").Type2; type3: import("/path/to/project/types.gen").Type3; }'

This was producing a mutation type signature that looked like this:

export const useDefaultServiceMyEndpoint = <
  TData = Common.DefaultServiceMyEndpointMutationResult,
  TError = unknown,
  TContext = unknown,
>(
  options?: Omit<
    UseMutationOptions<
      TData,
      TError,
      {
        companyId: number
        requestBody: {
          type1?: Type3
        }
      },
      TContext
    >,
    'mutationFn'
  >,
) => ...

As you can see, it's setting type1's type to Type3, which is incorrect. I discovered that the wildcard in getShortType was greedy, and that making it non-greedy would indeed fix the problem. I included a test with data in the same shape as my case.

Barring any issues with the PR, it would be very helpful for us if we could get this releases ASAP. Please let me know if there's anything further you need from me!

@collink collink changed the title Ensure that getShortType regex wildcard is non-greedy fix: Ensure that getShortType regex wildcard is non-greedy Aug 1, 2024
@collink
Copy link
Contributor Author

collink commented Aug 2, 2024

@7nohe @seriouslag this one should be pretty uncontroversial, haha. Hope it helps! Thanks again for the lib!

@7nohe 7nohe requested a review from seriouslag August 6, 2024 15:28
Copy link
Owner

@7nohe 7nohe left a comment

Choose a reason for hiding this comment

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

@collink Thank you for sponsoring the coffee and for the code fix!

@7nohe 7nohe merged commit 8eb0f33 into 7nohe:main Aug 7, 2024
4 checks passed
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