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

parameter with same name as schema conflicts

Open benny-noumena opened this issue 1 year ago • 11 comments

We have a parameter and a schema with the same name but they differ in casing.

We noticed that as of 0.24, types for parameters are generated too. This results in a file override on case-insensitive file systems.

Any idea how to solve this? Is there a flag to not generate types for parameters? What about moving models and parameters in a subdirectory?

Many thanks for advice.

benny-noumena avatar Jan 10 '24 11:01 benny-noumena

I'm seeing something similar too i think. As of 0.24 I'm getting many import errors in the client.

src/client/models/WorkflowBase.ts:5:15 - error TS2724: '"./Name"' has no exported member named 'Name'. Did you mean 'name'? import type { Name } from './Name';

Where the file Name.ts has export type name = string;

markygab avatar Jan 31 '24 04:01 markygab

@benny-noumena @markygab can you replicate this issue in our fork? It also has a --useOperationId flag which you can set to false to generate method names based on path (worth trying?)

mrlubos avatar Feb 20 '24 12:02 mrlubos

@mrlubos Sadly it still is an issue :(

image image image

benny-noumena avatar Feb 21 '24 12:02 benny-noumena

@benny-noumena are you able to provide spec to replicate this?

mrlubos avatar Feb 21 '24 13:02 mrlubos

@mrlubos You mean, like providing an openapi.yml fille? The reproduction is simple. If you have a parameter and a schema with the same name with different casing, then you end up having an export that differs only in casing. On a case-insensitive file-system this leads to the override of the file. In the end the ./models/Tenant.ts and the ./models/tenant.ts are the same file.

My solution would be to export all the parameters in a parameters namespace. But I don't know if this is feasible or not.

benny-noumena avatar Feb 21 '24 13:02 benny-noumena

@benny-noumena do you then import either model or parameter in your code? Is the issue just that the generated client isn't correct?

mrlubos avatar Mar 03 '24 04:03 mrlubos

Here's an example schema that demonstrates the problem:

openapi: 3.0.0
info:
  title: Sample API
  description: Example of conflicting component and parameter schemas
  version: 0.1.9
components:
  schemas:
    Metadata:
      type: string
  parameters:
    metadata:
      in: query
      schema:
        type: string

Generating the client and attempting to build it with TypeScript results in the following errors:

src/index.ts:10:15 - error TS2724: '"./models/Metadata"' has no exported member named 'Metadata'. Did you mean 'metadata'?

---

src/index.ts:11:31 - error TS1149: File name 'src/models/metadata.ts' differs from already included file name 'src/models/Metadata.ts' only in casing.
  The file is in the program because:
    Imported via './models/Metadata' from file 'src/index.ts'
    Imported via './models/metadata' from file 'src/index.ts'
    Matched by include pattern 'src' in 'tsconfig.json'

Additionally since I'm on a case-insensitive file system only Metadata.ts actually exists (I assume metadata.ts was overwritten).

andrew-pledge-io avatar Mar 04 '24 11:03 andrew-pledge-io

Thank you @andrew-pledge-io. Do you import parameters anywhere in your application code? Any preference for the ideal solution?

mrlubos avatar Mar 04 '24 13:03 mrlubos

I don't know. We don't use these parameters for now. However I guess there was a reason why these types were added. My idea was to generate them in a separate directory parameters. Like a namespace parameters?

benny-noumena avatar Mar 04 '24 14:03 benny-noumena

@benny-noumena yeah, it will be a breaking change either way which sucks. I'll try to find out the original motivation for it and go from there. I like the idea of single import module rather than having to go into subdirectories, so maybe prefix would work like we have for schemas ($), would that be okay with you?

mrlubos avatar Mar 04 '24 14:03 mrlubos

A prefix would be a good solution for our use case.

Adding a configurable option for the prefix could make it a non-breaking change if the default is an empty prefix.

Detecting path separators in the prefix could also allow the option to be used for generating into a subdirectory? Though that perhaps is complicating things.

andrew-pledge-io avatar Mar 04 '24 15:03 andrew-pledge-io