Skip to content

fix: type rawBody as optional to match runtime behavior - #178

Open
afonsojanu wants to merge 1 commit into
koajs:masterfrom
afonsojanu:fix/raw-body-optional-type
Open

fix: type rawBody as optional to match runtime behavior#178
afonsojanu wants to merge 1 commit into
koajs:masterfrom
afonsojanu:fix/raw-body-optional-type

Conversation

@afonsojanu

@afonsojanu afonsojanu commented Sep 6, 2026

Copy link
Copy Markdown

Closes #174

rawBody gets typed as a plain string on both koa.Request and http.IncomingMessage, but at runtime it's only set once a body actually gets parsed. If the content type isn't one of the supported ones, or the parser is disabled, or there's no body at all, it stays undefined. The test suite already checks for exactly that, there's a couple of expect(ctx.request.rawBody).toEqual(undefined) assertions in middleware.test.ts, so the runtime behavior clearly isn't in question here, just the type not reflecting it.

Widened both declarations to string | undefined. Small change, nothing else touched.

Ran the full test suite and xo lint locally, both clean, plus tsc --noEmit to confirm the type change compiles fine on its own.

Summary by Sourcery

Bug Fixes:

  • Reflect the runtime behavior of rawBody by typing it as optional on Koa requests and incoming HTTP messages.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected request type declarations to reflect that raw request body data may be unavailable on some requests.

rawBody is only assigned once a body actually gets parsed. For
unsupported content types, disabled parsers, or when a request has no
body, it stays undefined - the existing tests already check for this
case with toEqual(undefined). The type declarations on koa.Request and
http.IncomingMessage still said plain string though, so anything using
strict type checking has no way to know it might not be there.
@sourcery-ai

sourcery-ai Bot commented Sep 6, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates both rawBody declarations to string | undefined, accurately reflecting that the property is only populated when a supported body is parsed while preserving the existing runtime behavior.

File-Level Changes

Change Details Files
Align the TypeScript declarations for rawBody with its runtime availability.
  • Change Koa Request.rawBody from required string to optional string.
  • Change http.IncomingMessage.rawBody from required string to optional string.
src/body-parser.ts

Assessment against linked issues

Issue Objective Addressed Explanation
#174 Update the TypeScript declarations for koa.Request.rawBody and http.IncomingMessage.rawBody to reflect that the property may be undefined when no supported body is parsed.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Sep 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: 5b5ab20d-63b6-4616-acd6-f264b5e59d73

📥 Commits

Reviewing files that changed from the base of the PR and between b9b77aa and 7e5c132.

📒 Files selected for processing (1)
  • src/body-parser.ts

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

The request type declarations now mark rawBody as optional on both Koa.Request and http.IncomingMessage. This matches runtime behavior when the request does not contain a supported body type.

Merge Risk: ⚪ Minimal · up to 7e5c1

This updates request typings so rawBody may be absent when no body is parsed, matching runtime behavior. No merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary change: making rawBody optional to match runtime behavior.
Linked Issues check ✅ Passed The PR satisfies issue #174 by changing rawBody declarations on both koa.Request and http.IncomingMessage to optional, matching cases where the runtime value is undefined.
Out of Scope Changes check ✅ Passed The changes are limited to the two rawBody type declarations required by issue #174. No unrelated changes are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…

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.

❤️ Share

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/body-parser.ts" line_range="13" />
<code_context>
   // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
   interface Request {
     body?: unknown;
-    rawBody: string;
+    rawBody?: string;
   }
</code_context>
<issue_to_address>
**issue (bug_risk):** With `exactOptionalPropertyTypes` enabled, `rawBody?: string` allows the property to be omitted but does not allow callers to explicitly assign `undefined`; this contradicts the intended `string | undefined` contract and the runtime behavior.

**Triggers:** When a TypeScript consumer uses `exactOptionalPropertyTypes` and clears or forwards `rawBody` by assigning `undefined`.

**Suggested fix:** Declare the properties as `rawBody: string | undefined` if explicit `undefined` assignments are part of the supported contract.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: src/body-parser.ts:13


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/body-parser.ts
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Request {
body?: unknown;
rawBody: string;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): With exactOptionalPropertyTypes enabled, rawBody?: string allows the property to be omitted but does not allow callers to explicitly assign undefined; this contradicts the intended string | undefined contract and the runtime behavior.

Triggers: When a TypeScript consumer uses exactOptionalPropertyTypes and clears or forwards rawBody by assigning undefined.

Suggested fix: Declare the properties as rawBody: string | undefined if explicit undefined assignments are part of the supported contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rawBody can be undefined but it's typed as a string

1 participant