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

Enum components are being generated as nullable when the property holding it is nullable #258

Open
renke0 opened this issue Sep 18, 2024 · 1 comment

Comments

@renke0
Copy link

renke0 commented Sep 18, 2024

Description

I have the following scenario:

const Brand = z.enum(['Ford', 'Toyota', 'Tesla']).openapi('Brand');

const Car = z
    .object({
        brand: Brand.nullable(),
        model: z.string(),
    })
    .openapi('Car');

Expected result

I expected the property Car.brand to be a nullable Brand

components:
  schemas:
    Brand:
      type: string
      enum:
        - Ford
        - Toyota
        - Tesla
    Car:
      type: object
      properties:
        brand:
          oneOf:
            - type: 'null'
            - $ref: '#/components/schemas/Brand'
        model:
          type: string

Actual result

The spec generated declares Brand as nullable, instead of making the property Car.brand nullable

components:
  schemas:
    Brand:
      type:
        - string
        - 'null'
      enum:
        - Ford
        - Toyota
        - Tesla
    Car:
      type: object
      properties:
        brand:
          $ref: '#/components/schemas/Brand'
        model:
          type: string

More info

I understand the rationale behind the way this was generated. The problem is that Brand could be reusable, and in some places it might not be nullable. Is there a workaround this?

@AGalabov
Copy link
Collaborator

AGalabov commented Oct 4, 2024

@renke0 I can see the problem. If you pass Brand before Car in the OpenApiGeneratorV3 constructor's array of definitions everything works fine. But if you switch their order or simply do not pass Brand at all then what you've described happens.

I am not sure how easy it would be for us to fix this and when we will have the time for it. In the mean time - you can make sure to pass it in order as a workaround.

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

No branches or pull requests

2 participants