Skip to content

Commit 52bb50f

Browse files
committed
Fix for headers being overwritten by query
1 parent 0d9fa40 commit 52bb50f

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/openapi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export function toOpenAPISchema(
345345

346346
// Handle header parameters
347347
if (hooks.headers) {
348-
const headers = unwrapReference(unwrapSchema(hooks.query, vendors), definitions)
348+
const headers = unwrapReference(unwrapSchema(hooks.headers, vendors), definitions)
349349

350350
if (headers && headers.type === 'object' && headers.properties) {
351351
const required = headers.required || []

test/openapi.test.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect } from 'bun:test'
2-
import { getPossiblePath } from '../src/openapi'
2+
import { Elysia, t } from 'elysia'
3+
import { getPossiblePath, toOpenAPISchema } from '../src/openapi'
34

45
describe('OpenAPI utilities', () => {
56
it('getPossiblePath', () => {
@@ -12,3 +13,54 @@ describe('OpenAPI utilities', () => {
1213
])
1314
})
1415
})
16+
17+
describe('Convert Elysia routes to OpenAPI 3.0.3 paths schema', () => {
18+
describe('with path, header, query and cookie params', () => {
19+
const app = new Elysia().get('/', () => 'hi', {
20+
response: t.String({ description: 'sample description' }),
21+
headers: t.Object({
22+
testheader: t.String()
23+
}),
24+
params: t.Object({
25+
testparam: t.String()
26+
}),
27+
query: t.Object({
28+
testquery: t.String()
29+
}),
30+
cookie: t.Cookie({
31+
testcookie: t.String()
32+
})
33+
})
34+
35+
const {
36+
paths: { ['/']: path }
37+
} = toOpenAPISchema(app)
38+
39+
const parameters = path?.get?.parameters ?? []
40+
41+
it('includes all expected parameters', () => {
42+
const names = parameters.map((p: any) => p.name)
43+
expect(names).toEqual(
44+
expect.arrayContaining([
45+
'testheader',
46+
'testparam',
47+
'testquery',
48+
'testcookie'
49+
])
50+
)
51+
expect(names).toHaveLength(4)
52+
})
53+
54+
it('marks each parameter with the correct OpenAPI parameter location', () => {
55+
const map = Object.fromEntries(
56+
parameters.map((p: any) => [p.name, p.in])
57+
)
58+
expect(map).toMatchObject({
59+
testheader: 'header',
60+
testparam: 'path',
61+
testquery: 'query',
62+
testcookie: 'cookie'
63+
})
64+
})
65+
})
66+
})

0 commit comments

Comments
 (0)