Skip to content

Commit

Permalink
fix: OpenAPI generation: null params should be flagged with `nullab…
Browse files Browse the repository at this point in the history
…le` (#147)

* Null params should be flagged with nullable

* Add test
  • Loading branch information
andrii-balitskyi authored Jun 28, 2024
1 parent 6b87971 commit b038e18
Show file tree
Hide file tree
Showing 3 changed files with 18 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 @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
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 @@ -342,7 +342,7 @@ function parseDate({ zodRef, schemas }: ParsingArgs<z.ZodDate>): SchemaObject {
function parseNull({ zodRef, schemas }: ParsingArgs<z.ZodNull>): SchemaObject {
return merge(
{
type: "null" as SchemaObjectType,
nullable: true,
},
parseDescription(zodRef),
...schemas
Expand Down

0 comments on commit b038e18

Please sign in to comment.