Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow openapi schema #128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
} from 'fastify'
import type { FastifySerializerCompiler } from 'fastify/types/schema'
import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'
import type { z } from 'zod'
import { ZodSchema, type z } from 'zod'

import { InvalidSchemaError, ResponseSerializationError, createValidationError } from './errors'
import { resolveRefs } from './ref'
Expand Down Expand Up @@ -70,10 +70,14 @@ export const createJsonSchemaTransform = ({ skipList }: { skipList: readonly str

// eslint-disable-next-line @typescript-eslint/no-explicit-any
for (const prop in response as any) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const schema = resolveSchema((response as any)[prop])
const responseSchema = response[prop]

const transformedResponse = convertZodToJsonSchema(schema)
if (responseSchema instanceof ZodSchema === false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use typeguard instead to make it work across realms?

transformed.response[prop] = responseSchema
continue
}

const transformedResponse = convertZodToJsonSchema(responseSchema)
transformed.response[prop] = transformedResponse
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/__snapshots__/fastify-swagger.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,37 @@ exports[`transformer > generates types for fastify-swagger correctly 1`] = `
},
"description": "Default Response",
},
"201": {
"content": {
"application/json": {
"schema": {
"description": "Successful response",
"properties": {
"hello": {
"type": "string",
},
},
"type": "object",
},
},
},
"description": "Successful response",
"headers": {
"Set-Cookie": {
"description": "Cookies for session and refresh tokens",
"schema": {
"items": {
"example": [
"refreshToken=tokenValue; Path=/; HttpOnly; Secure; SameSite=Strict",
"sessionToken=tokenValue; Path=/; HttpOnly; Secure; SameSite=Strict",
],
"type": "string",
},
"type": "array",
},
},
},
},
"401": {
"content": {
"application/json": {
Expand Down
20 changes: 20 additions & 0 deletions test/fastify-swagger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ describe('transformer', () => {
response: {
200: z.string(),
401: UNAUTHORIZED_SCHEMA,
201: {
description: 'Successful response',
type: 'object',
properties: {
hello: { type: 'string' },
},
headers: {
'Set-Cookie': {
type: 'array',
items: {
type: 'string',
example: [
'refreshToken=tokenValue; Path=/; HttpOnly; Secure; SameSite=Strict',
'sessionToken=tokenValue; Path=/; HttpOnly; Secure; SameSite=Strict',
],
},
description: 'Cookies for session and refresh tokens',
},
},
},
},
},
handler: (req, res) => {
Expand Down
Loading