Skip to content

Commit

Permalink
fix: OpenAPI generator: fix nullable param parsing (#145)
Browse files Browse the repository at this point in the history
* Fix nullable params parsing

* Test the fix

* Fix tests
  • Loading branch information
andrii-balitskyi authored Jun 27, 2024
1 parent 8f83afb commit d1c0af4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/example-todo-app/pages/api/todo/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const jsonBody = z.object({
---
This is an unused, deprecated field.
`),
description: z.string().nullable().optional(),
})

export const route_spec = checkRouteSpec({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,20 @@ test("should generate openapi with expected properties", async (t) => {
)
t.deepEqual(typeof openapiJson, "object")
})

test("nullable param is correctly parsed", async (t) => {
const openapiJson = JSON.parse(
await generateOpenAPI({
packageDir: ".",
})
)

const routeSpec = openapiJson.paths["/api/todo/add"].post
const descriptionParam =
routeSpec.requestBody.content["application/json"].schema.properties
.description

t.truthy(descriptionParam)
t.is(descriptionParam.type, "string")
t.true(descriptionParam.nullable)
})
2 changes: 1 addition & 1 deletion packages/nextlove/src/generators/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ function parseNullable({
}: ParsingArgs<z.ZodNullable<OpenApiZodAny>>): SchemaObject {
const schema = generateSchema(zodRef.unwrap(), useOutput)
return merge(
{ ...schema, type: [schema.type, "null"] as SchemaObjectType[] },
{ ...schema, type: schema.type, nullable: true },
parseDescription(zodRef),
...schemas
)
Expand Down

0 comments on commit d1c0af4

Please sign in to comment.