Skip to content

fix(zod): union and intersection schema conversion#1157

Merged
dinwwwh merged 1 commit intomainfrom
fix/zod/common-schema-not-workwell-in-union
Nov 2, 2025
Merged

fix(zod): union and intersection schema conversion#1157
dinwwwh merged 1 commit intomainfrom
fix/zod/common-schema-not-workwell-in-union

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented Nov 1, 2025

Summary by CodeRabbit

  • Refactor

    • Improved recursion depth handling for nested schema conversions to tighten depth tracking.
    • Unified union/intersection schema output to always produce a consistent anyOf array.
  • Tests

    • Updated test expectations to match the new schema output format for unions including undefined.

@vercel
Copy link
Copy Markdown

vercel Bot commented Nov 1, 2025

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

Project Deployment Preview Comments Updated (UTC)
orpc Ready Ready Preview Comment Nov 1, 2025 9:48am

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Nov 1, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Nov 1, 2025

Walkthrough

Recursive conversion now increments structureDepth for child items in Union, DiscriminatedUnion, and Intersection branches; the single-element anyOf short-circuit was removed so unions always emit an { anyOf: [...] } form. Tests were updated to expect the anyOf-wrapped output for single-item unions with undefined.

Changes

Cohort / File(s) Summary
Tests updated
packages/zod/src/converter.test.ts, packages/zod/src/zod4/converter.combination.test.ts
Expected JSON-schema for unions that previously produced a single type (e.g., { type: 'string' }) now expects the anyOf-wrapped form (e.g., { anyOf: [{ type: 'string' }] }).
Main converter depth & anyOf behavior
packages/zod/src/converter.ts
Union, discriminated-union, and intersection branches now call child conversion with structureDepth + 1. Removed the optimization that returned a single anyOf item directly; unions always return an object with anyOf array.
Zod4 converter depth & standardized union format
packages/zod/src/zod4/converter.ts
Union and intersection recursive calls increment depth (structureDepth + 1). Union return always uses { anyOf: [...] } rather than returning a single item when one option exists.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as convert(schema, depth)
    participant Union as Union Handler
    participant Child as Child Converter

    rect rgb(240,248,255)
    Note over Caller,Child: Old behavior
    Caller->>Union: convert(unionSchema, depth=0)
    Union->>Child: convert(optionA, depth=0)
    Union->>Child: convert(optionB, depth=0)
    Union->>Caller: return anyOf[0] if length==1
    end

    rect rgb(255,245,240)
    Note over Caller,Child: New behavior
    Caller->>Union: convert(unionSchema, depth=0)
    Union->>Child: convert(optionA, depth=1)
    Union->>Child: convert(optionB, depth=1)
    Union->>Caller: return { anyOf: [ ... ] } always
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Pay attention to depth-limit logic and any places that rely on previous depth values.
  • Review callers/consumers that expected the old single-item return shape.
  • Confirm tests cover discriminated unions and nested intersections for depth-related behavior.

Possibly related PRs

  • unnoq/orpc#206 — Modifies the zod JSON-schema converter and tests; appears directly related to the converter changes and test expectations for unions/anyOf.

Suggested labels

size:M

Poem

🐇 I hopped through branches, depth in tow,
Each child now deeper, one step to grow.
AnyOf gathered in tidy array,
Tests now cheer for the standardized way. 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "fix(zod): union and intersection schema conversion" accurately reflects the main changes across the affected files. The PR modifies schema conversion behavior for unions, discriminated unions, and intersections by adjusting structureDepth tracking and removing the single-element anyOf short-circuit optimization. The title is specific and concise, clearly identifying the primary focus of the changes without being overly broad or misleading. While implementation details like recursion depth adjustments are not mentioned, the title appropriately captures the high-level objective.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/zod/common-schema-not-workwell-in-union

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7d034bb and 75c8463.

📒 Files selected for processing (4)
  • packages/zod/src/converter.test.ts (1 hunks)
  • packages/zod/src/converter.ts (2 hunks)
  • packages/zod/src/zod4/converter.combination.test.ts (1 hunks)
  • packages/zod/src/zod4/converter.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/zod/src/zod4/converter.ts
  • packages/zod/src/zod4/converter.combination.test.ts
  • packages/zod/src/converter.test.ts
🔇 Additional comments (1)
packages/zod/src/converter.ts (1)

533-550: LGTM: Intersection depth tracking fixed.

The change to increment structureDepth + 1 when recursing into intersection components (left and right) correctly tracks nesting depth and is consistent with the union fix at line 516.


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.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @unnoq, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a bug in the Zod to JSON Schema converter, specifically refining how union and intersection schemas are translated. The core improvement ensures that union types are always represented as anyOf arrays in the resulting JSON schema, even if they contain only one element, thereby preventing implicit type simplification. Additionally, it enhances the handling of nested schemas by correctly managing recursion depth during the conversion process, leading to more accurate and robust schema generation.

Highlights

  • Union Schema Conversion Logic: The conversion of z.union schemas to JSON Schema has been updated to consistently produce an anyOf array, even when the union effectively simplifies to a single type (e.g., z.union([z.string(), z.undefined()])). This prevents unintended simplification and ensures a more explicit representation of union types.
  • Recursion Depth Handling: The structureDepth parameter is now correctly incremented when recursively converting members of union and intersection schemas. This change improves the robustness of the converter, particularly for complex or deeply nested schema definitions, by ensuring proper tracking of recursion depth.
  • Test Case Alignment: Relevant test cases that previously expected z.union([z.string(), z.undefined()]) to simplify to a basic string type in the JSON schema output have been removed, aligning the tests with the new, more explicit conversion behavior.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request fixes how Zod union and intersection schemas are converted. The changes correctly increment the structureDepth during recursive calls for unions and intersections, which is a great bug fix. Additionally, the logic for handling unions is updated to no longer simplify anyOf arrays that contain a single item. This improves consistency in the generated schema. My main feedback is to suggest updating the removed test cases to assert the new, correct behavior instead of deleting them, to maintain test coverage.

@codecov
Copy link
Copy Markdown

codecov Bot commented Nov 1, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Nov 1, 2025

More templates

@orpc/ai-sdk

npm i https://pkg.pr.new/@orpc/ai-sdk@1157

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@1157

@orpc/client

npm i https://pkg.pr.new/@orpc/client@1157

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@1157

@orpc/experimental-durable-iterator

npm i https://pkg.pr.new/@orpc/experimental-durable-iterator@1157

@orpc/hey-api

npm i https://pkg.pr.new/@orpc/hey-api@1157

@orpc/interop

npm i https://pkg.pr.new/@orpc/interop@1157

@orpc/json-schema

npm i https://pkg.pr.new/@orpc/json-schema@1157

@orpc/nest

npm i https://pkg.pr.new/@orpc/nest@1157

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@1157

@orpc/openapi-client

npm i https://pkg.pr.new/@orpc/openapi-client@1157

@orpc/otel

npm i https://pkg.pr.new/@orpc/otel@1157

@orpc/experimental-publisher

npm i https://pkg.pr.new/@orpc/experimental-publisher@1157

@orpc/react

npm i https://pkg.pr.new/@orpc/react@1157

@orpc/react-query

npm i https://pkg.pr.new/@orpc/react-query@1157

@orpc/experimental-react-swr

npm i https://pkg.pr.new/@orpc/experimental-react-swr@1157

@orpc/server

npm i https://pkg.pr.new/@orpc/server@1157

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@1157

@orpc/solid-query

npm i https://pkg.pr.new/@orpc/solid-query@1157

@orpc/standard-server

npm i https://pkg.pr.new/@orpc/standard-server@1157

@orpc/standard-server-aws-lambda

npm i https://pkg.pr.new/@orpc/standard-server-aws-lambda@1157

@orpc/standard-server-fastify

npm i https://pkg.pr.new/@orpc/standard-server-fastify@1157

@orpc/standard-server-fetch

npm i https://pkg.pr.new/@orpc/standard-server-fetch@1157

@orpc/standard-server-node

npm i https://pkg.pr.new/@orpc/standard-server-node@1157

@orpc/standard-server-peer

npm i https://pkg.pr.new/@orpc/standard-server-peer@1157

@orpc/svelte-query

npm i https://pkg.pr.new/@orpc/svelte-query@1157

@orpc/tanstack-query

npm i https://pkg.pr.new/@orpc/tanstack-query@1157

@orpc/trpc

npm i https://pkg.pr.new/@orpc/trpc@1157

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@1157

@orpc/vue-colada

npm i https://pkg.pr.new/@orpc/vue-colada@1157

@orpc/vue-query

npm i https://pkg.pr.new/@orpc/vue-query@1157

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@1157

commit: 75c8463

The previous implementation incorrectly simplified `anyOf` schemas with
only one element. This commit removes that simplification.

Additionally, the `structureDepth` was not being incremented when
converting union and intersection schemas. This has been fixed to
prevent stack overflows on complex schemas.
@dinwwwh dinwwwh force-pushed the fix/zod/common-schema-not-workwell-in-union branch from 7d034bb to 75c8463 Compare November 1, 2025 09:43
@dinwwwh dinwwwh merged commit b1b9914 into main Nov 2, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant