Skip to content

fix(zod): parser generation when content-type contains charset precision - #3430

Closed
melloware wants to merge 2 commits into
masterfrom
O3285
Closed

fix(zod): parser generation when content-type contains charset precision#3430
melloware wants to merge 2 commits into
masterfrom
O3285

Conversation

@melloware

@melloware melloware commented May 24, 2026

Copy link
Copy Markdown
Collaborator

Fix #3420

Summary by CodeRabbit

  • Bug Fixes

    • Improved content-type matching to correctly handle charset/parameterized media types (e.g., application/json; charset=utf-8) and multipart form-data.
  • Tests

    • Added regression test covering multipart/form-data and JSON content keys with charset suffixes.
  • Chores

    • Generated mock outputs updated to emit missing/nullable values using an equivalent sentinel (e.g., void 0).
    • Added generated response schemas for the ListPets endpoint in snapshots.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 24, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR normalizes content-type strings and uses regex-based media-type matching in Zod generation, adds a regression test for charset-suffixed media types, and updates generated MSW/faker mocks and Zod snapshot exports across samples/tests.

Changes

Media Type Detection and Code Generation Updates

Layer / File(s) Summary
Media type detection with parameter normalization
packages/zod/src/index.ts
Introduces isMediaType helper to normalize content-type strings by stripping parameters and performs regex-based matching for JSON types (application/*+json, application/json) and form-data, replacing exact string lookups in parseBodyAndResponse.
Regression test for parameterized media types
packages/zod/src/zod.test.ts
Adds test coverage for multipart form-data and JSON content keys that include ; charset=... suffixes, validating the updated media type detection and Zod schema generation for both request and response bodies.
Generated mock data updates
samples/**/api/**/*.msw.ts, samples/**/src/**/endpoints/**/*.faker.ts
Updates generated MSW/faker mock files across sample projects to use void 0 instead of undefined for nullable/optional mock values (phone, tag, optionalNullableString, etc.).
Generated Zod schema exports
tests/__snapshots__/**/*.ts, tests/__snapshots__/**/*.zod.ts
Adds ListPetsResponseItem and ListPetsResponse Zod schema exports across snapshot/test generations, modeling union/intersection compositions of pet variants with shared identity/contact fields for list endpoints.

🎯 3 (Moderate) | ⏱️ ~20 minutes

suggested labels: zod``

🐰 Whiskers twitches with delight
Our media types now dance with charset grace,
void 0 hops where undefined used to trace,
Zod schemas bloom for pets both far and near,
The code sings sweeter—a developer's cheer! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main fix: handling Content-Type headers with charset precision in Zod parser generation, directly addressing the primary code change.
Linked Issues check ✅ Passed The PR addresses issue #3420 by implementing a fix for Content-Type charset handling in Zod generation, enabling the v8.12.2 release to proceed as intended.
Out of Scope Changes check ✅ Passed The changeset includes core fix changes (media type handling in parseBodyAndResponse), a regression test, and numerous generated snapshot updates. All changes are consistent with the Content-Type charset fix objective.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch O3285

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/zod/src/index.ts`:
- Around line 1352-1354: The media-type check is too permissive: update the
multipart/form-data match to be anchored so it only matches the exact media type
(not suffixes/prefixes). Change the usage of isMediaType in the formDataContent
selection (variable formDataContent and array contentEntries) to require an
exact match (or adjust isMediaType implementation to use an anchored pattern
such as ^multipart\/form-data$) and apply the same fix to the other occurrence
around the code that mirrors lines 1436-1439 so both branches only match the
exact media type.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 836220aa-3333-4fb3-a03b-f2fda7beffc1

📥 Commits

Reviewing files that changed from the base of the PR and between f2b15fe and 459c526.

📒 Files selected for processing (60)
  • packages/zod/src/index.ts
  • packages/zod/src/zod.test.ts
  • samples/angular-app/__snapshots__/api/endpoints-zod/pets/pets.msw.ts
  • samples/angular-app/__snapshots__/api/http-both/pets/pets.msw.ts
  • samples/angular-app/__snapshots__/api/http-client-custom-params/pets/pets.msw.ts
  • samples/angular-app/__snapshots__/api/http-client/pets/pets.msw.ts
  • samples/angular-app/__snapshots__/api/http-resource-zod/pets/pets.msw.ts
  • samples/angular-app/__snapshots__/api/http-resource/pets/pets.msw.ts
  • samples/angular-app/src/api/endpoints-zod/pets/pets.msw.ts
  • samples/angular-app/src/api/http-both/pets/pets.msw.ts
  • samples/angular-app/src/api/http-client-custom-params/pets/pets.msw.ts
  • samples/angular-app/src/api/http-client/pets/pets.msw.ts
  • samples/angular-app/src/api/http-resource-zod/pets/pets.msw.ts
  • samples/angular-app/src/api/http-resource/pets/pets.msw.ts
  • samples/angular-query/__snapshots__/api/endpoints/pets/pets.msw.ts
  • samples/angular-query/src/api/endpoints/pets/pets.msw.ts
  • samples/basic/__snapshots__/endpoints/petstoreFromFileSpecWithTransformer.ts
  • samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts
  • samples/react-app-with-swr/basic/__snapshots__/endpoints/petstoreFromFileSpecWithTransformer.faker.ts
  • samples/react-app-with-swr/basic/__snapshots__/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
  • samples/react-app-with-swr/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.faker.ts
  • samples/react-app-with-swr/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
  • samples/react-app/__snapshots__/endpoints/petstoreFromFileSpecWithTransformer.faker.ts
  • samples/react-app/__snapshots__/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
  • samples/react-app/src/api/endpoints/petstoreFromFileSpecWithTransformer.faker.ts
  • samples/react-app/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
  • samples/react-query/basic/__snapshots__/endpoints/petstoreFromFileSpecWithTransformer.faker.ts
  • samples/react-query/basic/__snapshots__/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
  • samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.faker.ts
  • samples/react-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
  • samples/solid-start/basic/src/api/endpoints/petstore.faker.ts
  • samples/solid-start/basic/src/api/endpoints/petstore.msw.ts
  • samples/svelte-query/basic/__snapshots__/endpoints/petstoreFromFileSpecWithTransformer.faker.ts
  • samples/svelte-query/basic/__snapshots__/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
  • samples/svelte-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.faker.ts
  • samples/svelte-query/basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
  • samples/vue-query/vue-query-basic/__snapshots__/endpoints/petstoreFromFileSpecWithTransformer.faker.ts
  • samples/vue-query/vue-query-basic/__snapshots__/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
  • samples/vue-query/vue-query-basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.faker.ts
  • samples/vue-query/vue-query-basic/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
  • tests/__snapshots__/default/schemas-zod-only/endpoints.ts
  • tests/__snapshots__/hono/endpoint-parameters/endpoints.zod.ts
  • tests/__snapshots__/hono/petstore-single/endpoints.zod.ts
  • tests/__snapshots__/hono/petstore-split-with-handlers/endpoints.zod.ts
  • tests/__snapshots__/hono/petstore-split/endpoints.zod.ts
  • tests/__snapshots__/hono/petstore-tags-split-with-handlers/pets/pets.zod.ts
  • tests/__snapshots__/hono/petstore-tags-split/pets/pets.zod.ts
  • tests/__snapshots__/hono/petstore-tags-with-handlers/pets.zod.ts
  • tests/__snapshots__/hono/petstore-tags/pets.zod.ts
  • tests/__snapshots__/hono/zod-schema-response/endpoints.zod.ts
  • tests/__snapshots__/mcp/custom-server/tool-schemas.zod.ts
  • tests/__snapshots__/mcp/single/tool-schemas.zod.ts
  • tests/__snapshots__/mcp/zod-schema-response/tool-schemas.zod.ts
  • tests/__snapshots__/zod/branded-types/branded-types.ts
  • tests/__snapshots__/zod/petstore-tags-split/pets/pets.ts
  • tests/__snapshots__/zod/petstore/endpoints.ts
  • tests/__snapshots__/zod/preprocess/preprocess.ts
  • tests/__snapshots__/zod/schemas-false/endpoints.ts
  • tests/__snapshots__/zod/split/endpoints.ts
  • tests/__snapshots__/zod/tags/pets.ts

Comment thread packages/zod/src/index.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/zod/src/index.ts (1)

1436-1439: 💤 Low value

Consider compiling the regex once for better performance.

The RegExp is currently created on each invocation of the returned predicate. For better performance, compile it once when isMediaType is called.

♻️ Suggested optimization
 const isMediaType =
-  (pattern: string) =>
-  ([contentType]: [string, object]): boolean =>
-    new RegExp(pattern).test(contentType.split(';')[0].trim().toLowerCase());
+  (pattern: string) => {
+    const regex = new RegExp(pattern);
+    return ([contentType]: [string, object]): boolean =>
+      regex.test(contentType.split(';')[0].trim().toLowerCase());
+  };
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/zod/src/index.ts` around lines 1436 - 1439, The predicate factory is
compiling a new RegExp on every call of the returned function; change
isMediaType to compile the regex once when called (e.g. create const re = new
RegExp(pattern)) and have the returned predicate use re.test(...) against
contentType.split(';')[0].trim().toLowerCase(); update the function
signature/name isMediaType and the inner predicate to reference the precompiled
regex.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/zod/src/index.ts`:
- Around line 1436-1439: The predicate factory is compiling a new RegExp on
every call of the returned function; change isMediaType to compile the regex
once when called (e.g. create const re = new RegExp(pattern)) and have the
returned predicate use re.test(...) against
contentType.split(';')[0].trim().toLowerCase(); update the function
signature/name isMediaType and the inner predicate to reference the precompiled
regex.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d77a90eb-dfe0-48e7-986d-c3b4f02dec93

📥 Commits

Reviewing files that changed from the base of the PR and between 459c526 and 3d7e9e4.

📒 Files selected for processing (1)
  • packages/zod/src/index.ts

@melloware

Copy link
Copy Markdown
Collaborator Author

cc @wadakatu i must be doing something wrong here i have run everything locally including a nuke:all but this keeps failing on GitHub with the same flip flopping failure about void 0 vs undefined?

@wadakatu

wadakatu commented May 24, 2026

Copy link
Copy Markdown
Contributor

Hey @melloware — debugged locally; CI is only upset about a narrow slice of the diff.

Fine: the zod fix in packages/zod/src/index.ts and the 20 snapshot updates under tests/__snapshots__/.../zod.ts. Verified by applying just the zod patch on top of clean master — those are legitimate drift from the fix and CI accepts them.

Breaking CI: the 38 undefinedvoid 0 substitutions across samples/**/*.msw.ts (and their __snapshots__/ counterparts). The orval mock generator only emits the literal 'undefined' — there's no void 0 anywhere in packages/mock/:

$ git grep -nE "void 0" packages/mock/
(no results)

test:snapshots depends on generate-api in turbo.json, so CI regenerates samples/*/src/api/.../*.msw.ts before comparing. The regenerated files come out with undefined, but the committed __snapshots__/ now have void 0 — mismatch.

I couldn't tell from here where those void 0s came from on your side (orval itself doesn't emit them). Resetting samples/** and regenerating clean should surface the source:

git checkout origin/master -- samples/
bun run nuke:all && bun install
bun run --filter '*' generate-api
git diff samples/

If it's easier on your side, happy to open a separate PR with the same fix (the zod change + the legitimate tests/__snapshots__/ updates only) — full credit stays with you for the fix. Totally fine either way 🙏

@melloware

Copy link
Copy Markdown
Collaborator Author

Yes please i just ran all your steps and its still thinks there is snapshot fix. Can you open a PR with this fix?

@melloware

Copy link
Copy Markdown
Collaborator Author

One caveat is I am running under Windows WSL not sure if that makes a difference or not?

@wadakatu

Copy link
Copy Markdown
Contributor

Opened #3433 with just the zod fix + the legitimate tests/__snapshots__/ updates — both you and @pierre-isabel-bbc are tagged as co-authors. Feel free to close this PR once #3433 is merged 🙏

@melloware melloware closed this May 24, 2026
@melloware
melloware deleted the O3285 branch May 24, 2026 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants