-
Notifications
You must be signed in to change notification settings - Fork 87
Fix for headers being overwritten by query #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix for headers being overwritten by query #254
Conversation
WalkthroughFixes header parameter extraction in toOpenAPISchema to use hooks.headers instead of hooks.query. Adds a test that builds an Elysia app and asserts generated OpenAPI parameters are correctly placed in header, path, query, and cookie. Changes
Sequence Diagram(s)Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal). Please share your feedback with us on this Discord post. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
test/openapi.test.ts (2)
17-66
: Strengthen assertions and add a guard for the “headers-only” caseAdd checks for required flags and ensure GET has no requestBody. Also add a dedicated test where only headers are provided to prevent regressions reported in 1.4.*.
Apply:
@@ describe('Convert Elysia routes to OpenAPI 3.0.3 paths schema', () => { describe('with path, header, query and cookie params', () => { @@ - it('marks each parameter with the correct OpenAPI parameter location', () => { + 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' }) }) + + it('marks parameters as required based on schema', () => { + const reqMap = Object.fromEntries( + parameters.map((p: any) => [p.name, Boolean(p.required)]) + ) + expect(reqMap).toMatchObject({ + testheader: true, + testparam: true, + testquery: true, + testcookie: true + }) + }) + + it('does not emit requestBody for GET', () => { + expect(path?.get?.requestBody).toBeUndefined() + }) }) + + describe('with only header params', () => { + const app2 = new Elysia().get('/only-header', () => 'hi', { + headers: t.Object({ only: t.String() }) + }) + const { + paths: { ['/only-header']: onlyHeaderPath } + } = toOpenAPISchema(app2) + + it('includes header parameters even when no query is present', () => { + const params = onlyHeaderPath?.get?.parameters ?? [] + const names = params.map((p: any) => p.name) + expect(names).toEqual(expect.arrayContaining(['only'])) + expect(params.find((p: any) => p.name === 'only')?.in).toBe('header') + }) + }) })
6-14
: Optional: de-duplicate getPossiblePath outputsThe helper currently returns duplicates (e.g., '/user/name' twice). If stability matters, consider returning unique paths and adjusting this expectation accordingly.
Proposed change in src/openapi.ts:
export const getPossiblePath = (path: string): string[] => { const optionalParams = path.match(optionalParamsRegex) if (!optionalParams) return [path] const originalPath = path.replace(/\?/g, '') - const paths = [originalPath] + const paths = new Set<string>([originalPath]) for (let i = 0; i < optionalParams.length; i++) { const newPath = path.replace(optionalParams[i], '') - paths.push(...getPossiblePath(newPath)) + for (const p of getPossiblePath(newPath)) paths.add(p) } - return paths + return [...paths] }And update the test expectation to remove the duplicate '/user/name'.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/openapi.ts
(1 hunks)test/openapi.test.ts
(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
test/openapi.test.ts (1)
src/openapi.ts (1)
toOpenAPISchema
(105-498)
🔇 Additional comments (1)
src/openapi.ts (1)
245-261
: Fix correctly sources header schema from hooks.headersThis resolves the overwrite bug where headers were derived from query. Good catch.
1c8020c
to
52bb50f
Compare
This afternoon we noticed an issue where header validation was being overwritten by query validation. Specifically:
This PR fixes the issue and adds targeted tests to prevent regression.
Steps to replicate
Summary by CodeRabbit
Bug Fixes
Tests