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

Don't expose foreign keys as attributes in openAPI spec #1856

Merged
merged 3 commits into from
Nov 17, 2024

Conversation

thomassnielsen
Copy link
Contributor

These attributes are handled as relationships, and as such don't need to be exposed as attributes.

Copy link
Contributor

coderabbitai bot commented Nov 14, 2024

Important

Review skipped

Review was skipped due to path filters

⛔ 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

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request focus on the RESTfulOpenAPIGenerator class within rest-generator.ts. Modifications were made to improve the handling of data model fields during OpenAPI specification generation. Specifically, foreign key fields are now excluded from the attributes section of generated schemas, and the logic for determining required fields has been refined based on their definitions and operation contexts.

Changes

File Path Change Summary
packages/plugins/openapi/src/rest-generator.ts Updated generateModelEntity method to exclude foreign key fields from attributes and refined logic for required fields based on operation context.

Possibly related PRs

  • Mark openapi attributes as required for reads #1772: This PR modifies the generateModelEntity method in the RESTfulOpenAPIGenerator class to ensure that non-nullable attributes are marked as required in the OpenAPI schema, which aligns with the changes made in the main PR regarding the handling of required fields during OpenAPI specification generation.

Suggested reviewers

  • ymc9

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

🧹 Outside diff range and nitpick comments (1)
packages/plugins/openapi/src/rest-generator.ts (1)

871-874: LGTM! Consider adding a comment for clarity.

The implementation correctly excludes foreign key fields from being exposed as attributes in the OpenAPI specification, which aligns with the PR objective. This is the right approach as foreign keys should be treated as relationships rather than attributes.

Consider adding a brief comment explaining why foreign keys are excluded:

 if (isForeignKeyField(field)) {
-    // foreign keys are not exposed as attributes
+    // Skip foreign keys as they should be exposed as relationships rather than attributes
+    // to maintain proper REST/JSON:API resource relationships
     continue;
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 85a8d5d and 13d3914.

⛔ 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 (1)
  • packages/plugins/openapi/src/rest-generator.ts (1 hunks)
🔇 Additional comments (1)
packages/plugins/openapi/src/rest-generator.ts (1)

871-874: Verify the impact of this breaking change.

Removing foreign keys from attributes is a breaking change that could affect existing API clients. While this change is correct and aligns with REST/JSON:API best practices, we should ensure proper communication of this change.

Let's identify affected models and their foreign key fields:

Consider:

  1. Adding a breaking change notice in the changelog
  2. Updating API documentation to reflect this change
  3. Communicating to API consumers about migrating to using relationship endpoints
✅ Verification successful

Let me gather more specific information about the impact by searching for actual foreign key field usage in API clients and tests.

This will help us:

  1. Find actual usage of foreign key fields in tests
  2. Identify existing OpenAPI test cases that might need updates
  3. Locate any documentation that should be updated with breaking change information

Let me check the test baselines to understand how foreign keys are currently handled in the OpenAPI spec.

This will help us:

  1. Understand the current OpenAPI spec structure for relationships
  2. See how foreign keys are currently handled in test cases

Based on the analysis of the OpenAPI spec and test files, I can now provide a final response:

Breaking change is aligned with JSON:API specification and properly handled

The removal of foreign keys from attributes is a correct implementation that aligns with JSON:API specification. The analysis shows that:

  1. Relationship fields are properly exposed through dedicated relationship endpoints (e.g., /user/{id}/relationships/posts)
  2. The OpenAPI spec correctly documents relationship access through both relationship links and related resource links
  3. Test cases demonstrate proper handling of relationships through dedicated endpoints rather than as attributes

The change is safe because:

  • Foreign key fields are still accessible through relationship endpoints
  • The OpenAPI spec provides clear documentation for relationship access
  • Test baselines validate the correct JSON:API structure
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all models with foreign key fields to assess the impact
# Expected results: List of models and their foreign key fields that will no longer be exposed as attributes

# Search for model definitions with foreign key fields
ast-grep --pattern 'model $MODEL {
  $$$
  $FIELD $TYPE @relation($$$)
  $$$
}'

# Search for potential API clients using these fields
rg -l "\.attributes\." 

Length of output: 1614


Script:

#!/bin/bash
# Search for specific foreign key field access patterns in tests and API clients
rg -A 2 "\.attributes\.[a-zA-Z]+Id" packages/plugins/openapi/tests/ packages/server/tests/

# Check OpenAPI test cases and assertions specifically
rg -A 5 "openapi|swagger" packages/plugins/openapi/tests/

# Look for any documentation or migration guides
fd -e md . | xargs rg -l "breaking|migration|upgrade"

Length of output: 24813


Script:

#!/bin/bash
# Check the baseline files for foreign key handling
cat packages/plugins/openapi/tests/baseline/rest-3.1.0.baseline.yaml

# Look for relation field definitions in the schema
rg "@relation" -A 2 packages/plugins/openapi/tests/

Length of output: 130969

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.

Hi @thomassnielsen , thanks for continue improving this. Please see my comments.

@ymc9 ymc9 merged commit fdb6862 into zenstackhq:dev Nov 17, 2024
13 checks passed
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