Skip to content

Conversation

@diesal11
Copy link
Contributor

@diesal11 diesal11 commented Mar 6, 2025

Fixes #2028

This also silently fixes the enhanced PrismaClient types by updating that code to use .includes(DELEGATE_AUX_RELATION_PREFIX) over .startsWith(DELEGATE_AUX_RELATION_PREFIX) to search for delegate aux fields.

Unfortunately I couldn't find an easy way to test the PrismaClient types within the existing test infrastructure, so that regression is untested.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 6, 2025

📝 Walkthrough

Walkthrough

The changes update the filtering conditions in schema generator methods by replacing the use of startsWith with includes for comparing against the DELEGATE_AUX_RELATION_PREFIX in both the enhancer and Zod generators. Additionally, a new regression test has been added to validate that schema generation correctly filters out delegate fields and properly handles relationships in the generated models.

Changes

Files Change Summary
packages/schema/.../enhancer/enhance/index.ts
packages/schema/.../zod/generator.ts
Updated filtering logic in methods (transformInterface, findAuxDecls, and generateObjectSchemas) by switching from startsWith to includes when checking for DELEGATE_AUX_RELATION_PREFIX. No public API declarations were modified.
tests/regression/tests/issue-2028.test.ts Introduced a new regression test that sets up a database schema with multiple models and relationships and asserts that Zod schemas correctly exclude delegate fields.

Sequence Diagram(s)

Filtering Logic Flow

sequenceDiagram
    participant Caller as Generator Caller
    participant Generator as Schema Generator
    participant Filter as Filtering Logic
    Caller->>Generator: Invoke schema generation (transformInterface/generateObjectSchemas)
    Generator->>Filter: Evaluate field/method names using "includes" check
    Filter-->>Generator: Return filtered list of fields/methods
    Generator-->>Caller: Return processed schema data
Loading

Regression Test Flow

sequenceDiagram
    participant Test as Regression Test
    participant DB as Database
    participant Schema as Schema Loader
    participant Parser as Zod Parser
    Test->>DB: Create and connect to database
    Test->>Schema: Load schema with models and relationships
    Test->>Parser: Parse schema using updated filtering logic
    Parser-->>Test: Return validation results (success/failure)
    Test->>Test: Assert expected behavior and failure on delegate fields
Loading

Possibly related PRs

Suggested reviewers

  • ymc9

📜 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 8cef343 and b8059ee.

📒 Files selected for processing (3)
  • packages/schema/src/plugins/enhancer/enhance/index.ts (2 hunks)
  • packages/schema/src/plugins/zod/generator.ts (1 hunks)
  • tests/regression/tests/issue-2028.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/schema/src/plugins/zod/generator.ts
  • packages/schema/src/plugins/enhancer/enhance/index.ts
  • tests/regression/tests/issue-2028.test.ts
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: build-test (20.x)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: build-test (20.x)
  • GitHub Check: OSSAR-Scan
  • GitHub Check: build-test (20.x)
  • GitHub Check: dependency-review

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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: 0

🧹 Nitpick comments (1)
tests/regression/tests/issue-2028.test.ts (1)

5-5: Minor naming inconsistency in database name.

The database name 'issue-1955-1' doesn't match the issue number being tested (2028). This won't affect functionality but could be confusing.

-const dbUrl = await createPostgresDb('issue-1955-1');
+const dbUrl = await createPostgresDb('issue-2028');
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8934679 and 8cef343.

📒 Files selected for processing (3)
  • packages/schema/src/plugins/enhancer/enhance/index.ts (2 hunks)
  • packages/schema/src/plugins/zod/generator.ts (1 hunks)
  • tests/regression/tests/issue-2028.test.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: build-test (20.x)
  • GitHub Check: dependency-review
  • GitHub Check: build-test (20.x)
  • GitHub Check: build-test (20.x)
  • GitHub Check: OSSAR-Scan
🔇 Additional comments (6)
packages/schema/src/plugins/zod/generator.ts (1)

247-247: Good fix to correctly handle delegate auxiliary fields.

This change from using startsWith to includes ensures that fields with the DELEGATE_AUX_RELATION_PREFIX anywhere in their name are properly filtered out, not just at the beginning of the name. This aligns with the PR objective to fix delegate auxiliary fields appearing in WhereUniqueInput schemas.

packages/schema/src/plugins/enhancer/enhance/index.ts (3)

568-568: Consistent filtering of delegate auxiliary fields in properties.

This change from startsWith to includes provides more comprehensive filtering of auxiliary fields, ensuring that properties with DELEGATE_AUX_RELATION_PREFIX anywhere in their name are excluded.


571-571: Consistent filtering of delegate auxiliary fields in methods.

Similarly to property filtering, this change ensures methods with DELEGATE_AUX_RELATION_PREFIX anywhere in their name are properly excluded.


853-853: Updated auxiliary field detection logic.

Changed the filtering of auxiliary declarations to use includes instead of startsWith, maintaining consistency with the other changes and ensuring more comprehensive filtering.

tests/regression/tests/issue-2028.test.ts (2)

1-116: Well-structured regression test for issue #2028.

This test effectively validates that:

  1. The Zod schemas correctly exclude delegate fields from the UserFolderWhereUniqueInputObjectSchema
  2. The compound unique query functionality works as expected for both delegate model types

The test creates a comprehensive scenario with inheritance relationships (Foo, Bar, Baz) and many-to-many relationships (User-UserFolder-Foo), which thoroughly validates the fix.


56-73: Good negative testing for delegate auxiliary fields.

The test properly verifies that parsing attempts with delegate fields like userId_delegate_aux_UserFolder_fooId_Bar and userId_delegate_aux_UserFolder_fooId_Baz return failure, confirming that these fields are correctly excluded from the schema.

ymc9
ymc9 previously approved these changes Mar 7, 2025
Copy link
Member

@ymc9 ymc9 left a comment

Choose a reason for hiding this comment

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

Hey @diesal11 , good catch and many thanks for making the fix! Code looks great to me. I'll merge and release a patch.

@ymc9 ymc9 changed the base branch from main to dev March 7, 2025 18:01
@ymc9 ymc9 dismissed their stale review March 7, 2025 18:01

The base branch was changed.

@ymc9 ymc9 merged commit 5db6910 into zenstackhq:dev Mar 7, 2025
11 checks passed
@diesal11 diesal11 deleted the fix/issue-2028 branch March 8, 2025 07:22
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.

delegate aux fields appearing in WhereUniqueInput schema, when aux field is not first in a @@unique constraint

2 participants