-
-
Notifications
You must be signed in to change notification settings - Fork 272
Fix regex escaping in generated Zod and Valibot schemas for forward slashes #2692
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
Fix regex escaping in generated Zod and Valibot schemas for forward slashes #2692
Conversation
|
|
🦋 Changeset detectedLatest commit: dba81bc The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
|
@copilot can you add a test covering already correctly escaped slashes? |
…ogic Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
Added test case for patterns with already correctly escaped slashes and improved the escaping logic to prevent double-escaping. The function now uses negative lookbehind |
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2692 +/- ##
==========================================
+ Coverage 25.20% 25.23% +0.03%
==========================================
Files 386 386
Lines 37186 37188 +2
Branches 1781 1783 +2
==========================================
+ Hits 9373 9385 +12
+ Misses 27800 27790 -10
Partials 13 13
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Problem
When generating Zod or Valibot schemas from OpenAPI specs containing regex patterns with forward slashes, the generated TypeScript code was syntactically invalid. Forward slashes inside regex patterns were not being escaped, causing the regex literal to terminate prematurely.
Example issue:
Generated invalid code:
This resulted in TypeScript compilation errors and broken generated code.
Solution
Fixed the
createRegularExpressionLiteralfunction inpackages/openapi-ts/src/tsc/types.tsto properly escape forward slashes in regex patterns before wrapping them in regex literal syntax.The fix:
(?<!\\)/to escape only unescaped forward slashes, preventing double-escapingNow generates valid code:
Impact
Testing
Added comprehensive test coverage with 8 test cases covering:
All existing tests continue to pass, confirming no regressions were introduced.
Fixes #2691
Fixes #2691
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.