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

Implement API Model Auto Generation using openapi-generation #442

Merged
merged 8 commits into from
Feb 1, 2025

Conversation

devleejb
Copy link
Member

@devleejb devleejb commented Jan 31, 2025

What this PR does / why we need it:
This pull request includes the addition of several new models to the backend-go/api/codepair/v1 directory, as well as documentation updates. The changes introduce new request and response models for various API endpoints and provide instructions for generating these models from the OpenAPI specification.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Additional documentation:


Checklist:

  • Added relevant tests or not required
  • Didn't break anything

Summary by CodeRabbit

  • New Features

    • Added comprehensive API specification for CodePair, including user authentication, workspace management, document handling, and file operations.
    • Introduced new endpoints for GitHub login, token refresh, workspace creation, and management.
    • Implemented file upload and download functionality with presigned URLs.
    • Added support for document sharing and intelligence features.
  • Documentation

    • Created detailed README for backend API model generation.
    • Added OpenAPI specification documentation.
  • Chores

    • Updated package configuration for backend code generation.
    • Added OpenAPI tools configuration.

Copy link
Contributor

coderabbitai bot commented Jan 31, 2025

Warning

Rate limit exceeded

@devleejb has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 5 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 14bf726 and b977e51.

📒 Files selected for processing (5)
  • api/backend-openapi-spec.json (1 hunks)
  • backend-go/api/codepair/v1/README.md (1 hunks)
  • backend-go/api/codepair/v1/models/model_check_name_conflict_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_refresh_token_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_refresh_token_response.go (1 hunks)

Walkthrough

This pull request introduces a new OpenAPI specification (version 3.0.0) for the CodePair API, detailing multiple endpoints for user authentication, workspace management, document handling, and utility functions. It includes comprehensive request and response schemas to ensure structured data representation and error handling. Additionally, new Go model structs are generated to provide type-safe representations of the API's data structures, supporting robust backend implementation. A README file and configuration for the OpenAPI generator CLI are also included to facilitate development.

Changes

File Change Summary
api/backend-openapi-spec.json New OpenAPI specification defining comprehensive API endpoints for user, workspace, and document management
backend-go/api/codepair/v1/README.md Added documentation for model generation process and guidelines
backend-go/api/codepair/v1/models/ Added multiple model structs for request/response handling across various API functionalities
openapitools.json New configuration for OpenAPI generator CLI
package.json Added scripts for backend and frontend code generation, along with OpenAPI generator CLI dependency

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant AuthService
    participant WorkspaceService
    participant DocumentService

    Client->>AuthService: Login with GitHub
    AuthService-->>Client: Return Access & Refresh Tokens
    
    Client->>WorkspaceService: Create Workspace
    WorkspaceService-->>Client: Return Workspace Details
    
    Client->>DocumentService: Create Document in Workspace
    DocumentService-->>Client: Return Document Metadata
Loading

Possibly related PRs

Suggested labels

enhancement 🌟

Poem

🐰 A Rabbit's API Ballad 🌈
In lines of JSON, a spec takes flight,
Endpoints dancing with pure delight
Workspaces, docs, and tokens so bright
CodePair's backend, a technological might!
Hop, hop, hooray for code's new height! 🚀


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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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. (Beta)
  • @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: 20

🧹 Nitpick comments (12)
backend-go/api/codepair/v1/models/model_file_upload_config.go (1)

5-6: Enhance field documentation.

While the documentation is present, consider making it more descriptive by explaining the implications of enabling/disabling file uploads.

-	// Whether file upload is enabled
+	// Enable controls whether file uploads are allowed in the system.
+	// When set to true, users can upload files; when false, file upload functionality is disabled.
backend-go/api/codepair/v1/models/model_check_name_conflict_request.go (1)

5-6: Add validation rules to documentation.

Consider enhancing the documentation to specify any validation rules for the name field (e.g., allowed characters, length limits).

