Skip to content

Conversation

@RobinTail
Copy link
Owner

@RobinTail RobinTail commented Jun 3, 2025

Partially reverts #2697 and #2698

Summary by CodeRabbit

  • Documentation

    • Updated changelog entry to clarify that the documentation generator now prioritizes Zod's native depiction for enums and literals.
  • New Features

    • Improved handling of enums and literals in generated documentation, ensuring more accurate type representation.
  • Tests

    • Added new test cases to validate the depiction of enums and literals in the documentation output.

@RobinTail RobinTail added refactoring The better way to achieve the same result prevention labels Jun 3, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 3, 2025

Walkthrough

This update introduces two new depicter functions, depictEnum and depictLiteral, to handle enum and literal types in JSON schema generation. These functions are registered in the depicters mapping and are accompanied by new unit tests. The changelog was updated to clarify the approach to native Zod depictions.

Changes

File(s) Change Summary
CHANGELOG.md Revised entry for version 24.2.1 to clarify prioritization of Zod's native enum/literal depiction.
express-zod-api/src/documentation-helpers.ts Added and registered depictEnum and depictLiteral depicter functions for enum/literal handling.
express-zod-api/tests/documentation-helpers.spec.ts Added unit tests for depictEnum and depictLiteral functions using parameterized and snapshot tests.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant DocumentationGenerator
    participant Depicters
    User->>DocumentationGenerator: Request schema depiction
    DocumentationGenerator->>Depicters: Select depicter for enum/literal
    Depicters->>DocumentationGenerator: Use depictEnum or depictLiteral
    DocumentationGenerator->>User: Return JSON schema with correct type
Loading

Possibly related PRs

  • #2698: Removes the depictLiteral function and its registration, directly conflicting with this PR's addition of the same function.
  • #2697: Removes the depictEnum function and its registration, which this PR introduces and registers.
  • #2692: Refactors another depicter (depictUnion) in the same file, affecting related schema processing logic.

Poem

🐇
In the land where schemas grow,
Enum and Literal now clearly show.
With depicters new and tests in tow,
Zod’s true nature we proudly bestow.
A changelog note, a gentle nudge—
The docs now judge
By native code, not fudge!

✨ 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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
CHANGELOG.md (1)

8-9: Consider standardizing bullet phrasing.

To align with other entries, you could revise bullets to use action verbs, e.g.:

  • “Add Enum type support in Zod v3.25.45”
  • “Add Literal type support in Zod v3.25.49”
express-zod-api/tests/documentation-helpers.spec.ts (1)

306-315: Consider testing with different data types.

The test correctly verifies that depictEnum sets the type property based on the enum values. However, it only tests with string enum values. According to the implementation that uses typeof jsonSchema.enum?.[0], the function should work with various data types.

Consider adding test cases for different data types:

  describe("depictEnum()", () => {
-   test("should set type", () => {
+   test.each([
+     { enum: ["test", "jest"], expectedType: "string" },
+     { enum: [1, 2, 3], expectedType: "number" },
+     { enum: [true, false], expectedType: "boolean" },
+   ])("should set type based on enum values %#", ({ enum: enumValues }) => {
      expect(
        depictEnum(
-         { zodSchema: z.never(), jsonSchema: { enum: ["test", "jest"] } },
+         { zodSchema: z.never(), jsonSchema: { enum: enumValues } },
          requestCtx,
        ),
      ).toMatchSnapshot();
    });
  });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3405c20 and 7ce0866.

⛔ Files ignored due to path filters (1)
  • express-zod-api/tests/__snapshots__/documentation-helpers.spec.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • express-zod-api/src/documentation-helpers.ts (2 hunks)
  • express-zod-api/tests/documentation-helpers.spec.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
express-zod-api/tests/documentation-helpers.spec.ts (1)
express-zod-api/src/documentation-helpers.ts (2)
  • depictEnum (150-153)
  • depictLiteral (159-162)
🔇 Additional comments (4)
express-zod-api/src/documentation-helpers.ts (1)

398-400: Restore backward-compatibility mapping for enum and literal depicters
Re-registering depictLiteral and depictEnum in the depicters map ensures that legacy schemas still receive a fallback when Zod’s native depiction is unavailable.

CHANGELOG.md (1)

7-9: Changelog entry clarification aligns with new behavior.

The updated wording clearly describes prioritizing native Zod depictions via the new depictEnum and depictLiteral functions, improving backward-compatibility clarity.

express-zod-api/tests/documentation-helpers.spec.ts (2)

28-29: LGTM: Import statements are correctly added.

The new depicter functions depictEnum and depictLiteral are properly imported and will be available for testing.


317-326: Well-structured parameterized test with good coverage.

The test correctly verifies that depictLiteral sets the type property from both const and enum properties. The parameterized approach effectively tests both code paths in the implementation.

However, similar to the depictEnum test, consider adding test cases with different data types to ensure the typeof operation works correctly across various JavaScript types (numbers, booleans, etc.).

@RobinTail RobinTail merged commit b95ccfd into master Jun 3, 2025
15 checks passed
@RobinTail RobinTail deleted the rev-preserve-fallback branch June 3, 2025 05:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

prevention refactoring The better way to achieve the same result

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants