Skip to content

Commit

Permalink
fix(zod-openapi): use JSONParsed for creating a response type (#576)
Browse files Browse the repository at this point in the history
* fix(zod-openapi): use `JSONParsed` for creating a response type

* add changeset
  • Loading branch information
yusukebe authored Jun 14, 2024
1 parent 6b06138 commit 9a9de50
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-pears-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

fix: use `JSONParsed` for creating a response type
4 changes: 2 additions & 2 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
TypedResponse,
} from 'hono'
import type { MergePath, MergeSchemaPath } from 'hono/types'
import type { JSONParsed, RemoveBlankRecord } from 'hono/utils/types'
import type {
ClientErrorStatusCode,
InfoStatusCode,
Expand All @@ -35,7 +36,6 @@ import type {
StatusCode,
SuccessStatusCode,
} from 'hono/utils/http-status'
import type { RemoveBlankRecord } from 'hono/utils/types'
import { mergePath } from 'hono/utils/url'
import type { AnyZodObject, ZodError, ZodSchema } from 'zod'
import { ZodType, z } from 'zod'
Expand Down Expand Up @@ -167,7 +167,7 @@ export type RouteConfigToTypedResponse<R extends RouteConfig> = {
> extends never
? TypedResponse<{}, ExtractStatusCode<Status>, string>
: TypedResponse<
ExtractContent<R['responses'][Status]['content']>,
JSONParsed<ExtractContent<R['responses'][Status]['content']>>,
ExtractStatusCode<Status>,
'json'
>
Expand Down
31 changes: 29 additions & 2 deletions packages/zod-openapi/test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Hono, Env, ToSchema } from 'hono'
import { describe, it, expectTypeOf, assertType } from 'vitest'
import type { Env, Hono, ToSchema } from 'hono'
import { assertType, describe, expectTypeOf, it } from 'vitest'
import { OpenAPIHono, createRoute, z } from '../src/index'

describe('Types', () => {
Expand Down Expand Up @@ -171,3 +171,30 @@ describe('Input types', () => {
})
})
})

describe('Response schema includes a Date type', () => {
it('Should not throw a type error', () => {
new OpenAPIHono().openapi(
createRoute({
method: 'get',
path: '/example',
responses: {
200: {
content: {
'application/json': {
schema: z.object({
updatedAt: z.date(),
}),
},
},
description: '',
},
},
}),
async (ctx) => {
// Don't throw an error:
return ctx.json({ updatedAt: new Date() }, 200)
}
)
})
})

0 comments on commit 9a9de50

Please sign in to comment.