Rocket.Chat icon indicating copy to clipboard operation
Rocket.Chat copied to clipboard

feat: Add OpenAPI Support to Statistics API

Open ahmed-n-abdeltwab opened this issue 8 months ago β€’ 44 comments

Description:
This PR integrates OpenAPI support into the Rocket.Chat API, migrate of Rocket.Chat API endpoints to the new OpenAPI pattern. The update includes improved API documentation, enhanced type safety, and response validation using AJV.

Key Changes:

  • Implement the OpenAPI pattern with better route handling.
  • Added AJV-based schema validation for API responses to ensure type safety.
  • Improved the code structure using chained route methods for better maintainability and readability.
  • No breaking changes

Issue Reference:
Relates to #34983, part of the ongoing OpenAPI integration effort.

Testing:

  • Validated that the endpoints return correctly formatted responses.
  • Verified AJV schema validation for the responses.
  • Ensured that the API documentation matches the updated response schema.
  • No breaking changes

Endpoints Migration:

  • [x] statistics:
    • [x] Get Last Statistics
    • [x] Get Statistics List

Looking forward to your feedback! πŸš€


Description

This pull request introduces OpenAPI support to the statistics API in the ahmed-n-abdeltwab/Rocket.Chat repository. The changes are made in the apps/meteor/app/api/server/v1/stats.ts file. The update involves refactoring the statistics API routes to incorporate typed validation using ajv schema definitions. This enhancement aims to improve type safety and request validation while preserving the existing functionality of the API. The changes are proposed from the feat/OpenAPI branch to be merged into the develop branch.


  • To see the specific tasks where the Asana app for GitHub is being used, see below:
    • https://app.asana.com/0/0/1210463245358771
    • https://app.asana.com/0/0/1210463247551569

ahmed-n-abdeltwab avatar Apr 03 '25 06:04 ahmed-n-abdeltwab

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label
  • This PR is missing the required milestone or project

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

dionisio-bot[bot] avatar Apr 03 '25 06:04 dionisio-bot[bot]

πŸ¦‹ Changeset detected

Latest commit: 2748afa7b596c7c050faf43e55dd3be3cec3dab4

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Apr 03 '25 06:04 changeset-bot[bot]

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Apr 03 '25 06:04 CLAassistant

@kody start-review

ahmed-n-abdeltwab avatar Apr 03 '25 06:04 ahmed-n-abdeltwab

Code Review Completed! πŸ”₯

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a πŸ‘ for helpful suggestions or a πŸ‘Ž if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security βœ…
Code Style βœ…
Kody Rules βœ…
Refactoring βœ…
Error Handling βœ…
Maintainability βœ…
Potential Issues βœ…
Documentation And Comments βœ…
Performance And Optimization βœ…
Breaking Changes ❌

Access your configuration settings here.

​

kody-ai[bot] avatar Apr 03 '25 06:04 kody-ai[bot]

I usually run yarn lint -- --fix before committing, but this time, after running it, I noticed that it applied fixes across the entire project. As a result, when I added my file to Git and committed, it unintentionally included changes from the entire codebase.

To resolve this, I reverted the unintended changes and manually fixed only my file using the following command:

npx eslint --parser @typescript-eslint/parser --parser-options "project: ['./tsconfig.base.json']" apps/meteor/app/api/server/v1/stats.ts --fix

This ensured that only the intended file was fixed and committed correctly.
Should I report this as an issue, or should we wait to see if it happens again?

ahmed-n-abdeltwab avatar Apr 03 '25 14:04 ahmed-n-abdeltwab

@kody start-review

ahmed-n-abdeltwab avatar Apr 03 '25 15:04 ahmed-n-abdeltwab

Kody Review Complete

Great news! πŸŽ‰ No issues were found that match your current review configurations.

