-
-
Notifications
You must be signed in to change notification settings - Fork 20
Add runtime response header validation with OpenAPI hot-reload #1804
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fc23f2a
Initial plan
Copilot a356bdf
Add runtime response header validation with counterfact-error-N headers
Copilot 756012d
Address code review feedback: improve status key logic, boolean coerc…
Copilot 9b3a17f
Rename error headers to response-type-error (one per error, same name)
Copilot e9037b4
Changes before error encountered
Copilot 5e8278c
Use appendedHeaders for response-type-error: one entry per error
Copilot f967106
Watch OpenAPI spec file and reload dispatcher document on change
Copilot f68d9cd
Move OpenAPI file watching into server/openapi-watcher.ts and load-op…
Copilot cbc69cc
Merge branch 'main' into copilot/fix-response-type-errors
pmcelhaney 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
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,5 @@ | ||
| --- | ||
| "counterfact": minor | ||
| --- | ||
|
|
||
| Validate response headers at runtime and report type errors as `response-type-error` HTTP headers (one per error, multiple headers with the same name). Use --no-validate-response to disable. |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import Ajv from "ajv"; | ||
|
|
||
| import type { OpenApiOperation } from "../counterfact-types/index.js"; | ||
| import type { CounterfactResponseObject } from "./registry.js"; | ||
|
|
||
| const ajv = new Ajv({ | ||
| allErrors: true, | ||
| strict: false, | ||
| coerceTypes: false, | ||
| }); | ||
|
|
||
| export interface ResponseValidationResult { | ||
| errors: string[]; | ||
| valid: boolean; | ||
| } | ||
|
|
||
| export function validateResponse( | ||
| operation: OpenApiOperation | undefined, | ||
| response: CounterfactResponseObject, | ||
| ): ResponseValidationResult { | ||
| if (!operation) { | ||
| return { errors: [], valid: true }; | ||
| } | ||
|
|
||
| const errors: string[] = []; | ||
|
|
||
| const statusKey = | ||
| response.status !== undefined ? String(response.status) : undefined; | ||
|
|
||
| const responseSpec = | ||
| (statusKey !== undefined ? operation.responses[statusKey] : undefined) ?? | ||
| operation.responses.default; | ||
|
|
||
| if (!responseSpec) { | ||
| return { errors: [], valid: true }; | ||
| } | ||
|
|
||
| const specHeaders = responseSpec.headers ?? {}; | ||
| const actualHeaders = response.headers ?? {}; | ||
|
|
||
| for (const [name, headerSpec] of Object.entries(specHeaders)) { | ||
| const actualValue = | ||
| actualHeaders[name] ?? actualHeaders[name.toLowerCase()]; | ||
|
|
||
| if (headerSpec.required === true && actualValue === undefined) { | ||
| errors.push(`response header '${name}' is required`); | ||
| continue; | ||
| } | ||
|
|
||
| if (actualValue !== undefined && headerSpec.schema !== undefined) { | ||
| const coercedValue = | ||
| typeof actualValue === "string" | ||
| ? coerceHeaderValue(actualValue, headerSpec.schema) | ||
| : actualValue; | ||
|
|
||
| const valid = ajv.validate(headerSpec.schema, coercedValue); | ||
|
|
||
| if (!valid && ajv.errors) { | ||
| for (const error of ajv.errors) { | ||
| const path = error.instancePath ?? ""; | ||
|
|
||
| errors.push( | ||
| `response header '${name}'${path} ${error.message ?? "is invalid"}`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| errors, | ||
| valid: errors.length === 0, | ||
| }; | ||
| } | ||
|
|
||
| function coerceHeaderValue( | ||
| value: string, | ||
| schema: { [key: string]: unknown }, | ||
| ): unknown { | ||
| const type = schema.type as string | undefined; | ||
|
|
||
| if (type === "integer" || type === "number") { | ||
| const num = Number(value); | ||
|
|
||
| return Number.isNaN(num) ? value : num; | ||
| } | ||
|
|
||
| if (type === "boolean") { | ||
| if (value === "true") return true; | ||
| if (value === "false") return false; | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| return value; | ||
| } |
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.
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.
No, one header per error.