Mock server - #20
Mock server#20
Conversation
|
Caution Review failedThe pull request is closed. WalkthroughAdds a mock-server feature that generates runtime responses from OpenAPI specs, Spectral linting for success responses, ApiLinting integration for endpoint operations, new exceptions, tests, package/jest changes for faker/json-schema-faker, and seed/schema updates for x-faker-driven mocking. Changes
Sequence Diagram(s)sequenceDiagram
participant C as Client
participant Ctrl as MockServerController
participant Svc as MockServerService
participant PS as ProjectsService
participant AC as AccessControlService
participant JSF as JSONSchemaFaker+Faker
C->>Ctrl: request /mock/… (method, headers: x-mock-project-id, x-mock-status-code, x-mock-locale)
Ctrl->>Svc: generateMockResponse(projectId, method, path, user, status, locale)
Svc->>AC: verify access(user, projectId)
AC-->>Svc: allowed/denied
Svc->>PS: get dereferenced OpenAPI spec(projectId)
PS-->>Svc: spec
Svc->>Svc: match operation(method, path)
Svc->>Svc: select response (requested or fallback)
Svc->>Svc: extract application/json schema (or $ref)
Svc->>JSF: generate mock body(schema, locale)
JSF-->>Svc: mock body
Svc-->>Ctrl: { status, body }
Ctrl->>C: HTTP response (status + body) / or throws mapped exception
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (4)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 6
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
bun.lockis excluded by!**/*.lock
📒 Files selected for processing (17)
.spectral.yaml(1 hunks)package.json(4 hunks)prisma/seed.ts(10 hunks)src/app.module.ts(2 hunks)src/auth/auth.service.spec.ts(1 hunks)src/mock-server/exceptions/mock-endpoint-not-found.exception.ts(1 hunks)src/mock-server/exceptions/mock-generation-conflict.exception.ts(1 hunks)src/mock-server/exceptions/mock-response-not-found.exception.ts(1 hunks)src/mock-server/mock-server.controller.ts(1 hunks)src/mock-server/mock-server.module.ts(1 hunks)src/mock-server/mock-server.service.spec.ts(1 hunks)src/mock-server/mock-server.service.ts(1 hunks)src/projects/endpoints/endpoints.module.ts(1 hunks)src/projects/endpoints/endpoints.service.spec.ts(3 hunks)src/projects/endpoints/endpoints.service.ts(6 hunks)src/projects/projects.module.ts(1 hunks)test/jest-e2e.json(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (8)
src/mock-server/mock-server.module.ts (2)
src/app.module.ts (1)
Module(37-117)src/projects/projects.module.ts (1)
Module(18-40)
src/mock-server/exceptions/mock-response-not-found.exception.ts (1)
src/common/exceptions/base-app.exception.ts (1)
BaseAppException(3-15)
src/mock-server/mock-server.controller.ts (3)
src/types/fastify.d.ts (1)
FastifyRequest(16-24)src/auth/decorators/current-user.decorator.ts (1)
CurrentUser(7-10)src/auth/dto/user.dto.ts (1)
UserDto(6-40)
src/mock-server/exceptions/mock-generation-conflict.exception.ts (1)
src/common/exceptions/base-app.exception.ts (1)
BaseAppException(3-15)
src/projects/endpoints/endpoints.service.ts (2)
src/common/constants/http.constants.ts (1)
VALID_HTTP_METHODS(6-15)src/common/exceptions/spec-linting.exception.ts (1)
SpecLintingException(12-28)
src/mock-server/mock-server.service.ts (6)
src/auth/dto/user.dto.ts (1)
UserDto(6-40)src/common/exceptions/project-not-found.exception.ts (1)
ProjectNotFoundException(4-12)src/mock-server/exceptions/mock-endpoint-not-found.exception.ts (1)
MockEndpointNotFoundException(4-12)src/mock-server/exceptions/mock-response-not-found.exception.ts (1)
MockResponseNotFoundException(4-12)src/mock-server/exceptions/mock-generation-conflict.exception.ts (1)
MockGenerationConflictException(4-12)src/common/constants/http.constants.ts (1)
VALID_HTTP_METHODS(6-15)
src/mock-server/exceptions/mock-endpoint-not-found.exception.ts (1)
src/common/exceptions/base-app.exception.ts (1)
BaseAppException(3-15)
src/projects/endpoints/endpoints.module.ts (2)
src/app.module.ts (1)
Module(37-117)src/projects/projects.module.ts (1)
Module(18-40)
🔇 Additional comments (1)
src/auth/auth.service.spec.ts (1)
3-3: Good cleanup.Removing the inline comment from the import statement improves code cleanliness without any functional impact.
| @ApiParam({ | ||
| name: 'projectId', | ||
| description: 'The ID of the project to generate a mock response for.', | ||
| }) | ||
| @ApiParam({ | ||
| name: '*', | ||
| description: 'The API path to mock (e.g., "users/123/profile").', | ||
| }) |
There was a problem hiding this comment.
Fix inaccurate Swagger path parameter definition
This route never declares a projectId path segment—the wildcard * captures the entire downstream path, while the controller reads the project ID from the x-mock-project-id header. Documenting a projectId path parameter causes generated clients (and humans) to send requests like /mock/{projectId}/users, which makes mockPath resolve to '123/users' and the service look up /123/users, inevitably triggering MockEndpointNotFoundException. Either remove this param from the Swagger docs or actually add a :projectId segment and adjust the handler accordingly; as written, it’s a user-facing correctness bug.
Apply this diff if the project ID is meant to stay in the header:
- @ApiParam({
- name: 'projectId',
- description: 'The ID of the project to generate a mock response for.',
- })🤖 Prompt for AI Agents
In src/mock-server/mock-server.controller.ts around lines 24 to 31, remove the
incorrect @ApiParam describing a path-level projectId (it doesn't exist in the
route) and instead document the X header used to convey the project ID: delete
the @ApiParam block for name 'projectId' and add an @ApiHeader (name:
'x-mock-project-id', description: 'Project ID used to select mock data',
required: true) so Swagger clients are informed the project ID is passed via
header while keeping the wildcard path param for the downstream route.
Summary by CodeRabbit
New Features
Chores