Skip to content

Commit

Permalink
Fix property content type wrongfully appearing in response schema (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Mar 14, 2024
1 parent 0f02a3b commit 2d9d2bf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export function OpenAPIRouter<
RequestType = IRequest,
Args extends any[] = any[],
RouteType = Equal<RequestType, IRequest> extends true
? Route
: UniversalRoute<RequestType, Args>
? Route
: UniversalRoute<RequestType, Args>
>(options?: RouterOptions): OpenAPIRouterType<RouteType, Args> {
const registry: OpenAPIRegistryMerger = new OpenAPIRegistryMerger()

Expand Down
3 changes: 3 additions & 0 deletions src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export class OpenAPIRoute<I = IRequest, A extends any[] = any[]> {
}

delete value.schema
if (value.contentType) {
delete value.contentType
}
}

if (value.headers && !isAnyZodType(value.headers)) {
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/openapi-schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'isomorphic-fetch'
import { buildRequest, findError } from '../utils'
import { ToDoList, todoRouter } from '../router'

describe('openapi schema', () => {
test('custom content type', async () => {
const request = await todoRouter.handle(
buildRequest({ method: 'GET', path: '/openapi.json' })
)
const resp = await request.json()
const respSchema = resp.paths['/contenttype'].get.responses[200]

expect(respSchema.contentType).toBeUndefined()
expect(respSchema.content).toEqual({
'text/csv': {
schema: {
type: 'string',
},
},
})
})
})
22 changes: 22 additions & 0 deletions tests/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ export class ToDoGet extends OpenAPIRoute {
}
}

export class ContentTypeGet extends OpenAPIRoute {
static schema = {
responses: {
'200': {
description: 'Successful Response',
contentType: 'text/csv',
schema: new Str(),
},
},
}

async handle(request: Request, env: any, context: any, data: any) {
return {
todo: {
lorem: 'lorem',
ipsum: 'ipsum',
},
}
}
}

export class ToDoCreate extends OpenAPIRoute {
static schema: OpenAPIRouteSchema = {
tags: ['ToDo'],
Expand Down Expand Up @@ -147,6 +168,7 @@ export const todoRouter = OpenAPIRouter({ openapiVersion: '3' })
todoRouter.get('/todos', ToDoList)
todoRouter.get('/todos/:id', ToDoGet)
todoRouter.post('/todos', ToDoCreate)
todoRouter.get('/contenttype', ContentTypeGet)

// 404 for everything else
todoRouter.all('*', () => new Response('Not Found.', { status: 404 }))

0 comments on commit 2d9d2bf

Please sign in to comment.