Skip to content

Conversation

@ymc9
Copy link
Member

@ymc9 ymc9 commented Apr 15, 2025

fixes #2084

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 15, 2025

📝 Walkthrough

Walkthrough

The changes extend the OpenAPI schema generator to support TypeDef declarations, enabling fields with custom types and the @json attribute to be correctly represented as JSON objects in the generated OpenAPI specification. The generator now processes TypeDef types alongside DataModel and Enum, creating appropriate OpenAPI schema components for them. The test schema is updated to include a Meta type and new fields in the Foo model to verify correct handling of both custom JSON object fields and plain JSON fields.

Changes

File(s) Change Summary
packages/plugins/openapi/src/rest-generator.ts Extended OpenAPI generation to support TypeDef declarations, updated methods to handle TypeDef and JSON fields, added logic for generating schema components for TypeDef.
packages/plugins/openapi/tests/openapi-restful.test.ts Updated test schema: added Meta type, and two new fields (json with @json and plainJson of type Json) to Foo model for testing JSON and custom types.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant RESTfulOpenAPIGenerator
    participant Schema

    User->>RESTfulOpenAPIGenerator: Generate OpenAPI schema
    RESTfulOpenAPIGenerator->>Schema: Retrieve DataModel, Enum, TypeDef declarations
    RESTfulOpenAPIGenerator->>RESTfulOpenAPIGenerator: generateComponents for DataModel, Enum, TypeDef
    RESTfulOpenAPIGenerator->>RESTfulOpenAPIGenerator: generateTypeDefComponent for each TypeDef
    RESTfulOpenAPIGenerator->>RESTfulOpenAPIGenerator: generateField for each field (DataModelField or TypeDefField)
    RESTfulOpenAPIGenerator->>RESTfulOpenAPIGenerator: fieldTypeToOpenAPISchema for DataModelFieldType or TypeDefFieldType
    RESTfulOpenAPIGenerator-->>User: Return OpenAPI schema with TypeDef and JSON support
Loading

Assessment against linked issues

Objective Addressed Explanation
Support for @json attribute and TypeDef in OpenAPI schema generation (#2084)
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 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)
packages/plugins/openapi/src/rest-generator.ts (1)

793-800: Return type annotation removed from generateEnumComponent.

The explicit return type annotation was removed while preserving the logic. While this doesn't affect functionality, consider maintaining consistent typing across similar methods.

Consider adding the explicit return type OAPI.SchemaObject for consistency with other component generation methods:

-private generateEnumComponent(_enum: Enum) {
+private generateEnumComponent(_enum: Enum): OAPI.SchemaObject {
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 401f45d and 9c35f9d.

⛔ Files ignored due to path filters (2)
  • packages/plugins/openapi/tests/baseline/rest-type-coverage-3.0.0.baseline.yaml is excluded by !**/*.yaml
  • packages/plugins/openapi/tests/baseline/rest-type-coverage-3.1.0.baseline.yaml is excluded by !**/*.yaml
📒 Files selected for processing (2)
  • packages/plugins/openapi/src/rest-generator.ts (6 hunks)
  • packages/plugins/openapi/tests/openapi-restful.test.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: OSSAR-Scan
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: build-test (20.x)
  • GitHub Check: build-test (20.x)
🔇 Additional comments (8)
packages/plugins/openapi/src/rest-generator.ts (6)

15-27: Imports expanded to support TypeDef declarations.

The imports have been updated to include the necessary TypeDef-related types from the SDK's AST module, which is essential for properly supporting TypeDef declarations in the OpenAPI schema generation.


508-511: Appropriate JSON handling for filter parameters.

Fields referencing a TypeDef or of type Json are now correctly treated as strings with JSON format when generating filter parameters. This ensures proper OpenAPI representation for JSON fields in query parameters.


557-560: TypeDef components generation added.

Iterating through TypeDef declarations to generate corresponding schema components completes the support for custom type definitions in the OpenAPI specification.


802-812: New TypeDef component generator implementation.

The implementation correctly generates OpenAPI schema objects for TypeDef declarations, creating properties for each field in the TypeDef using the existing field generation logic.


979-984: Field generator updated to handle TypeDef fields.

The generateField method now accepts both DataModelField and TypeDefField types, enabling it to process fields from both data models and type definitions while maintaining the same logic.


986-988: Field type to OpenAPI schema conversion updated.

The fieldTypeToOpenAPISchema method now handles both DataModelFieldType and TypeDefFieldType, ensuring consistent schema generation regardless of where the field is defined.

packages/plugins/openapi/tests/openapi-restful.test.ts (2)

309-311: Added TypeDef declaration for testing.

A new Meta type has been added with a string field, which provides a good test case for the TypeDef component generation implementation.


324-325: Test coverage added for JSON fields.

Two new field types have been added to the test model:

  1. A nullable Meta type with the @json attribute, testing custom JSON object serialization
  2. A plain Json field, testing the standard JSON type handling

These additions provide complete test coverage for the new JSON field support.

@ymc9 ymc9 merged commit c41addd into dev Apr 15, 2025
11 of 12 checks passed
@ymc9 ymc9 deleted the fix/issue-2084 branch April 15, 2025 23:15
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