-
Notifications
You must be signed in to change notification settings - Fork 3
Fix bug with zod ordering and default/optional methods #324
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
Conversation
|
I think you can do without diff --git a/oxide-openapi-gen-ts/src/schema/zod.ts b/oxide-openapi-gen-ts/src/schema/zod.ts
index f240fc8e50..7fe7a88555 100644
--- a/oxide-openapi-gen-ts/src/schema/zod.ts
+++ b/oxide-openapi-gen-ts/src/schema/zod.ts
@@ -85,11 +85,9 @@
},
integer(schema, io) {
- schemaToZodInt(schema, io, { skipDefault: schema.nullable });
- if (schema.nullable) {
- io.w0(".nullable()");
- io.w0(getDefaultString(schema));
- }
+ schemaToZodInt(schema, io);
+ if (schema.nullable) io.w0(".nullable()");
+ io.w0(getDefaultString(schema));
},
array(schema, io) {
@@ -201,11 +199,7 @@
},
});
-function schemaToZodInt(
- schema: OpenAPIV3.SchemaObject,
- { w0 }: IO,
- options?: { skipDefault?: boolean }
-) {
+function schemaToZodInt(schema: OpenAPIV3.SchemaObject, { w0 }: IO) {
if ("enum" in schema) {
/** See comment in {@link setupZod} */
w0(`IntEnum(${JSON.stringify(schema.enum)} as const)`);
@@ -230,8 +224,4 @@
// It's signed so remove the most significant bit
w0(`.max(${Math.pow(2, parseInt(size) - 1) - 1})`);
}
-
- if (!options?.skipDefault) {
- w0(getDefaultString(schema));
- }
} |
| // Only emit .default(null) if the schema is nullable to avoid runtime errors | ||
| if (defaultValue === null && !schema.nullable) { | ||
| return ""; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't happen in our spec and the schema generator probably wouldn't let it happen, but I suppose it's fine to be defensive. Plus you can manually build a schema in the API if you really want to.
There were two issues I discovered when running this generator against https://github.com/oxidecomputer/console. This PR addresses them and adds a few tests.
First, a commit I made yesterday introduced an ordering bug, where zod schemas had a
default()dropped in before the constraints (min,max), as in here:-"mvlan": z.number().default(null).min(0).max(65535).nullable().optional(),This is now fixed so that the default gets added after the constraints:
+"mvlan": z.number().min(0).max(65535).nullable().default(null),Second, the generator shouldn't generate both
defaultandoptionalon the same record.