From 52bb50f7663e20249b6f00f640f8ddad528efd42 Mon Sep 17 00:00:00 2001 From: Han Date: Mon, 15 Sep 2025 17:52:20 +0100 Subject: [PATCH] Fix for headers being overwritten by query --- src/openapi.ts | 2 +- test/openapi.test.ts | 54 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/openapi.ts b/src/openapi.ts index 328e27b..b21fa61 100644 --- a/src/openapi.ts +++ b/src/openapi.ts @@ -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 || [] diff --git a/test/openapi.test.ts b/test/openapi.test.ts index 1a54bad..d84f5d5 100644 --- a/test/openapi.test.ts +++ b/test/openapi.test.ts @@ -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', () => { @@ -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' + }) + }) + }) +})