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(mock): null is excluded instead of being converted to an empty string '' #1166

Merged

Conversation

soartec-lab
Copy link
Member

Status

READY

Description

If null exists in enum, the generated enum type will not include null or the empty string ''.
For example bellow generated by https://github.com/anymaniax/orval/blob/master/tests/specifications/null-type-v3-0.yaml#L91

export type NullableStringEnum = typeof NullableStringEnum[keyof typeof NullableStringEnum] | null;

export const NullableStringEnum = {
  ONE: 'ONE',
  TWO: 'TWO',
  THREE: 'THREE',
} as const;

Therefore, they are unnecessary, including the value patterns generated by mock. In fact, specifying the type causes an error.

export const getFetchNullableEnumsMock = () => (
  faker.helpers.arrayElement([faker.helpers.arrayElement(['ONE','TWO','THREE', ''] as const), null])
)
const mock: NullableStringEnum = getFetchNullableEnumsMock()
// => error TS2322: Type '"" | "ONE" | "TWO" | "THREE" | null' is not assignable to type 'NullableStringEnum'.
//    Type '""' is not assignable to type 'NullableStringEnum'.

So I fixed it so that it doesn't contain the empty string ''''.

export const getFetchNullableEnumsMock = () => (
  faker.helpers.arrayElement([faker.helpers.arrayElement(['ONE','TWO','THREE'] as const), null])
)
const mock: NullableStringEnum = getFetchNullableEnumsMock()
// => not error

Related PRs

none

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

  1. cd test
  2. yarn generate:default
  3. Check the getFetchNullableEnumsMock function in tests/generated/default/null-type-v3-0/endpoints.ts

@soartec-lab soartec-lab force-pushed the fix/mock-sample-with-nullable-enum branch from 36bc45c to d3b54e0 Compare January 22, 2024 09:22
@melloware melloware merged commit c41635b into orval-labs:master Jan 22, 2024
2 checks passed
@soartec-lab soartec-lab added the bug Something isn't working label Jan 28, 2024
@soartec-lab soartec-lab added this to the 6.24.0 milestone Jan 28, 2024
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

Successfully merging this pull request may close these issues.

2 participants