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

Ability to use OpenAPI's 'default' with generated Zod schema object #1922

Closed
marty0678 opened this issue Feb 21, 2025 · 1 comment · Fixed by #1924
Closed

Ability to use OpenAPI's 'default' with generated Zod schema object #1922

marty0678 opened this issue Feb 21, 2025 · 1 comment · Fixed by #1924
Labels
zod Zod related issue
Milestone

Comments

@marty0678
Copy link

Feature request to add the ability to use the default property from an OpenAPI schema property, to apply that value to the Zod schema object. For example a property of:

...
properties:
    exampleField:
      type: string
      maxLength: 255
      default: "Hello World"
...

Would generate a Zod object with the following property (assuming the above schema was used in a list endpoint):

export const exampleListResponseResultsItemExampleResponseMax = 255;
export const exampleListResponseResultsItemExampleResponseDefault = "Hello World";

export const exampleListResponse = zod.object({
  "exampleField": zod.string().max(exampleListResponseResultsItemExampleResponseMax ).optional().default(exampleListResponseResultsItemExampleResponseDefault),
...

To maintain backward compatibility, it could be behind a config option, at the most basic level a bool:

 module.exports = {
   petstore: {
     output: {
       ...
       override: {
         zod: {
           default: true, // Default: false
         },
       },
     },
     ...
   },
 };

Thank you for this great library!

@melloware melloware added the zod Zod related issue label Feb 21, 2025
@melloware melloware added this to the 7.6.0 milestone Feb 21, 2025
@marty0678
Copy link
Author

Thanks so much for the fast PR on this, that's super appreciated! I gave it a shot tonight and it works great thank you!

I did want to report two possible issues, keeping in mind I manually packed the various packages and locally imported them, so there is a chance I messed something up in that process, but figured I'd report them anyway for now:

  1. Looks like there are new lines missing in the generated TS file. For example in one of my schemas it comes out to:
export const apiUserFilesSaveFileAmplifierPortCreateBodySecondaryColourMax = 7;
export const apiUserFilesSaveFileAmplifierPortCreateBodyCategoryDefault = "CAMR";export const apiUserFilesSaveFileAmplifierPortCreateBodyIpAddressDefault = "192.168.86.101";export const apiUserFilesSaveFileAmplifierPortCreateBodyJointPortGroupMax = 255;
export const apiUserFilesSaveFileAmplifierPortCreateBodySortOrderMin = 0;
  1. On one of my large schemas, I am getting a call stack size exceeded error trying to generate it now, if mocks are enabled. It works when mocks are disabled or when using 7.5.0. A simpler schema such as petstore works no problem however.

This one I'm more inclined to think I messed something up with the local packaging so feel free to ignore this one and I can re-test when 7.6.0 is actually released, but if not, I am happy to share my schema privately.

🛑 schema - RangeError: Maximum call stack size exceeded
    at {{projectDirectory}}\node_modules\.pnpm\@orval+mock@file+lib+orval+orval-mock-7.6.0pre.tgz\node_modules\@orval\mock\dist\index.js:183:16
    at Array.map (<anonymous>)
    at getMockObject ({{projectDirectory}}\node_modules\.pnpm\@orval+mock@file+lib+orval+orval-mock-7.6.0pre.tgz\node_modules\@orval\mock\dist\index.js:159:36)
    at getMockScalar ({{projectDirectory}}\node_modules\.pnpm\@orval+mock@file+lib+orval+orval-mock-7.6.0pre.tgz\node_modules\@orval\mock\dist\index.js:419:14)
    at resolveMockValue ({{projectDirectory}}\node_modules\.pnpm\@orval+mock@file+lib+orval+orval-mock-7.6.0pre.tgz\node_modules\@orval\mock\dist\index.js:543:21)
    at getMockScalar ({{projectDirectory}}\node_modules\.pnpm\@orval+mock@file+lib+orval+orval-mock-7.6.0pre.tgz\node_modules\@orval\mock\dist\index.js:358:11)
    at resolveMockValue ({{projectDirectory}}\node_modules\.pnpm\@orval+mock@file+lib+orval+orval-mock-7.6.0pre.tgz\node_modules\@orval\mock\dist\index.js:593:18)
    at {{projectDirectory}}\node_modules\.pnpm\@orval+mock@file+lib+orval+orval-mock-7.6.0pre.tgz\node_modules\@orval\mock\dist\index.js:169:29
    at Array.map (<anonymous>)
    at getMockObject ({{projectDirectory}}\node_modules\.pnpm\@orval+mock@file+lib+orval+orval-mock-7.6.0pre.tgz\node_modules\@orval\mock\dist\index.js:159:36)

The line in question is from getMockObject and is:

imports2.push(...resolvedValue.imports);

Neither are a big deal however for me personally, but wanted to report just in case. Thank you again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
zod Zod related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants