refactor: Migrate users.create endpoint to OpenAPI with AJV validation
Proposed changes
Migrates the users.create endpoint from the legacy addRoute() pattern to the new OpenAPI-compatible chained route pattern with inline AJV validation schemas.
Resolves: RocketChat/Rocket.Chat-Open-API#150
Changes
apps/meteor/app/api/server/v1/users.ts
- Replaced legacy
API.v1.addRoute('users.create', ...)with the new chained.post('users.create', ...)pattern on the existingusersEndpointsvariable - Added local
UserCreateParamsPOSTtype,userCreateParamsPostSchema, andisUserCreateParamsPOSTvalidator (moved from rest-typings, names preserved exactly) - Added
body: isUserCreateParamsPOSTreplacing deprecatedvalidateParams - Added response validation schemas:
200with$ref: '#/components/schemas/IUser'via Typia400withvalidateBadRequestErrorResponse401withvalidateUnauthorizedErrorResponse
- Used
async function action()named handler (not arrow function) - Business logic, permissions, and side effects are completely unchanged
- Types are automatically picked up via existing
ExtractRoutesFromAPI<typeof usersEndpoints>+declare moduleaugmentation at the bottom of the file
packages/rest-typings/src/v1/users.ts
- Removed
/v1/users.createentry from theUsersEndpointsinterface (now auto-generated viaExtractRoutesFromAPI) - Removed
import type { UserCreateParamsPOST }(no longer needed in this file)
packages/rest-typings/src/v1/users/UserCreateParamsPOST.ts
- Removed the AJV schema (
userCreateParamsPostSchema), compiled validator (isUserCreateParamsPOST), andajvimport — these are now defined locally in the endpoint file - Kept the
UserCreateParamsPOSTtype export because it is imported by client code (AdminUserForm.tsx) for form typing
.changeset/migrate-users-create-openapi.md
- Added changeset with
minorbump for@rocket.chat/meteorand@rocket.chat/rest-typings
Test results
Note on the 1 failing test: The should create a new user with default roles test fails due to a pre-existing Typia schema mismatch — IUser.inactiveReason is typed as 'deactivated' | 'pending_approval' | 'idle_too_long' (optional), but the database returns null for newly created users. The Typia-generated $ref schema for IUser rejects null because the TypeScript type doesn't include it. This is not caused by this migration — any endpoint using $ref: '#/components/schemas/IUser' in response validation would hit this same issue. The fix belongs in core-typings (updating the inactiveReason type to include null) as a separate PR.
Summary by CodeRabbit
-
Documentation
- OpenAPI support added for the users.create endpoint with formal request/response schemas and updated docs.
-
New Features
- Stronger request/response validation for user creation and more consistent success/error responses.
-
Bug Fixes
- Post-creation output cleaned up (null inactiveReason removed) and created user returned with a stable projection.