Skip to content

Better implementation for depictUnion#2692

Merged
RobinTail merged 3 commits intomasterfrom
fix-discriminator-depict
Jun 1, 2025
Merged

Better implementation for depictUnion#2692
RobinTail merged 3 commits intomasterfrom
fix-discriminator-depict

Conversation

@RobinTail
Copy link
Copy Markdown
Owner

@RobinTail RobinTail commented Jun 1, 2025

Summary by CodeRabbit

  • Documentation

    • Updated the changelog to clarify the status and restoration of the getExamples() helper.
  • Refactor

    • Improved the handling of union schemas in documentation generation for more accurate discriminator extraction.

@RobinTail RobinTail added the refactoring The better way to achieve the same result label Jun 1, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 1, 2025

Walkthrough

The changelog was updated for clarity regarding the status of the getExamples() helper, noting its restoration in version 24.1.0. In the codebase, the depictUnion function was refactored to use explicit type checks and direct discriminator extraction for union schemas, replacing a previous heuristic approach.

Changes

File(s) Change Summary
CHANGELOG.md Clarified the removal and restoration history of the getExamples() helper in the version 24.0.0 entry.
express-zod-api/src/documentation-helpers.ts Refactored depictUnion to use explicit type checks and direct discriminator extraction for union schemas.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant depictUnion
    participant zodSchema

    User->>depictUnion: Call with zodSchema and jsonSchema
    depictUnion->>zodSchema: Check if union or discriminated union
    alt Is union/discriminated union and has discriminator
        depictUnion->>zodSchema: Extract discriminator from _zod.def.discriminator
        depictUnion->>User: Return jsonSchema with discriminator property
    else Not a union or no discriminator
        depictUnion->>User: Return original jsonSchema
    end
Loading

Possibly related PRs

  • RobinTail/express-zod-api#2679: Refactors depictUnion by removing legacy code and still relies on propValues for discriminator extraction, indicating both PRs modify the same function but with different extraction approaches.

Poem

In the docs, a helper lost,
But soon returned, no longer tossed.
Unions now checked with care,
Discriminators found right there!
With code refined and clarity won,
This rabbit’s work is never done.
🐇✨


📜 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 116405e and 4ed9a64.

📒 Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • express-zod-api/src/documentation-helpers.ts (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
express-zod-api/src/documentation-helpers.ts (2)
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:1-3
Timestamp: 2025-05-27T20:27:17.001Z
Learning: The JSONSchema type is not exported from the main "zod" module and must be imported from "zod/v4/core" when using Zod v4. This is acceptable for type-only imports as they don't create runtime dependencies.
Learnt from: RobinTail
PR: RobinTail/express-zod-api#2546
File: express-zod-api/src/json-schema-helpers.ts:1-3
Timestamp: 2025-05-27T20:27:17.001Z
Learning: The JSONSchema type is not exported from the main "zod" module and must be imported from "zod/v4/core" when using Zod v4. This is acceptable for type-only imports as they don't create runtime dependencies.
🧬 Code Graph Analysis (1)
express-zod-api/src/documentation-helpers.ts (1)
express-zod-api/src/common-helpers.ts (1)
  • isSchema (85-91)
🔇 Additional comments (3)
express-zod-api/src/documentation-helpers.ts (2)

2-2: LGTM: Proper type imports for union schema handling.

The addition of $ZodDiscriminatedUnion and $ZodUnion type imports follows the established pattern for Zod v4 and enables proper type checking in the refactored depictUnion function.

Also applies to: 7-7


118-126: Excellent refactor: More explicit and type-safe union handling.

The refactored depictUnion function is a significant improvement over the previous heuristic approach:

  1. Explicit type checking: Using isSchema<$ZodUnion | $ZodDiscriminatedUnion>(zodSchema, "union") provides compile-time type safety and runtime validation
  2. Direct property access: Accessing zodSchema._zod.def.discriminator directly is cleaner than the previous approach of extracting from propValues keys
  3. Clear control flow: Early returns make the function logic more readable and maintainable

The logic correctly preserves the original behavior while being more robust and type-safe.

CHANGELOG.md (1)

50-50: Changelog update for getExamples() helper
The v24.0.0 entry clearly strikes through the old recommendation and notes its restoration in v24.1.0. This improves accuracy and aligns with the v24.1.0 section above.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@RobinTail RobinTail marked this pull request as ready for review June 1, 2025 10:15
Copy link
Copy Markdown
Owner Author

@RobinTail RobinTail left a comment

Choose a reason for hiding this comment

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

ok

@coveralls-official
Copy link
Copy Markdown

coveralls-official Bot commented Jun 1, 2025

Coverage Status

coverage: 100.0%. remained the same
when pulling 4ed9a64 on fix-discriminator-depict
into 116405e on master.

@RobinTail RobinTail merged commit 9b93238 into master Jun 1, 2025
15 checks passed
@RobinTail RobinTail deleted the fix-discriminator-depict branch June 1, 2025 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring The better way to achieve the same result

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant