这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 24, 2025

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:

# OpenAPI spec
components:
  schemas:
    base64Image:
      type: string
      pattern: '^data:image/svg\+xml;base64,[A-Za-z0-9+/]+=*$'

Generated invalid code:

export const zBase64Image = z.string().regex(/^data:image/svg\+xml;base64,[A-Za-z0-9+/]+=*$/);
//                                                    ^ This forward slash terminates the regex

This resulted in TypeScript compilation errors and broken generated code.

Solution

Fixed the createRegularExpressionLiteral function in packages/openapi-ts/src/tsc/types.ts to properly escape forward slashes in regex patterns before wrapping them in regex literal syntax.

The fix:

  1. Extracts pattern content (removing outer slashes if present)
  2. Uses negative lookbehind (?<!\\)/ to escape only unescaped forward slashes, preventing double-escaping
  3. Wraps the escaped pattern with forward slashes to create a valid regex literal

Now generates valid code:

export const zBase64Image = z.string().regex(/^data:image\/svg\+xml;base64,[A-Za-z0-9+\/]+=*$/);
//                                                    ^ Forward slashes properly escaped

Impact

  • Fixes Zod schemas for all versions (v3, v4, mini)
  • Fixes Valibot schemas (uses the same underlying function)
  • Backward compatible - existing patterns without forward slashes continue to work unchanged
  • Handles already escaped slashes - prevents double-escaping of patterns that are already correctly escaped
  • No performance impact - only processes patterns when forward slashes are present

Testing

Added comprehensive test coverage with 8 test cases covering:

  • Patterns with forward slashes (main issue case)
  • Patterns without forward slashes (regression prevention)
  • Patterns with multiple forward slashes
  • Patterns with existing outer slashes
  • Patterns with already correctly escaped slashes (prevents double-escaping)
  • Flag preservation
  • Edge cases

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.

@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@changeset-bot
Copy link

changeset-bot bot commented Sep 24, 2025

🦋 Changeset detected

Latest commit: dba81bc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hey-api/openapi-ts Patch

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

@vercel
Copy link

vercel bot commented Sep 24, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
hey-api-docs Ready Ready Preview Comment Sep 25, 2025 9:05am

Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
Copilot AI changed the title [WIP] Generated zod schemas do not escape regexes correctly Fix regex escaping in generated Zod and Valibot schemas for forward slashes Sep 24, 2025
Copilot AI requested a review from mrlubos September 24, 2025 12:10
Copilot finished work on behalf of mrlubos September 24, 2025 12:10
@mrlubos
Copy link
Member

mrlubos commented Sep 24, 2025

@copilot can you add a test covering already correctly escaped slashes?

…ogic

Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Sep 24, 2025

@copilot can you add a test covering already correctly escaped slashes?

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 (?<!\\)/ to only escape unescaped forward slashes, leaving already escaped ones unchanged. Fixed in commit d4efd5b.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Sep 24, 2025

Open in StackBlitz

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/codegen-core@2692
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/nuxt@2692
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/openapi-ts@2692
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/vite-plugin@2692

commit: dba81bc

@codecov
Copy link

codecov bot commented Sep 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 25.23%. Comparing base (c8124d9) to head (dba81bc).
⚠️ Report is 5 commits behind head on main.

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              
Flag Coverage Δ
unittests 25.23% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mrlubos mrlubos marked this pull request as ready for review September 25, 2025 09:06
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. bug 🔥 Something isn't working labels Sep 25, 2025
@mrlubos mrlubos merged commit bee26fe into main Sep 25, 2025
17 checks passed
@mrlubos mrlubos deleted the copilot/fix-4811abf9-2ebf-43bc-b1cd-2fde0f579207 branch September 25, 2025 09:57
@hey-api hey-api bot mentioned this pull request Sep 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🔥 Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated zod schemas do not escape regexes correctly

2 participants