Keep up the excellent work! πŸš€

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a πŸ‘ for helpful suggestions or a πŸ‘Ž if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security βœ…
Code Style βœ…
Kody Rules βœ…
Refactoring βœ…
Error Handling βœ…
Maintainability βœ…
Potential Issues βœ…
Documentation And Comments βœ…
Performance And Optimization βœ…
Breaking Changes ❌

Access your configuration settings here.

​

kody-ai[bot] avatar Apr 03 '25 15:04 kody-ai[bot]

@kody start-review

ahmed-n-abdeltwab avatar Apr 03 '25 18:04 ahmed-n-abdeltwab

Code Review Completed! πŸ”₯

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a πŸ‘ for helpful suggestions or a πŸ‘Ž if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security βœ…
Code Style βœ…
Kody Rules βœ…
Refactoring βœ…
Error Handling βœ…
Maintainability βœ…
Potential Issues βœ…
Documentation And Comments βœ…
Performance And Optimization βœ…
Breaking Changes ❌

Access your configuration settings here.

​

kody-ai[bot] avatar Apr 03 '25 18:04 kody-ai[bot]

@kody start-review

ahmed-n-abdeltwab avatar Apr 03 '25 18:04 ahmed-n-abdeltwab

Kody Review Complete

Great news! πŸŽ‰ No issues were found that match your current review configurations.

Keep up the excellent work! πŸš€

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a πŸ‘ for helpful suggestions or a πŸ‘Ž if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security βœ…
Code Style βœ…
Kody Rules βœ…
Refactoring βœ…
Error Handling βœ…
Maintainability βœ…
Potential Issues βœ…
Documentation And Comments βœ…
Performance And Optimization βœ…
Breaking Changes ❌

Access your configuration settings here.

​

kody-ai[bot] avatar Apr 03 '25 18:04 kody-ai[bot]

@kody start-review

ahmed-n-abdeltwab avatar Apr 04 '25 04:04 ahmed-n-abdeltwab

Hi @ggazzo,

I've refactored the statistics endpoints to improve:

  1. Code organization (separated handlers from route configs)
  2. Schema validation (additionalProperties: false, added descriptions)
  3. Documentation (OpenAPI compatibility)

Could you please review these changes and share your feedback.

ahmed-n-abdeltwab avatar Apr 04 '25 04:04 ahmed-n-abdeltwab

there is no reason to "over-optimize" the code.

keep the functions and schemas directly in the endpoint declarations.

this way TypeScript can infer things like this, and in this case, since we have several schemas and actions, it is better to understand which one belongs what

I see your point! My thought was to improve maintainability by keeping things modular, but I can inline them if that's the preferred structure. Let me know if you want any adjustments!

ahmed-n-abdeltwab avatar Apr 04 '25 16:04 ahmed-n-abdeltwab

@kody start-review

ahmed-n-abdeltwab avatar Apr 04 '25 17:04 ahmed-n-abdeltwab

Hey @ggazzo, I’ve made the recent changes you suggested by keeping the functions and schemas inline within the endpoint declarations. I also added types for the query parameters and body, and fixed the schema naming to be uppercase. Let me know if you have any feedback!

ahmed-n-abdeltwab avatar Apr 04 '25 18:04 ahmed-n-abdeltwab

@ahmed-n-abdeltwab how is your editor configured? we dont need to run the ci to realize we have typescript errors

ggazzo avatar Apr 04 '25 21:04 ggazzo

@ahmed-n-abdeltwab how is your editor configured? we dont need to run the ci to realize we have typescript errors

I'm using Vim as my editor. With default configuration

ahmed-n-abdeltwab avatar Apr 04 '25 21:04 ahmed-n-abdeltwab

@ahmed-n-abdeltwab how is your editor configured? we dont need to run the ci to realize we have typescript errors

Is there something I need to change in my setup to see the error earlier?

ahmed-n-abdeltwab avatar Apr 05 '25 03:04 ahmed-n-abdeltwab

