-
-
Notifications
You must be signed in to change notification settings - Fork 148
feat(openapi): cookie parameters #1582
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
Open
RafalFilipek
wants to merge
7
commits into
middleapi:main
Choose a base branch
from
RafalFilipek:feat/openapi-cookie-parameters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a76e413
docs: add design spec for OpenAPI cookie parameters support
ea302fb
docs: add implementation plan for OpenAPI cookie parameters
2658d13
test(openapi): verify toOpenAPIParameters handles cookie parameterIn
1f5b4df
feat(openapi): support cookies in inputStructure detailed generator
ad19e1d
feat(openapi): parse Cookie header into cookies in detailed input str…
92bf405
fix(openapi): handle edge cases in cookie header parsing
b7564b9
refactor(openapi): use cookie package for Cookie header parsing in codec
RafalFilipek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
436 changes: 436 additions & 0 deletions
436
docs/superpowers/plans/2026-06-11-openapi-cookie-parameters.md
Large diffs are not rendered by default.
Oops, something went wrong.
192 changes: 192 additions & 0 deletions
192
docs/superpowers/specs/2026-06-11-openapi-cookie-parameters-design.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| # OpenAPI Cookie Parameters Support | ||
|
|
||
| **Date:** 2026-06-11 | ||
| **Status:** Approved | ||
|
|
||
| ## Problem | ||
|
|
||
| oRPC's `inputStructure: 'detailed'` mode supports `params`, `query`, `headers`, and `body` as input keys. It does not support `cookies`. This means: | ||
|
|
||
| 1. **OpenAPI spec is incorrect** — no `in: cookie` parameter objects are generated, even when the user reads cookies in their handler. | ||
| 2. **Runtime validation is missing** — the `Cookie` request header is never parsed or exposed to Zod validation. | ||
|
|
||
| The OpenAPI spec supports cookie parameters as defined in [Swagger 3.0 — Cookie Parameters](https://swagger.io/docs/specification/v3_0/describing-parameters/#cookie-parameters). | ||
|
|
||
| ## Scope | ||
|
|
||
| - `inputStructure: 'detailed'` only (not `compact`) | ||
| - Server-side: spec generation + runtime decode | ||
| - No client-side changes (browsers manage cookies automatically; can be added later) | ||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| User Schema (Zod) | ||
| z.object({ session_id: z.string() }) | ||
| ↓ | ||
| inputStructure: 'detailed' input shape | ||
| { cookies: z.object({ session_id: z.string() }), ... } | ||
| ↓ | ||
| OpenAPI Generator | ||
| in: cookie → ParameterObject[] | ||
| ↓ | ||
| StandardOpenAPICodec.decode() | ||
| Cookie header → parsed key/value object → exposed as `cookies` | ||
| ↓ | ||
| Zod validation | ||
| validates cookies like any other input field | ||
| ``` | ||
|
|
||
| All changes are confined to `packages/openapi`. No changes to `packages/contract` or `packages/server`. | ||
|
|
||
| ## Changes | ||
|
|
||
| ### 1. OpenAPI Generator — `packages/openapi/src/openapi-generator.ts` | ||
|
|
||
| **Line 389 — extend the loop over input structure keys:** | ||
|
|
||
| ```ts | ||
| // Before: | ||
| for (const from of ['params', 'query', 'headers']) { | ||
| const parameterIn: 'path' | 'query' | 'header' = from === 'params' | ||
| ? 'path' | ||
| : from === 'headers' ? 'header' : 'query' | ||
| // ... | ||
| } | ||
|
|
||
| // After: | ||
| for (const from of ['params', 'query', 'headers', 'cookies']) { | ||
| const parameterIn: 'path' | 'query' | 'header' | 'cookie' = from === 'params' | ||
| ? 'path' | ||
| : from === 'headers' ? 'header' | ||
| : from === 'cookies' ? 'cookie' | ||
| : 'query' | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| **Line 366 — update the error message:** | ||
|
|
||
| ```ts | ||
| // Before: | ||
| 'When input structure is "detailed", input schema must satisfy: ' | ||
| + '{ params?: Record<string, unknown>, query?: Record<string, unknown>, headers?: Record<string, unknown>, body?: unknown }' | ||
|
|
||
| // After: | ||
| 'When input structure is "detailed", input schema must satisfy: ' | ||
| + '{ params?: Record<string, unknown>, query?: Record<string, unknown>, headers?: Record<string, unknown>, cookies?: Record<string, unknown>, body?: unknown }' | ||
| ``` | ||
|
|
||
| No changes needed to `toOpenAPIParameters()` — it already accepts `'cookie'` as a valid `parameterIn` value. | ||
|
|
||
| ### 2. Server Codec — `packages/openapi/src/adapters/standard/openapi-codec.ts` | ||
|
|
||
| **Add a cookie header parser** (private helper function or inline): | ||
|
|
||
| ```ts | ||
| function parseCookieHeader(cookieHeader: string | undefined): Record<string, string> { | ||
| if (!cookieHeader) return {} | ||
| return Object.fromEntries( | ||
| cookieHeader.split(';').map((pair) => { | ||
| const idx = pair.indexOf('=') | ||
| return [pair.slice(0, idx).trim(), pair.slice(idx + 1).trim()] | ||
| }), | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| **Extend the `decode()` return object** with a lazy `cookies` getter, consistent with the existing `query` lazy getter pattern: | ||
|
|
||
| ```ts | ||
| return { | ||
| params, | ||
| get query() { | ||
| const value = deserializeSearchParams() | ||
| Object.defineProperty(this, 'query', { value, writable: true }) | ||
| return value | ||
| }, | ||
| set query(value) { | ||
| Object.defineProperty(this, 'query', { value, writable: true }) | ||
| }, | ||
| headers: request.headers, | ||
| get cookies() { | ||
| const value = parseCookieHeader(request.headers['cookie'] as string | undefined) | ||
|
RafalFilipek marked this conversation as resolved.
|
||
| Object.defineProperty(this, 'cookies', { value, writable: true }) | ||
| return value | ||
| }, | ||
| set cookies(value) { | ||
| Object.defineProperty(this, 'cookies', { value, writable: true }) | ||
| }, | ||
| body: this.serializer.deserialize(await request.body()), | ||
| } | ||
| ``` | ||
|
|
||
| Cookie values are always `string` — the existing Zod/JSON Schema smart coercion will convert them to the required types (numbers, booleans, etc.) during validation. | ||
|
|
||
| ### 3. No changes required | ||
|
|
||
| - `packages/openapi/src/openapi-utils.ts` — `toOpenAPIParameters()` already handles `'cookie'` | ||
| - `packages/contract/src/route.ts` — `inputStructure` is a string enum; cookie support is defined by user's schema shape | ||
| - `packages/server/src/helpers/cookie.ts` — not used here; we parse the `Cookie` header directly | ||
|
|
||
| ## Tests | ||
|
|
||
| ### `packages/openapi/src/openapi-generator.test.ts` | ||
|
|
||
| - `inputStructure: 'detailed'` with `cookies` → generates `in: cookie` parameters correctly | ||
| - Error when `cookies` schema is not an object | ||
| - Combination: `cookies` + `headers` + `query` + `body` together | ||
|
|
||
| ### `packages/openapi/src/openapi-utils.test.ts` | ||
|
|
||
| - `toOpenAPIParameters` with `parameterIn: 'cookie'` — verify `style`/`explode` are NOT added (cookie parameters do not support `deepObject` style) | ||
|
|
||
| ### `packages/openapi/src/adapters/standard/openapi-codec.test.ts` | ||
|
|
||
| - `decode()` with `inputStructure: 'detailed'` and a `Cookie` request header → `cookies` key contains parsed key/value object | ||
| - `decode()` with no `Cookie` header → `cookies` is an empty object `{}` | ||
| - `decode()` with malformed `Cookie` header → graceful handling | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ## Usage Example | ||
|
|
||
| ```ts | ||
| import { os } from '@orpc/server' | ||
| import { z } from 'zod' | ||
|
|
||
| const getProfile = os | ||
| .route({ | ||
| method: 'GET', | ||
| path: '/profile', | ||
| inputStructure: 'detailed', | ||
| }) | ||
| .input( | ||
| z.object({ | ||
| cookies: z.object({ | ||
| session_id: z.string(), | ||
| }), | ||
| }), | ||
| ) | ||
| .handler(({ input }) => { | ||
| const { session_id } = input.cookies | ||
| // session_id is validated and typed | ||
| }) | ||
| ``` | ||
|
|
||
| Generated OpenAPI spec: | ||
|
|
||
| ```yaml | ||
| /profile: | ||
| get: | ||
| parameters: | ||
| - name: session_id | ||
| in: cookie | ||
| required: true | ||
| schema: | ||
| type: string | ||
| ``` | ||
|
|
||
| ## Out of Scope | ||
|
|
||
| - `inputStructure: 'compact'` cookie support | ||
| - Client-side cookie encoding (`openapi-link-codec.ts`) | ||
| - `Set-Cookie` response header in output structure | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.