-	// Name to check for conflict
+	// Name to check for conflict. The name should follow the system's naming rules
+	// and will be checked against existing names to ensure uniqueness.
backend-go/api/codepair/v1/models/model_change_nickname_request.go (1)

5-6: Add validation rules to documentation.

Consider enhancing the documentation to specify any validation rules for the nickname field (e.g., allowed characters, length limits).

-	// New nickname to update
+	// New nickname to update. The nickname should follow the system's naming conventions
+	// and any length or character restrictions defined in the validation rules.
backend-go/api/codepair/v1/models/model_create_workspace_request.go (2)

5-6: Add validation rules to documentation.

Consider enhancing the documentation to specify any validation rules for the workspace title (e.g., allowed characters, length limits).

-	// Title for the new workspace
+	// Title for the new workspace. The title should follow workspace naming conventions
+	// and any length or character restrictions defined in the validation rules.

1-7: Consider adding validation at the OpenAPI specification level.

Since these models are auto-generated, consider defining validation rules (e.g., minLength, maxLength, pattern) in the OpenAPI specification. This would ensure consistent validation across different generated clients and servers.

backend-go/api/codepair/v1/models/model_yorkie_intelligence_config.go (1)

3-10: Consider using stronger types for configuration values.

While using map[string]interface{} provides flexibility, it could lead to runtime type assertion issues. Consider:

  1. Using a more specific type for config values if the possible types are known
  2. Adding validation methods for the config map
  3. Documenting expected config key-value pairs

Example of a more type-safe approach:

type ConfigValue struct {
    Type  string      `json:"type"`  // e.g., "string", "number", "boolean"
    Value interface{} `json:"value"`
}

type YorkieIntelligenceConfig struct {
    Enable bool                    `json:"enable"`
    Config map[string]ConfigValue `json:"config"`
}
backend-go/api/codepair/v1/models/model_create_workspace_document_share_token_request.go (1)

7-14: Add role validation and expiration constraints.

The model could benefit from:

  1. Defining valid roles as constants/enums
  2. Adding validation for the expiration time

Example implementation:

const (
    RoleReader = "reader"
    RoleWriter = "writer"
    // Add other valid roles...
)

type CreateWorkspaceDocumentShareTokenRequest struct {
    Role      string    `json:"role" validate:"oneof=reader writer"`
    ExpiredAt time.Time `json:"expiredAt" validate:"gt=now"`
}

func (r *CreateWorkspaceDocumentShareTokenRequest) Validate() error {
    if !isValidRole(r.Role) {
        return fmt.Errorf("invalid role: %s", r.Role)
    }
    if r.ExpiredAt.Before(time.Now()) {
        return fmt.Errorf("expiration time must be in the future")
    }
    return nil
}
backend-go/api/codepair/v1/models/model_find_settings_response.go (1)

3-10: Clean composition of configuration types!

The model effectively combines the configuration types with clear documentation. Consider documenting whether these fields can be nil and how they should be handled by consumers.

Consider adding a comment about nil handling:

type FindSettingsResponse struct {
    // Settings related to Yorkie Intelligence
    // This field will never be nil in responses
    YorkieIntelligence YorkieIntelligenceConfig `json:"yorkieIntelligence"`

    // Settings related to file uploads
    // This field will never be nil in responses
    FileUpload FileUploadConfig `json:"fileUpload"`
}
backend-go/api/codepair/v1/models/model_export_file_request.go (1)

3-13: Consider adding validation for ExportType field.

While the struct is well-designed, consider adding validation to ensure ExportType only accepts valid values (e.g., "PDF", "HTML").

Example validation implementation:

var ValidExportTypes = map[string]bool{
    "PDF":  true,
    "HTML": true,
}

func (r *ExportFileRequest) Validate() error {
    if !ValidExportTypes[r.ExportType] {
        return fmt.Errorf("invalid export type: %s", r.ExportType)
    }
    return nil
}
backend-go/api/codepair/v1/models/model_workspace_domain.go (1)

1-1: Consider updating the OpenAPI specification.

Since these models are auto-generated, the identified issues (float32 for counts, naming conventions) should be addressed in the OpenAPI specification itself. Consider:

  1. Using integer types for count fields in the spec
  2. Following Go naming conventions (URL, ID) in the spec's schema definitions

This will ensure the generated code follows Go best practices.

api/backend-openapi-spec.json (1)

906-916: Add maximum length validation for nickname.

The ChangeNicknameRequest schema has a minimum length validation but lacks a maximum length constraint.

Add a maximum length validation:

       "ChangeNicknameRequest": {
         "type": "object",
         "properties": {
           "nickname": {
             "type": "string",
             "description": "New nickname to update",
-            "minLength": 2
+            "minLength": 2,
+            "maxLength": 50
           }
         },
         "required": ["nickname"]
       },
backend-go/api/codepair/v1/README.md (1)

3-3: Use a more robust path reference.

The relative path to the OpenAPI spec (../../../../api/backend-openapi-spec.json) is fragile and could break if the directory structure changes.

Consider using a path relative to the repository root:

-The types in `./models` are all automatically generated from the [OpenAPI spec](../../../../api/backend-openapi-spec.json).
+The types in `./models` are all automatically generated from the [OpenAPI spec](/api/backend-openapi-spec.json).
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between fe44e3d and d4d8f63.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (38)
  • api/backend-openapi-spec.json (1 hunks)
  • backend-go/api/codepair/v1/README.md (1 hunks)
  • backend-go/api/codepair/v1/models/model_change_nickname_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_check_name_conflic_reponse.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_check_name_conflict_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_check_yorkie_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_check_yorkie_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_create_invitation_token_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_create_invitation_token_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_create_upload_presigned_url_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_create_upload_presigned_url_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_create_workspace_document_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_create_workspace_document_share_token_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_create_workspace_document_share_token_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_create_workspace_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_export_file_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_export_file_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_file_upload_config.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_find_document_from_sharing_token_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_find_settings_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_find_user_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_find_workspace_documents_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_find_workspace_users_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_find_workspaces_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_http_exception_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_join_workspace_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_login_response.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_refresh_token_request_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_refresh_token_response_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_run_feature_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_run_follow_up_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_update_document_title_request.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_workspace_document_domain.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_workspace_domain.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_workspace_user_domain.go (1 hunks)
  • backend-go/api/codepair/v1/models/model_yorkie_intelligence_config.go (1 hunks)
  • openapitools.json (1 hunks)
  • package.json (2 hunks)
✅ Files skipped from review due to trivial changes (3)
  • backend-go/api/codepair/v1/models/model_refresh_token_request_request.go
  • backend-go/api/codepair/v1/models/model_create_invitation_token_response.go
  • openapitools.json
🧰 Additional context used
🪛 golangci-lint (1.62.2)
backend-go/api/codepair/v1/models/model_http_exception_response.go

[warning] 3-3: var-naming: type HttpExceptionResponse should be HTTPExceptionResponse

(revive)

backend-go/api/codepair/v1/models/model_workspace_domain.go

[warning] 10-10: var-naming: struct field Id should be ID

(revive)

backend-go/api/codepair/v1/models/model_run_follow_up_request.go

[warning] 6-6: var-naming: struct field DocumentId should be DocumentID

(revive)

backend-go/api/codepair/v1/models/model_workspace_document_domain.go

[warning] 10-10: var-naming: struct field Id should be ID

(revive)


[warning] 13-13: var-naming: struct field YorkieDocumentId should be YorkieDocumentID

(revive)

backend-go/api/codepair/v1/models/model_create_upload_presigned_url_response.go

[warning] 3-3: var-naming: type CreateUploadPresignedUrlResponse should be CreateUploadPresignedURLResponse

(revive)

backend-go/api/codepair/v1/models/model_find_document_from_sharing_token_response.go

[warning] 13-13: var-naming: struct field YorkieDocumentId should be YorkieDocumentID

(revive)

backend-go/api/codepair/v1/models/model_create_upload_presigned_url_request.go

[warning] 3-3: var-naming: type CreateUploadPresignedUrlRequest should be CreateUploadPresignedURLRequest

(revive)

backend-go/api/codepair/v1/models/model_run_feature_request.go

[warning] 6-6: var-naming: struct field DocumentId should be DocumentID

(revive)

backend-go/api/codepair/v1/models/model_workspace_user_domain.go

[warning] 10-10: var-naming: struct field Id should be ID

(revive)

🪛 Checkov (3.2.334)
api/backend-openapi-spec.json

[HIGH] 1-1449: Ensure that the global security field has rules defined

(CKV_OPENAPI_4)


[HIGH] 1-1449: Ensure that security operations is not empty.

(CKV_OPENAPI_5)

🪛 LanguageTool
backend-go/api/codepair/v1/README.md

[duplication] ~10-~10: Possible typo: you repeated a word.
Context: .... Move to the root of the repository 2. Run pnpm install 3. Run pnpm run generate:backend

(ENGLISH_WORD_REPEAT_RULE)

🔇 Additional comments (10)
backend-go/api/codepair/v1/models/model_update_document_title_request.go (1)

3-7: LGTM!

The struct is well-defined with proper documentation and follows Go conventions for JSON tags.

backend-go/api/codepair/v1/models/model_create_workspace_document_request.go (1)

3-7: LGTM!

The struct is well-defined with proper documentation and follows Go conventions for JSON tags.

backend-go/api/codepair/v1/models/model_join_workspace_request.go (1)

1-7: LGTM!

The struct is well-defined with proper documentation and JSON tags.

backend-go/api/codepair/v1/models/model_create_workspace_document_share_token_response.go (1)

1-7: LGTM!

The struct is well-defined with proper documentation and JSON tags.

backend-go/api/codepair/v1/models/model_create_invitation_token_request.go (1)

1-11: LGTM!

The struct is well-defined with proper documentation, JSON tags, and time.Time import.

backend-go/api/codepair/v1/models/model_login_response.go (1)

3-10: LGTM!

The struct is well-documented and follows Go conventions.

backend-go/api/codepair/v1/models/model_find_workspaces_response.go (1)

3-10: Verify the WorkspaceDomain type.

The struct looks good, but let's verify the existence and implementation of the WorkspaceDomain type.

✅ Verification successful

WorkspaceDomain type verification successful

The WorkspaceDomain type is properly defined with all necessary fields and follows Go conventions. The type exists in the same package and is correctly structured for use in the FindWorkspacesResponse.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the WorkspaceDomain type exists and is properly defined

# Search for WorkspaceDomain type definition
rg -t go "type WorkspaceDomain struct" backend-go/

Length of output: 142


Script:

#!/bin/bash
# Description: Inspect the WorkspaceDomain implementation
cat backend-go/api/codepair/v1/models/model_workspace_domain.go

Length of output: 442

backend-go/api/codepair/v1/models/model_check_yorkie_response.go (1)

3-10: Well-structured response model with clear documentation!

The CheckYorkieResponse struct is well-designed with appropriate field types and clear documentation for each field. The JSON serialization tags follow standard naming conventions.

backend-go/api/codepair/v1/models/model_check_yorkie_request.go (1)

3-13: LGTM! Well-structured request model with clear documentation.

The struct is well-designed with appropriate field types and helpful documentation for each field.

package.json (1)

17-18: LGTM! OpenAPI generator integration looks good.

The changes correctly set up the OpenAPI generator:

  • Added script to generate Go Echo server models
  • Added required openapi-generator-cli dependency

Also applies to: 28-29

Copy link
Member

@kokodak kokodak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)
I believe this can be merged quickly, so I have left just one simple suggestion.

backend-go/api/codepair/v1/README.md Outdated Show resolved Hide resolved
@devleejb devleejb merged commit 67c689c into main Feb 1, 2025
1 check passed
@devleejb devleejb deleted the openapi-generation branch February 1, 2025 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants