Improve implicit FormData detection. Add tests.#1539
Improve implicit FormData detection. Add tests.#1539aphofstede wants to merge 2 commits intoacacode:mainfrom
Conversation
|
e12e441 to
4a39905
Compare
|
bugbot run |
|
You want to merge it @smorimoto? |
There was a problem hiding this comment.
Pull request overview
Refines the “implicit FormData detection” workaround so it only triggers for actual File/binary request body types (avoiding false positives like FileType), and adds a dedicated spec fixture to lock in the intended behavior (Fixes #1538).
Changes:
- Tighten implicit FormData detection in
SchemaRoutesby replacing a substring check with a more precise regex match. - Add a new OpenAPI spec fixture and a Vitest snapshot test covering true positives (
file/binary) and false positives (custom formats / component names containingFile).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/schema-routes/schema-routes.ts |
Updates the implicit FormData detection heuristic to avoid matching File* type names too broadly. |
tests/spec/implicitFormData/schema.json |
Adds a spec fixture with endpoints exercising file/binary detection and multiple false-positive scenarios. |
tests/spec/implicitFormData/basic.test.ts |
Adds a snapshot test that generates a client from the fixture and asserts the expected ContentType selection. |
tests/spec/implicitFormData/__snapshots__/basic.test.ts.snap |
Captures the expected generated output for the new spec test. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Match e.g. ": File" followed by word boundary, array brackets, or union/nullable syntax | ||
| // This prevents false positives like ": FileType" or ": SomeFile" | ||
| // Escape special regex characters (getSchemaType can theoretically return complex types like "File | null" or "UtilRequiredKeys<File, ...>") | ||
| const escapedType = dataType.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
| return new RegExp(`:\\s*${escapedType}(?:\\[\\]|\\s*\\||\\b|$)`).test( | ||
| content, | ||
| ); |
There was a problem hiding this comment.
The regex only matches : File followed by []/|/word-boundary, but it won’t match array types produced as : (File)[] (or : (File | null)[]) because of the leading (. If request bodies can contain arrays of file/binary, the workaround will miss them and keep contentKind as JSON. Consider allowing optional parentheses around the matched type (e.g., match :\s*\(?${type}\)? before handling []/unions), or add a dedicated branch to handle the (...)[] form.
| // Match e.g. ": File" followed by word boundary, array brackets, or union/nullable syntax | |
| // This prevents false positives like ": FileType" or ": SomeFile" | |
| // Escape special regex characters (getSchemaType can theoretically return complex types like "File | null" or "UtilRequiredKeys<File, ...>") | |
| const escapedType = dataType.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | |
| return new RegExp(`:\\s*${escapedType}(?:\\[\\]|\\s*\\||\\b|$)`).test( | |
| content, | |
| ); | |
| // Match e.g. ": File" or ": (File)" (optionally with unions like "(File | null)") | |
| // followed by word boundary, array brackets, or union/nullable syntax. | |
| // This prevents false positives like ": FileType" or ": SomeFile". | |
| // Escape special regex characters (getSchemaType can theoretically return complex types like "File | null" or "UtilRequiredKeys<File, ...>") | |
| const escapedType = dataType.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | |
| // Allow either a bare type or a parenthesized form that may contain unions, e.g. "(File | null)". | |
| const pattern = `:\\s*(?:\\(\\s*${escapedType}(?:\\s*\\|[^)]*)?\\)|${escapedType})(?:\\[\\]|\\s*\\||\\b|$)`; | |
| return new RegExp(pattern).test(content); |
Avoid matching "File" type usage too broadly.
Fixes #1538
Note
Use precise regex matching to detect actual File/binary request bodies as FormData and add tests covering custom formats and false positives.
src/schema-routes/schema-routes.ts):File/binarytypes (including arrays/unions) while avoiding false positives (e.g.,FileType).tests/spec/implicitFormData/):schema.json), test (basic.test.ts), and snapshot verifying:FileType) do not.Filedo.Written by Cursor Bugbot for commit 4a39905. This will update automatically on new commits. Configure here.