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

Referencing combined enums results in a duplicate schema name error #1419

Closed
RinseV opened this issue May 31, 2024 · 4 comments · Fixed by #1488
Closed

Referencing combined enums results in a duplicate schema name error #1419

RinseV opened this issue May 31, 2024 · 4 comments · Fixed by #1488
Labels
bug Something isn't working
Milestone

Comments

@RinseV
Copy link
Contributor

RinseV commented May 31, 2024

What are the steps to reproduce this issue?

  1. Define an enum in your schema that uses oneOf/anyOf/allOf with 2 or more referenced enums like so:
openapi: 3.0.3
info:
  title: Combined enums
  version: 1.0.0
paths:
  /api/colors:
    get:
      summary: sample colors
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ColorObject'
components:
    schemas:
      Colors1:
        type: string
        enum: [red, blue, yellow]
      Colors2:
        type: string
        enum: [green, purple, orange]
      Colors:
        oneOf:
          - $ref: '#/components/schemas/Colors1'
          - $ref: '#/components/schemas/Colors2'
      ColorObject:
        type: object
        properties:
          color:
            $ref: '#/components/schemas/Colors'
  1. Generate Orval code for said schema

What happens?

You'll get an error saying: Error: Duplicate schema names detected: 1x Colors

What were you expecting to happen?

Orval would generate a model for the combined enum:

export type Colors = (typeof Colors)[keyof typeof Colors];

// eslint-disable-next-line @typescript-eslint/no-redeclare
export const Colors = { ...Colors1, ...Colors2 } as const;

Any logs, error output, etc?

combined-enums - Error: Duplicate schema names detected:
  1x Colors
    at writeSchemas (orval/packages/core/dist/index.js:47446:13)
    at async Promise.all (index 0)
    at async writeSpecs (orval/packages/orval/dist/bin/orval.js:4626:5)
    at async generateSpec (orval/packages/orval/dist/bin/orval.js:4763:3)
    at async orval/packages/orval/dist/bin/orval.js:4786:18
    at async asyncReduce (orval/packages/core/dist/index.js:44239:18)
    at async generateConfig (/orval/packages/orval/dist/bin/orval.js:4830:5)
    at async CAC.<anonymous> (orval/packages/orval/dist/bin/orval.js:4887:5)

Any other comments?

This seems to happen specifically if you reference a combined enum, if you declare them "inline" as a property it works fine.
Works (if you leave out the Colors definition):

ColorsObject:
  type: object
  properties:
    color:
      oneOf:
        - $ref: '#/components/schemas/Colors1'
        - $ref: '#/components/schemas/Colors2'

Doesn't work:

ColorsObject
  type: object
  properties:
    color:
      $ref: '#/components/schemas/Colors'

This worked fine in any version < 6.29.0, but 6.29.0 introduced a change that broke this behaviour.

What versions are you using?

Operating System: MacOS Sonoma 14.5
Package Version: 6.29.1
Browser Version: Firefox 126.0.1

@melloware melloware added the bug Something isn't working label May 31, 2024
@melloware
Copy link
Collaborator

@RinseV can you look through the issue in 6.29.0 and see what broke it: https://github.com/anymaniax/orval/issues?q=is%3Aclosed+milestone%3A6.29.0

Let me know so we can notify the dev who fixed it of this new issue.

@RinseV
Copy link
Contributor Author

RinseV commented May 31, 2024

@melloware This PR seems to have broken the functionality, specifically this change with schemas. Removing this part:

{
  imports: [],
  model: newEnum,
  name: pascal(name),
},

fixes the problem. I'm guessing that it's creating a new enum definition every time the combined enum is being referenced, resulting in duplicates, but I could be wrong.

@melloware
Copy link
Collaborator

@arthurfiorette your PR introduced a regression.

@tw99
Copy link

tw99 commented May 31, 2024

Noticing the same issue. Pinned 6.28.2 for now until a patch is released.

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
3 participants