Fix bug with zod ordering and default/optional methods - #324
Merged
Conversation
Collaborator
|
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));
- }
} |
david-crespo
reviewed
Jan 9, 2026
david-crespo
reviewed
Jan 9, 2026
| // Only emit .default(null) if the schema is nullable to avoid runtime errors | ||
| if (defaultValue === null && !schema.nullable) { | ||
| return ""; | ||
| } |
Collaborator
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.