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

Error: Duplicate schema names detected #1733

Open
SamyZog opened this issue Dec 3, 2024 · 11 comments
Open

Error: Duplicate schema names detected #1733

SamyZog opened this issue Dec 3, 2024 · 11 comments
Labels
bug Something isn't working

Comments

@SamyZog
Copy link

SamyZog commented Dec 3, 2024

Trying to generate swagger client from openapi 3.0.3
Orval v7.3.0 (also tried v7.2.0)

When generating i get an error

Error: Duplicate schema names detected:
  1x PatchedPayoutRequestListAdminDTOReimbursability
  1x PayoutRequestListAdminDTOReimbursability
  1x PayoutRequestRetrieveAdminDTOReimbursability
  1x PayoutRequestRetrieveUserDTOReimbursability
  1x PayoutRequestSetReimbursabilityDTOReimbursability

The schema is valid, I have ran it through the swagger editor and all is clean.

this is my config

export default defineConfig({
  newTechService: {
    hooks: {
      afterAllFilesWrite: "eslint --fix src/api",
    },
    output: {
      mode: "tags-split",
      target: "./src/api/query",
      schemas: "./src/api/model",
      client: "react-query",
      httpClient: "axios",
      urlEncodeParameters: false,
      clean: true,
      tslint: true,
      override: {
        fetch: {
          includeHttpResponseReturnType: false,
        },
        query: {
          signal: true,
          useInfinite: false,
        },
        mutator: {
          path: "./src/api/custom-instance/axios-instance.ts",
          name: "customInstance",
        },
        components: {
          schemas: {
            suffix: "DTO",
          },
          responses: {
            suffix: "Response",
          },
          parameters: {
            suffix: "Params",
          },
          requestBodies: {
            suffix: "Body",
          },
        },
      },
    },
    input: {
      target: ...
    },
  },
});
@melloware melloware added the bug Something isn't working label Dec 3, 2024
@nitoba
Copy link

nitoba commented Dec 7, 2024

The same issue here

@AllieJonsson
Copy link
Contributor

@SamyZog Could you provide the schema specification so I can try to replicate this?

@Luiz-N
Copy link
Contributor

Luiz-N commented Jan 21, 2025

@AllieJonsson This happens for me if i have a field name defined twice in both snake_case and camelCase on the same pydantic model class (later used in an endpoint).

@vandreleal
Copy link

If index files are not needed, a temporary workaround is to disable indexFiles. This error only happens when indexFiles is true.

if (indexFiles) {
const schemaFilePath = upath.join(schemaPath, `/index${fileExtension}`);
await fs.ensureFile(schemaFilePath);
// Ensure separate files are used for parallel schema writing.
// Throw an exception, which list all duplicates, before attempting
// multiple writes on the same file.
const schemaNamesSet = new Set<string>();
const duplicateNamesMap = new Map<string, number>();
schemas.forEach((schema) => {
if (!schemaNamesSet.has(schema.name)) {
schemaNamesSet.add(schema.name);
} else {
duplicateNamesMap.set(
schema.name,
(duplicateNamesMap.get(schema.name) || 0) + 1,
);
}
});
if (duplicateNamesMap.size) {
throw new Error(
'Duplicate schema names detected:\n' +
Array.from(duplicateNamesMap)
.map((duplicate) => ` ${duplicate[1]}x ${duplicate[0]}`)
.join('\n'),
);
}

@AllieJonsson
Copy link
Contributor

That workaround works if the two colliding properties are of the same type, but if they are different, e.g.

calling_code:
  type: number
  enum: [1, 2, 3]
callingCode:
  type: string
  enum: ['+33', '+420', '+33']

the generated models will be

interface Pet {
  calling_code: PetCallingCode;
  callingCode: PetCallingCode;
}

...

export type PetCallingCode = (typeof PetCallingCode)[keyof typeof PetCallingCode];
export const PetCallingCode = {
  '+33': '+33',
  '+420': '+420',
} as const;

which is wrong.

What is the reason to have properties with the same (when Pascal-cased) name in the model? If only one of them is used, it is probably advisable to either remove them from the schema or filter them out in the input transformer when generating

@soartec-lab
Copy link
Member

Perhaps this issue was fixed in #1958? so i'll close this.

@PrinceLoren
Copy link

Just encountered this problem on @7.7.0

@AllieJonsson
Copy link
Contributor

Perhaps this issue was fixed in #1958? so i'll close this.

@soartec-lab that only fixed the error message 😅

@melloware melloware reopened this Mar 17, 2025
@vdsbenoit
Copy link

vdsbenoit commented Mar 17, 2025

Same issue with Orval 7.7.0 (and v6 too).

The issue occurs only if output:schemas is set. When I comment this setting, Orval processes my openapi.yaml file. However, in my <project_name>.schemas.ts file, I have duplicate types. For instance :

export type POSTprofilesBody = {
  first_name?: string;
  last_name?: string;
  email?: string;
};

export type POSTprofilesBody = {
  '*'?: POSTprofilesBody;
};

I need to manually delete the second one to get it working.

@vdsbenoit
Copy link

I found out it comes from a request that have a "*" root key. For instance :

requestBody:
  required: false
  content:
    application/json:
      schema:
        type: object
        properties:
          '*':
            type: object
            description: ''
            example: []
            properties:
              email:
                type: string
                description: ''
                example: [email protected]
              name:
                type: string
                description: ''
                example: vega

It looks like this in the swagger ui

Image

This kind of layout is generated by Laravel when a bulk request is possible.

I'm not sure whether this use case should be supported by Orval or if I should change some config in the export of my Laravel API.

@vdsbenoit
Copy link

I fixed it on my side by patching the OpenAPI specification file. With the example from my previous comment, it would look like this after the patch is applied :

requestBody:
  required: false
  content:
    application/json:
      schema:
        type: object
        description: ''
        example: []
        properties:
          email:
            type: string
            description: ''
            example: [email protected]
          name:
            type: string
            description: ''
            example: vega

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants