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

Add support for discriminator on native enums properties #210

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

shaikegross
Copy link

Currently discriminating on a field, which changes between different NativeEnum types causes model definition to be incorrect as mapping from discriminator field doesn't map to each of the enum values.

I used the same solution that existed for ZodEnum, and applied it for ZodNativeEnum

shaikegross and others added 9 commits November 6, 2023 13:41
…[command]

Options:
  -V, --version                      output the version number
  -d, --sdkDir <sdkDir>              sdkDir, default: process.cwd()/service
  -t, --templatePath <templatePath>  templatePath
  -t, --type <type>                  ts/js, default ts
  -c, --camelCase <camelCase>        filename style, true 为大驼峰,lower 为小驼峰
  -d, --sdkDir <sdkDir>              sdkDir, default: process.cwd()/service
  -t, --templatePath <templatePath>  templatePath
  -t, --type <type>                  ts/js, default ts
  -c, --camelCase <camelCase>        filename style, true 为大驼峰,lower 为小驼峰
  -h, --help                         output usage information

Commands:
  url <url>                          swagger2/oas3 json data url
  data <filePath>                    swagger2/oas3 json data file
  config <cfgPath>                   config path
  *
…ject

added format: date-time to  for  in Usage: openapi-generator [options…
removed old change I made on dates
Copy link
Collaborator

@AGalabov AGalabov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shaikegross thank you for this PR - left a small question in the code. Additionally:

  1. In this repo we try our best to keep everything new tested - so would you be open to adding a test for that scenario - there is a discriminated-union.spec.ts file where we have a z.enum test that you can use as a base.

  2. Your build is failing - you have a typescript error.

@@ -727,6 +727,13 @@ export class OpenAPIGenerator {
return;
}

if (isZodType(value, 'ZodNativeEnum')) {
Object.values(value._def.values).forEach((enumValue: string) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need Object.values here as opposed to the direct usage of value._def.values above?

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

Successfully merging this pull request may close these issues.

None yet

2 participants