[/statistics] should return an error when the user does not have the necessary permission: Error: expected 400 "Bad Request", got 502 "Bad Gateway" at /home/runner/work/Rocket.Chat/Rocket.Chat/apps/meteor/tests/end-to-end/api/statistics.ts:19:7

I think this error happens because the 400 response validation has not been created yet

ahmed-n-abdeltwab avatar Apr 05 '25 15:04 ahmed-n-abdeltwab

@kody start-review

ahmed-n-abdeltwab avatar Apr 05 '25 18:04 ahmed-n-abdeltwab

[/statistics] should return an error when the user does not have the necessary permission: Error: expected 400 "Bad Request", got 502 "Bad Gateway" at /home/runner/work/Rocket.Chat/Rocket.Chat/apps/meteor/tests/end-to-end/api/statistics.ts:19:7

I think this error happens because the 400 response validation has not been created yet

Hi @ggazzo, you asked for changes! Should I delete my last commit?

ahmed-n-abdeltwab avatar Apr 06 '25 03:04 ahmed-n-abdeltwab

@ggazzo just pinging you in case you haven't saw my comment

ahmed-n-abdeltwab avatar Apr 07 '25 14:04 ahmed-n-abdeltwab

Hi @ahmed-n-abdeltwab , we highly recommend you switch to VSCode in order to get instant feedback regarding type errors and eslint errors.

If you still wish to use the command-line:

yarn turbo run typecheck
yarn lint

Please ensure both of these commands are successful before sending more commits.

Got it! I'll check the code and get back to you.

ahmed-n-abdeltwab avatar Apr 19 '25 14:04 ahmed-n-abdeltwab

The error happens because the status endpoint gives an error when the user doesn’t have the right permissions.
hmm, Shouldn't the APIClass check the user permissions before and handle the error by itself?

I will try to understand what is going on. First, I will look at api.ts code Link to code

ahmed-n-abdeltwab avatar Apr 20 '25 14:04 ahmed-n-abdeltwab

@ahmed-n-abdeltwab looks like you sent a commit that wasn't tied to your github account:

https://github.com/RocketChat/Rocket.Chat/pull/35692/commits/00b35147320ce94586625ba674a6717e9ca25a86

Please follow the advice in the CLA bot comment:

https://github.com/RocketChat/Rocket.Chat/pull/35692#issuecomment-2774590440

Regarding the permission error you mentioned: whatever the behavior was before, must remain the same. We're only looking to refactor without changing behavior. Maybe @ggazzo coud clarify.

cardoso avatar Apr 20 '25 17:04 cardoso

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 61.83%. Comparing base (99330c5) to head (e339c53). Report is 42 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop   #35692      +/-   ##
===========================================
- Coverage    64.76%   61.83%   -2.93%     
===========================================
  Files         3156     2833     -323     
  Lines       104857    99509    -5348     
  Branches     19959    18684    -1275     
===========================================
- Hits         67908    61531    -6377     
- Misses       34267    35804    +1537     
+ Partials      2682     2174     -508     
Flag Coverage Ξ”
e2e 46.80% <ΓΈ> (-10.81%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Apr 20 '25 17:04 codecov[bot]

Should I revert to this specific commit in Git to attempt a fix, or is it acceptable to leave the current changes as they are for now?

To clarify what I mean by "permission," I'm not referring to user permission. In this context, the user is not me. Instead, I'm talking about whether the user entity is authorized to return the result. In the old code, within the addRoute function, the initial logic block checked for permissions and whether authentication was required before proceeding with its main logic, such as querying and performing other tasks. My question is whether the API currently performs the same logic to check permissions and handle them accordingly.

ahmed-n-abdeltwab avatar Apr 21 '25 05:04 ahmed-n-abdeltwab

During my research to identify the problem, I came across the file federation.ts. However, it lacks the logic to check for permissions, which made me question whether what I did is the same as in federation.ts, or if I am missing something.

ahmed-n-abdeltwab avatar Apr 21 '25 06:04 ahmed-n-abdeltwab