Skip to content

Commit

Permalink
fix(zod-openapi): Fix a bug that slashes were duplicated when mountin… (
Browse files Browse the repository at this point in the history
#258)

* fix(zod-openapi): Fix a bug that slashes were duplicated when mounting a path using the route method

* fix: Make the paths be merged using internally defined utility functions instead
  • Loading branch information
Karibash authored Nov 16, 2023
1 parent 53cd5f7 commit 368c352
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-rice-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

Fix a bug that slashes were duplicated when mounting a path using the route method
5 changes: 3 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 {
} from 'hono'
import type { MergePath, MergeSchemaPath } from 'hono/types'
import type { RemoveBlankRecord } from 'hono/utils/types'
import { mergePath } from 'hono/utils/url'
import type { AnyZodObject, ZodSchema, ZodError } from 'zod'
import { z, ZodType } from 'zod'

Expand Down Expand Up @@ -323,13 +324,13 @@ export class OpenAPIHono<
case 'route':
return this.openAPIRegistry.registerPath({
...def.route,
path: `${path}${def.route.path}`,
path: mergePath(path, def.route.path),
})

case 'webhook':
return this.openAPIRegistry.registerWebhook({
...def.webhook,
path: `${path}${def.webhook.path}`,
path: mergePath(path, def.webhook.path),
})

case 'schema':
Expand Down
137 changes: 137 additions & 0 deletions packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,3 +977,140 @@ describe('It allows the response type to be Response', () => {
expect(res.body).toBe(null)
})
})

describe('Path normalization', () => {
const createRootApp = () => {
const app = new OpenAPIHono()
app.doc('/doc', {
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'My API',
},
})
return app
}

const generateRoute = (path: string) => {
return createRoute({
path,
method: 'get',
responses: {
204: {
description: 'No Content',
},
},
})
}

const handler = (c) => c.body(null, 204)

describe('Duplicate slashes in the root path', () => {
const app = createRootApp()
const childApp = new OpenAPIHono()

childApp.openapi(generateRoute('/child'), handler)
app.route('/', childApp)

it('Should remove duplicate slashes', async () => {
const res = await app.request('/doc')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'My API',
},
components: {
schemas: {},
parameters: {},
},
paths: {
'/child': {
get: {
responses: {
204: {
description: 'No Content',
},
},
},
},
},
})
})
})

describe('Duplicate slashes in the child path', () => {
const app = createRootApp()
const childApp = new OpenAPIHono()
const grandchildApp = new OpenAPIHono()

grandchildApp.openapi(generateRoute('/granchild'), handler)
childApp.route('/', grandchildApp)
app.route('/api', childApp)

it('Should remove duplicate slashes', async () => {
const res = await app.request('/doc')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'My API',
},
components: {
schemas: {},
parameters: {},
},
paths: {
'/api/granchild': {
get: {
responses: {
204: {
description: 'No Content',
},
},
},
},
},
})
})
})

describe('Duplicate slashes in the trailing path', () => {
const app = createRootApp()
const childApp = new OpenAPIHono()
const grandchildApp = new OpenAPIHono()

grandchildApp.openapi(generateRoute('/'), handler)
childApp.route('/', grandchildApp)
app.route('/api', childApp)

it('Should remove duplicate slashes', async () => {
const res = await app.request('/doc')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'My API',
},
components: {
schemas: {},
parameters: {},
},
paths: {
'/api': {
get: {
responses: {
204: {
description: 'No Content',
},
},
},
},
},
})
})
})
})

0 comments on commit 368c352

Please sign in to comment.