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(openapi): bugfix for compound id create and update #1855

Merged
merged 6 commits into from
Nov 26, 2024

Conversation

thomassnielsen
Copy link
Contributor

When creating a record with a compound id, Prisma expects the parts of the compound id to be passed as attributes. This change reflects that.

Copy link
Contributor

coderabbitai bot commented Nov 14, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces significant modifications to the RESTfulOpenAPIGenerator class in rest-generator.ts, focusing on the logic for generating OpenAPI schemas for data models. Key updates include changes to how ID fields are included in model entities and filter parameters, adjustments to required fields logic, and overall schema generation refinements. Additionally, extensive updates to the REST server tests in rest.test.ts enhance coverage for CRUD operations, error handling, and data validation, particularly concerning compound IDs and relationships.

Changes

File Path Change Summary
packages/plugins/openapi/src/rest-generator.ts Updated logic for ID fields in model entity generation, filter parameters, required fields, and schema structure.
packages/server/tests/api/rest.test.ts Extensive modifications to REST server tests, including CRUD operations, error handling, and validation for user and post entities. Method signatures for handlers updated.
packages/server/src/api/rest/index.ts Modified processUpdate method to change relationship handling from set to connect. Method signature updated.

Possibly related PRs

Suggested reviewers

  • ymc9

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4d95306 and de8aefa.

📒 Files selected for processing (1)
  • packages/plugins/openapi/src/rest-generator.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/plugins/openapi/src/rest-generator.ts

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.

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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 85a8d5d and 057c73b.

⛔ Files ignored due to path filters (2)
  • packages/plugins/openapi/tests/baseline/rest-3.0.0.baseline.yaml is excluded by !**/*.yaml
  • packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml is excluded by !**/*.yaml
📒 Files selected for processing (2)
  • packages/plugins/openapi/src/rest-generator.ts (2 hunks)
  • packages/server/tests/api/rest.test.ts (0 hunks)
💤 Files with no reviewable changes (1)
  • packages/server/tests/api/rest.test.ts
🔇 Additional comments (1)
packages/plugins/openapi/src/rest-generator.ts (1)

863-863: Approved: Updated field inclusion logic aligns with compound ID handling

The updated condition correctly includes all model fields when dealing with compound IDs (idFields.length > 1) in 'create' and 'read' operations, and excludes ID fields during 'update' operations. This change ensures that the OpenAPI schema accurately represents the requirements for compound ID handling in Prisma.

packages/plugins/openapi/src/rest-generator.ts Outdated Show resolved Hide resolved
@thomassnielsen thomassnielsen changed the title Bugfix for compound id create Bugfix for compound id create and update Nov 15, 2024
@thomassnielsen thomassnielsen requested a review from ymc9 November 15, 2024 09:44
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)
packages/server/tests/api/rest.test.ts (1)

2009-2032: LGTM! Consider enhancing test coverage.

The test case effectively validates the updating of compound IDs through relationship changes. The setup, execution, and assertions are well-structured and align with the PR objectives.

Consider adding additional assertions to verify:

  1. The old postLike record (1_user1) no longer exists
  2. The new postLike record (2_user1) exists with all attributes preserved
  3. The relationships are properly updated on both posts
 expect(r.status).toBe(200);
 expect(r.body.data.id).toBe(`2${idDivider}user1`);
+
+// Verify old record is gone
+const oldRecord = await prisma.postLike.findUnique({
+    where: { postId_userId: { postId: 1, userId: 'user1' } }
+});
+expect(oldRecord).toBeNull();
+
+// Verify new record exists with preserved attributes
+const newRecord = await prisma.postLike.findUnique({
+    where: { postId_userId: { postId: 2, userId: 'user1' } }
+});
+expect(newRecord).toMatchObject({
+    superLike: false,
+    userId: 'user1',
+    postId: 2
+});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 057c73b and 4d95306.

📒 Files selected for processing (3)
  • packages/plugins/openapi/src/rest-generator.ts (2 hunks)
  • packages/server/src/api/rest/index.ts (1 hunks)
  • packages/server/tests/api/rest.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/plugins/openapi/src/rest-generator.ts
🔇 Additional comments (1)
packages/server/src/api/rest/index.ts (1)

962-962: Correct fix for handling compound IDs in single relationships.

The change from set to connect is the correct approach. When dealing with single relationships in Prisma, especially those involving compound IDs, using connect is more appropriate than set because:

  1. connect establishes a relationship with an existing record using its ID(s)
  2. It aligns with Prisma's requirement of supplying compound ID components as separate attributes
  3. It maintains referential integrity by ensuring the related record exists

Let's verify the usage of compound IDs in relationship connections:

✅ Verification successful

Let me generate additional verification scripts to check the implementation of compound ID handling:


Based on the verification results, I can now provide the final response:

The change to connect is correct and properly handles compound IDs.

The implementation shows that:

  1. makeIdConnect method properly handles both single and compound IDs:

    • For single IDs: Creates a simple connect object with the ID field
    • For compound IDs: Creates a connect object with Prisma's compound ID format using makePrismaIdKey
  2. The switch from set to connect aligns with Prisma's requirements for establishing relationships, especially for compound IDs where individual components need to be supplied as separate attributes.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other instances of relationship handling with compound IDs
# to ensure consistent usage of `connect` over `set`.

# Search for relationship handling patterns
rg -A 5 'connect:|set:' 

# Search for compound ID handling
ast-grep --pattern 'makePrismaIdKey|makeCompoundId'

Length of output: 229737


Script:

#!/bin/bash
# Search for compound ID handling implementation and usage
rg -A 5 "makePrismaIdKey|makeCompoundId|makeIdConnect" packages/server/src/api/rest/index.ts

# Search for relationship handling patterns to understand the context
ast-grep --pattern 'makeIdConnect($_, $_)'

Length of output: 3120

@thomassnielsen
Copy link
Contributor Author

@ymc9 could we make sure this makes it into the next release? It's a blocking issue on our end.

@ymc9 ymc9 changed the title Bugfix for compound id create and update fix(server): bugfix for compound id create and update Nov 26, 2024
@ymc9 ymc9 changed the title fix(server): bugfix for compound id create and update fix(openapi): bugfix for compound id create and update Nov 26, 2024
@ymc9 ymc9 merged commit 545898b into zenstackhq:dev Nov 26, 2024
13 checks passed
@ymc9
Copy link
Member

ymc9 commented Nov 27, 2024

Hi @thomassnielsen , v2.9.4 is live now!

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.

2 participants