Repository navigation
fix(api): relax provider-scoped chat completion validation #5907
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,6 @@ import { initTranslators } from "@omniroute/open-sse/translator/index.ts"; | |
| import { errorResponse } from "@omniroute/open-sse/utils/error.ts"; | ||
| import { HTTP_STATUS } from "@omniroute/open-sse/config/constants.ts"; | ||
| import { getRegistryEntry } from "@omniroute/open-sse/config/providerRegistry.ts"; | ||
| import { providerChatCompletionSchema } from "@/shared/validation/schemas"; | ||
| import { isValidationFailure, validateBody } from "@/shared/validation/helpers"; | ||
|
|
||
| let initialized = false; | ||
|
|
||
|
|
@@ -30,6 +28,7 @@ export async function OPTIONS() { | |
| /** | ||
| * POST /v1/providers/{provider}/chat/completions | ||
| * Routes to the specified provider, validating model/provider match. | ||
| * Full body format validation is delegated to handleChat. | ||
| */ | ||
| export async function POST(request, { params }) { | ||
| const { provider: rawProvider } = await params; | ||
|
|
@@ -45,18 +44,25 @@ export async function POST(request, { params }) { | |
|
|
||
| await ensureInitialized(); | ||
|
|
||
| // Clone request with provider-prefixed model | ||
| let rawBody; | ||
| // Parse body once so this provider-scoped route can normalize the model prefix | ||
| // before delegating full chat-format validation to handleChat. | ||
| let rawBody: unknown; | ||
| try { | ||
| rawBody = await request.json(); | ||
| } catch { | ||
| return errorResponse(HTTP_STATUS.BAD_REQUEST, "Invalid JSON body"); | ||
| } | ||
| const validation = validateBody(providerChatCompletionSchema, rawBody); | ||
| if (isValidationFailure(validation)) { | ||
| return errorResponse(HTTP_STATUS.BAD_REQUEST, validation.error.message); | ||
|
|
||
| if (!rawBody || typeof rawBody !== "object" || Array.isArray(rawBody)) { | ||
| return errorResponse(HTTP_STATUS.BAD_REQUEST, "Request body must be a JSON object"); | ||
| } | ||
|
|
||
| const body = rawBody as { model?: string; [key: string]: unknown }; | ||
|
Comment on lines
+56
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential Bug / Content-Length Mismatch:\nWhen the model prefix is normalized (e.g., prepending |
||
|
|
||
| // Keep the route-level checks minimal: only guard fields needed for provider prefix handling. | ||
| if (body.model !== undefined && typeof body.model !== "string") { | ||
| return errorResponse(HTTP_STATUS.BAD_REQUEST, "model must be a string"); | ||
| } | ||
| const body = validation.data; | ||
|
|
||
| // Validate model belongs to this provider | ||
| if (body.model) { | ||
|
|
||
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.
Style Guide Violation (Rule 9 - Missing Tests):\nAccording to the Repository Style Guide (Section 2, Rule 9):\n> 9. Always include tests when changing production code (
src/,open-sse/,electron/,bin/).\n\nSince this PR modifies production code insrc/app/api/v1/providers/[provider]/chat/completions/route.tsto relax the validation logic, we must include or update corresponding tests.\n\nPlease add or update unit tests in thetests/directory (e.g., intests/unit/chat-route-coverage.test.tsor a new test file) to verify:\n1. That requests with extra fields (which were previously rejected byproviderChatCompletionSchema) are now successfully accepted and forwarded.\n2. That invalid JSON or a non-stringmodelare still rejected with a400 Bad Requestas expected.References