feat: Add OpenAPI Support to Statistics API
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
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
π¦ 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
@kody start-review
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-reviewcommand 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 | β |
β
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?
@kody start-review
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-reviewcommand 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 | β |
β
@kody start-review
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-reviewcommand 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 | β |
β
@kody start-review
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-reviewcommand 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 | β |
β
@kody start-review
Hi @ggazzo,
I've refactored the statistics endpoints to improve:
- Code organization (separated handlers from route configs)
- Schema validation (additionalProperties: false, added descriptions)
- Documentation (OpenAPI compatibility)
Could you please review these changes and share your feedback.
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!
@kody start-review
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 how is your editor configured? we dont need to run the ci to realize we have typescript errors
@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 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?
[/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
@kody start-review
[/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?
@ggazzo just pinging you in case you haven't saw my comment
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 typecheckyarn lintPlease ensure both of these commands are successful before sending more commits.
Got it! I'll check the code and get back to you.
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 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.
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
@@ 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.
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.
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.