Skip to content

Fixing ApiContractMetadataToRouteMapper to make it more generic#888

Merged
CarlosGamero merged 2 commits intomainfrom
bugfix/metadata-mapper-type
Mar 9, 2026
Merged

Fixing ApiContractMetadataToRouteMapper to make it more generic#888
CarlosGamero merged 2 commits intomainfrom
bugfix/metadata-mapper-type

Conversation

@CarlosGamero
Copy link
Copy Markdown
Collaborator

@CarlosGamero CarlosGamero commented Mar 9, 2026

Changes

Checklist

  • Apply one of following labels; major, minor, patch or skip-release
  • I've updated the documentation, or no changes were necessary
  • I've updated the tests, or no changes were necessary

AI Assistance Tracking

We're running a metric to understand where AI assists our engineering work. Please select exactly one of the options below:

Mark "Yes" if AI helped in any part of this work, for example: generating code, refactoring, debugging support,
explaining something, reviewing an idea, or suggesting an approach.

  • Yes, AI assisted with this PR
  • No, AI did not assist with this PR

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 9, 2026

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository: lokalise/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 06887933-30d8-4762-8d19-6a37f07616d6

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The change modifies the ApiContractMetadataToRouteMapper type alias in the fastify API contracts package. It updates the mapped type from picking fields off RouteType to picking fields off RouteOptions, and expands the set of picked keys from three lifecycle hooks ('config', 'bodyLimit', 'preParsing') to twelve keys, adding support for additional Fastify lifecycle hooks ('preSerialization', 'preHandler', 'preValidation', 'onRequest', 'onSend', 'onError', 'onResponse', 'onTimeout', 'onRequestAbort').

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description is incomplete; the 'Changes' section is entirely empty despite significant type alterations being made, leaving the motivation and details unexplained. Add a detailed explanation in the 'Changes' section describing why ApiContractMetadataToRouteMapper was modified and what benefit the expanded RouteOptions lifecycle hooks provide.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fixing ApiContractMetadataToRouteMapper to make it more generic' accurately summarizes the main change - updating the type alias to use RouteOptions instead of RouteType with an expanded key set.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bugfix/metadata-mapper-type

Comment @coderabbitai help to get the list of available commands and usage tips.

metadata: ApiContract['metadata'],
) => Pick<
RouteType,
RouteOptions,
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

By using the generic Fastify RouteOptions, the type remains compatible with Opinionated Machine and likely with other libraries as well. If we use the custom RouteType, it introduces type issues.

For this case, both types are equivalent.

Copy link
Copy Markdown
Collaborator

@kibertoad kibertoad Mar 9, 2026

Choose a reason for hiding this comment

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

we don't get in trouble with generics in this case?
IIRC original type comes with plenty of generics, although I might be wrong

Copy link
Copy Markdown
Collaborator Author

@CarlosGamero CarlosGamero Mar 9, 2026

Choose a reason for hiding this comment

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

Yeah, those generics are actually the problem:
Here is the original type:

export type RouteType<
  // biome-ignore lint/suspicious/noExplicitAny: It's ok
  ReplyType = any,
  // biome-ignore lint/suspicious/noExplicitAny: It's ok
  BodyType = any,
  // biome-ignore lint/suspicious/noExplicitAny: It's ok
  ParamsType = any,
  // biome-ignore lint/suspicious/noExplicitAny: It's ok
  QueryType = any,
  // biome-ignore lint/suspicious/noExplicitAny: It's ok
  HeadersType = any,
> = RouteOptions<
  RawServerDefault,
  RawRequestDefaultExpression,
  RawReplyDefaultExpression,
  FastifyContractRouteInterface<ReplyType, BodyType, ParamsType, QueryType, HeadersType>,
  // biome-ignore lint/suspicious/noExplicitAny: it's ok
  any,
  // biome-ignore lint/suspicious/noExplicitAny: it's ok
  any,
  // biome-ignore lint/suspicious/noExplicitAny: it's ok
  any,
  // biome-ignore lint/suspicious/noExplicitAny: it's ok
  any
>

The problem is with FastifyContractRouteInterface, Opinionated machine is expecting RouteGenericInterface there.

For metadata, it is not important as we are not allowing to modify the types defined there

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

gotcha!

@CarlosGamero CarlosGamero marked this pull request as ready for review March 9, 2026 17:00
@CarlosGamero CarlosGamero requested review from a team, dariacm and kibertoad as code owners March 9, 2026 17:00
Copy link
Copy Markdown

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/app/fastify-api-contracts/src/types.ts (1)

94-108: ⚠️ Potential issue | 🔴 Critical

onRequestAbort is not a valid per-route hook in Fastify's RouteOptions—remove it from the Pick list.

onRequestAbort can only be registered via fastify.addHook() at the instance or plugin level, not as a per-route hook. The official Fastify documentation lists the valid per-route lifecycle hooks as: onRequest, preParsing, preValidation, preHandler, preSerialization, onSend, onResponse, onTimeout, and onError. Line 107 should be removed from the type definition.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/app/fastify-api-contracts/src/types.ts` around lines 94 - 108, The
Pick type that narrows RouteOptions incorrectly includes the non-route hook
"onRequestAbort"; update the type in types.ts to remove 'onRequestAbort' from
the Pick list so it only includes valid per-route hooks (e.g.,
'onRequest','preParsing','preValidation','preHandler','preSerialization','onSend','onResponse','onTimeout','onError'),
ensuring the exported type that references RouteOptions no longer lists
'onRequestAbort'.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@packages/app/fastify-api-contracts/src/types.ts`:
- Around line 94-108: The Pick type that narrows RouteOptions incorrectly
includes the non-route hook "onRequestAbort"; update the type in types.ts to
remove 'onRequestAbort' from the Pick list so it only includes valid per-route
hooks (e.g.,
'onRequest','preParsing','preValidation','preHandler','preSerialization','onSend','onResponse','onTimeout','onError'),
ensuring the exported type that references RouteOptions no longer lists
'onRequestAbort'.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: lokalise/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 3dd0e1dc-397f-429a-ad35-b6917e0f5654

📥 Commits

Reviewing files that changed from the base of the PR and between 632ee80 and 15bd7b7.

📒 Files selected for processing (1)
  • packages/app/fastify-api-contracts/src/types.ts

@CarlosGamero CarlosGamero merged commit 8b80e5c into main Mar 9, 2026
6 checks passed
@CarlosGamero CarlosGamero deleted the bugfix/metadata-mapper-type branch March 9, 2026 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants