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

OpenApi @@id support #1757

Merged
merged 17 commits into from
Oct 8, 2024
Merged

Conversation

thomassnielsen
Copy link
Contributor

Companion to #1754

@ymc9 how do I best update the baseline files with the extra model needed for new tests?

Copy link
Contributor

coderabbitai bot commented Oct 4, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request enhance the RESTfulOpenAPIGenerator class in the rest-generator.ts file to improve OpenAPI specification generation for RESTful APIs. Key updates include modifications to filter parameter generation, model entity generation, field filtering logic, response schemas, and error handling. Additionally, the test suite in openapi-restful.test.ts has been expanded to include new models and relationships, specifically the PostLike model, and to validate the updated functionality and security configurations.

Changes

File Path Change Summary
packages/plugins/openapi/src/rest-generator.ts - Updated generateFilterParameters to handle multiple ID fields.
- Modified generateModelEntity to expose components of compound IDs.
- Adjusted field filtering logic.
- Refined response schemas to include ID fields.
- Enhanced error handling methods.
packages/plugins/openapi/tests/openapi-restful.test.ts - Added PostLike model and associated relationships.
- Updated User and post_Item models to include likes field.
- Expanded test cases for new relationships and security schemes.
- Validated compound ID fields in tests.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant PostLike
    participant post_Item
    participant RESTfulOpenAPIGenerator

    User->>RESTfulOpenAPIGenerator: Request to generate OpenAPI spec
    RESTfulOpenAPIGenerator->>PostLike: Check for relationships
    PostLike-->>RESTfulOpenAPIGenerator: Return relationship data
    RESTfulOpenAPIGenerator->>post_Item: Check for likes field
    post_Item-->>RESTfulOpenAPIGenerator: Return likes field data
    RESTfulOpenAPIGenerator->>User: Generate OpenAPI spec with relationships
    User-->>RESTfulOpenAPIGenerator: Validate generated spec
Loading

Possibly related PRs

  • RESTApiHandler support compound ids (@@id) #1754: The changes in this PR enhance the handling of composite IDs in the RequestHandler class, which is directly related to the modifications in the RESTfulOpenAPIGenerator class regarding the handling of models with multiple ID fields. Both PRs focus on improving the functionality and representation of models with complex ID structures in the OpenAPI specifications.

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.

@ymc9
Copy link
Member

ymc9 commented Oct 5, 2024

Companion to #1754

@ymc9 how do I best update the baseline files with the extra model needed for new tests?

The test cases print the location of the generated yaml files to console. I usually just copy/paste its content over to the "baseline" folder and diff to make sure the changes are expected 😄.

@ymc9 ymc9 changed the base branch from main to dev October 6, 2024 14:42
@thomassnielsen thomassnielsen marked this pull request as ready for review October 7, 2024 10:26
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

🧹 Outside diff range and nitpick comments (3)
packages/plugins/openapi/tests/openapi-restful.test.ts (3)

38-38: Consider adding a description to the likes field in the User model

Adding a description to the likes field will enhance the generated OpenAPI documentation, making it clearer for API consumers.


59-59: Consider adding a description to the likes field in the post_Item model

Providing a description for the likes field will improve the OpenAPI documentation and help users understand its purpose in the API.


371-371: Remove unnecessary console.log statement

The console.log(JSON.stringify(parsed)); statement may clutter the test output. Consider removing it or using a logger with appropriate log levels.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b4418ac and 55d3c7a.

⛔ 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 (3 hunks)
  • packages/plugins/openapi/tests/openapi-restful.test.ts (4 hunks)
🧰 Additional context used
🔇 Additional comments (7)
packages/plugins/openapi/tests/openapi-restful.test.ts (3)

111-113: likes relationship paths for User model are correctly tested

The added test cases effectively verify that the OpenAPI paths for /user/{id}/relationships/likes are properly generated and accessible via GET, POST, and PATCH methods.


117-119: likes relationship paths for post_Item model are correctly tested

The new test assertions confirm that the OpenAPI specification includes the correct paths for /post_Item/{id}/relationships/likes, ensuring they support GET, POST, and PATCH methods as expected.


343-371: Test case for compound ID fields is well-implemented

The test accurately checks that individual fields from a compound ID (role, company) are exposed as attributes in the OpenAPI schema for the User model.

packages/plugins/openapi/src/rest-generator.ts (4)

412-413: Ensure consistent use of hasMultipleIds

The variable hasMultipleIds correctly identifies if the model has multiple ID fields. Verify that this variable is consistently used throughout the method to handle filter parameters appropriately for models with compound IDs.


421-422: Correctly filter ID fields based on the number of IDs

The condition if (isIdField(field) && !hasMultipleIds) ensures that ID fields are treated as regular filterable fields when the model has multiple IDs. This change allows clients to filter by each component of a compound ID, which enhances query flexibility.


849-851: Expose all fields in the schema for models with compound IDs

By setting fields to model.fields when there are multiple ID fields, all fields, including ID fields, are included in the schema. This adjustment ensures that each component of a compound ID is exposed as a separate field in the API schema, which is necessary for clients to construct and interpret compound IDs correctly.


894-895: Confirm ID field requirements in create requests

The logic adds the id property to the required fields if all ID fields lack a @default attribute. For models with compound IDs without default values, this enforces that all ID components must be provided during creation. Ensure that this behavior aligns with the expected API usage and that clients are aware of this requirement.

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/tests/openapi-restful.test.ts (1)

343-370: LGTM: New test case for compound ID fields

This new test case is a valuable addition to the test suite. It verifies that the OpenAPI generator correctly exposes individual fields from a compound ID as attributes in the generated specification. The test is well-structured and covers an important edge case in OpenAPI generation.

Suggestions for enhancement:

  1. Consider adding assertions to verify the data types of the 'role' and 'company' fields in the generated specification.
  2. It might be beneficial to add a similar test case for the REST API endpoints to ensure they correctly handle compound IDs in URL parameters.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 55d3c7a and 14827ee.

⛔ 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/tests/openapi-restful.test.ts (4 hunks)
🧰 Additional context used
🔇 Additional comments (5)
packages/plugins/openapi/tests/openapi-restful.test.ts (5)

38-38: LGTM: New relationship field added to User model

The addition of the likes PostLike[] field to the User model correctly establishes a relationship with the new PostLike model. This change is consistent with the PR objectives and enhances the model's functionality.


59-59: LGTM: New relationship field added to post_Item model

The addition of the likes PostLike[] field to the post_Item model correctly establishes a relationship with the new PostLike model. This change aligns with the PR objectives and enhances the model's functionality.


66-72: LGTM: New PostLike model added

The new PostLike model is well-structured with appropriate relationships to post_Item and User models. The use of a composite primary key @@id([postId, userId]) is correct for this many-to-many relationship.

Note: A previous suggestion to add timestamp fields was deemed unnecessary for testing purposes by the author.


111-113: LGTM: New assertions added for likes relationships

The new assertions correctly validate the generation of API paths for the likes relationship in both User and post_Item models. They cover the necessary HTTP methods (GET, POST, PATCH) and ensure that the OpenAPI specification accurately reflects the new model relationships.

Also applies to: 117-119


Line range hint 1-385: Overall assessment: Well-structured and comprehensive test additions

The changes in this file effectively enhance the test coverage for the OpenAPI Plugin RESTful functionality. The additions include:

  1. New model relationships for PostLike
  2. Assertions for API paths related to the new relationships
  3. A new test case for compound ID fields

These changes align well with the PR objectives and provide robust testing for the new features. The test suite now offers more comprehensive coverage of the OpenAPI specification generation process.

@thomassnielsen thomassnielsen requested a review from ymc9 October 8, 2024 11:35
@ymc9
Copy link
Member

ymc9 commented Oct 8, 2024

I'm merging it and will include the change in the upcoming 2.7.0 release.

@ymc9 ymc9 merged commit a2c78a1 into zenstackhq:dev Oct 8, 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