diff --git a/apps/example-todo-app/pages/api/todo/add.ts b/apps/example-todo-app/pages/api/todo/add.ts index af84a5829..1109c1558 100644 --- a/apps/example-todo-app/pages/api/todo/add.ts +++ b/apps/example-todo-app/pages/api/todo/add.ts @@ -27,6 +27,7 @@ export const jsonBody = z.object({ z.union([z.string().max(500), z.boolean(), z.null()]) ) .optional(), + testNull: z.null().optional(), }) export const route_spec = checkRouteSpec({ diff --git a/apps/example-todo-app/tests/openapi-generation/openapi-generation.test.ts b/apps/example-todo-app/tests/openapi-generation/openapi-generation.test.ts index 359e3c4e2..254df3850 100644 --- a/apps/example-todo-app/tests/openapi-generation/openapi-generation.test.ts +++ b/apps/example-todo-app/tests/openapi-generation/openapi-generation.test.ts @@ -47,3 +47,19 @@ test("generateOpenAPI marks properties with null unions as nullable", async (t) ) ) }) + +test("generateOpenAPI marks null params with nullable flag", async (t) => { + const openapiJson = JSON.parse( + await generateOpenAPI({ + packageDir: ".", + }) + ) + + const routeSpec = openapiJson.paths["/api/todo/add"].post + const testNullParam = + routeSpec.requestBody.content["application/json"].schema.properties.testNull + + t.truthy(testNullParam) + t.falsy(testNullParam.type) + t.true(testNullParam.nullable) +}) diff --git a/packages/nextlove/src/generators/lib/zod-openapi.ts b/packages/nextlove/src/generators/lib/zod-openapi.ts index a08e28f6d..3ccd7c24f 100644 --- a/packages/nextlove/src/generators/lib/zod-openapi.ts +++ b/packages/nextlove/src/generators/lib/zod-openapi.ts @@ -342,7 +342,7 @@ function parseDate({ zodRef, schemas }: ParsingArgs): SchemaObject { function parseNull({ zodRef, schemas }: ParsingArgs): SchemaObject { return merge( { - type: "null" as SchemaObjectType, + nullable: true, }, parseDescription(zodRef), ...schemas