Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid reference unused enums in generated policy code #1851

Merged
merged 2 commits into from
Nov 14, 2024
Merged

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Nov 14, 2024

No description provided.

Copy link
Contributor

coderabbitai bot commented Nov 14, 2024

📝 Walkthrough

Walkthrough

The pull request introduces a new function getDataModels from @zenstackhq/sdk to enhance the isEnumReferenced function. This modification allows isEnumReferenced to retrieve and check all data models associated with a given model for references to a specified enum, rather than solely relying on the initial model. Additionally, a new regression test suite has been added to validate this functionality, focusing on an issue related to enum handling.

Changes

File Path Change Summary
packages/schema/src/plugins/enhancer/policy/utils.ts - Introduced getDataModels function.
- Modified isEnumReferenced to check all associated data models for enum references.
tests/regression/tests/issue-1835.test.ts - Added a new regression test suite for issue 1835 with a test case that loads a schema containing enums and models.

Possibly related PRs


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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 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.

@ymc9
Copy link
Member Author

ymc9 commented Nov 14, 2024

fixes #1835

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

🧹 Outside diff range and nitpick comments (1)
tests/regression/tests/issue-1835.test.ts (1)

3-4: Add test description documenting the regression case

Consider adding a comment before the test case explaining:

  1. What issue this regression test is preventing
  2. The expected behavior regarding enum references in ignored models

Example:

 describe('issue 1835', () => {
+    // Ensures enums referenced only in ignored models are properly handled
+    // and not incorrectly marked as unused in generated policy code
     it('regression', async () => {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 04184b9 and 34af96f.

📒 Files selected for processing (2)
  • packages/schema/src/plugins/enhancer/policy/utils.ts (2 hunks)
  • tests/regression/tests/issue-1835.test.ts (1 hunks)
🔇 Additional comments (5)
tests/regression/tests/issue-1835.test.ts (3)

1-4: LGTM: Test structure follows best practices

The test setup is clean and follows standard Jest/testing conventions with proper describe/it blocks and async handling.


22-25: LGTM: Test configuration is appropriate

The PostgreSQL provider with pushDb: false is suitable for this test case as we're testing schema validation rather than actual database operations.


7-16: Verify test coverage for enum reference scenarios

The test case defines an enum that's only referenced in an ignored model (@@ignore). While this tests one scenario, consider adding additional test cases for:

  1. Enums referenced in multiple models
  2. Enums referenced in models with different directives
  3. Completely unused enums

Would you like me to help generate additional test cases to improve coverage?

✅ Verification successful

Let me analyze the test coverage patterns for enums with this broader search approach.


Test coverage for enum references is comprehensive

Based on the search results, the codebase already has good test coverage for enum references across various scenarios:

  1. Enums referenced in multiple models:

    • issue-1745.test.ts has multiple enums (BuyerType, ChainStore) used in models
    • issue-1381.test.ts tests multiple enums (MemberRole, SpaceType) in related models
  2. Enums with different directives:

    • issue-1210.test.ts tests enum with @default and @deny directives
    • issue-1257.test.ts tests enum with @@auth directive
    • issue-714.test.ts tests enum in relation fields
  3. Unused enums:

    • issue-674.test.ts explicitly tests an unused enum with MyUnUsedEnum
    • Current test case (issue-1835.test.ts) covers enum in ignored model

The existing test suite comprehensively covers the suggested scenarios, making additional test cases unnecessary.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if there are other test cases covering enum references
rg -l "enum.*model.*@@ignore" tests/

Length of output: 38


Script:

#!/bin/bash
# Search for enum definitions and their usage patterns in test files
rg -A 10 "enum\s+\w+" tests/ --type ts

# Also search for test files containing both enum and model definitions
rg -l "enum.*\n.*model" tests/ --type ts

Length of output: 16814

packages/schema/src/plugins/enhancer/policy/utils.ts (2)

11-11: LGTM: Import addition is clean and well-placed

The new import is properly grouped with other imports from the same package and is utilized in the code.


541-553: LGTM: Enhanced enum reference checking implementation

The implementation properly extends enum reference checking to all related data models, which should fix the issue with unused enums in generated policy code. The logic maintains thorough checking for both field types and direct enum references.

Let's verify the performance impact of checking all data models:

@ymc9 ymc9 merged commit e41a927 into dev Nov 14, 2024
13 checks passed
@ymc9 ymc9 deleted the fix/issue-1835 branch November 14, 2024 04: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.

1 participant