Skip to content
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
2 changes: 1 addition & 1 deletion src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ export function toOpenAPISchema(

// Handle header parameters
if (hooks.headers) {
const headers = unwrapReference(unwrapSchema(hooks.query, vendors), definitions)
const headers = unwrapReference(unwrapSchema(hooks.headers, vendors), definitions)

if (headers && headers.type === 'object' && headers.properties) {
const required = headers.required || []
Expand Down
54 changes: 53 additions & 1 deletion test/openapi.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect } from 'bun:test'
import { getPossiblePath } from '../src/openapi'
import { Elysia, t } from 'elysia'
import { getPossiblePath, toOpenAPISchema } from '../src/openapi'

describe('OpenAPI utilities', () => {
it('getPossiblePath', () => {
Expand All @@ -12,3 +13,54 @@ describe('OpenAPI utilities', () => {
])
})
})

describe('Convert Elysia routes to OpenAPI 3.0.3 paths schema', () => {
describe('with path, header, query and cookie params', () => {
const app = new Elysia().get('/', () => 'hi', {
response: t.String({ description: 'sample description' }),
headers: t.Object({
testheader: t.String()
}),
params: t.Object({
testparam: t.String()
}),
query: t.Object({
testquery: t.String()
}),
cookie: t.Cookie({
testcookie: t.String()
})
})

const {
paths: { ['/']: path }
} = toOpenAPISchema(app)

const parameters = path?.get?.parameters ?? []

it('includes all expected parameters', () => {
const names = parameters.map((p: any) => p.name)
expect(names).toEqual(
expect.arrayContaining([
'testheader',
'testparam',
'testquery',
'testcookie'
])
)
expect(names).toHaveLength(4)
})

it('marks each parameter with the correct OpenAPI parameter location', () => {
const map = Object.fromEntries(
parameters.map((p: any) => [p.name, p.in])
)
expect(map).toMatchObject({
testheader: 'header',
testparam: 'path',
testquery: 'query',
testcookie: 'cookie'
})
})
})
})