Skip to content

fix: skip request body validation gracefully instead of throwing error#2188

Open
bobbiejaxn wants to merge 2 commits intoasyncapi:masterfrom
bobbiejaxn:fix/issue-1987-request-body-validation
Open

fix: skip request body validation gracefully instead of throwing error#2188
bobbiejaxn wants to merge 2 commits intoasyncapi:masterfrom
bobbiejaxn:fix/issue-1987-request-body-validation

Conversation

@bobbiejaxn
Copy link
Copy Markdown

Fixes #1987

The Bug

When using request validation in the CLI, request body validation is skipped or reported as unsupported for certain paths or HTTP methods, even when a valid request body schema is defined. Two specific issues:

Bug 1: Unsafe property access crashes validation

requestBody.content["'application/json'].schema throws TypeError: Cannot read properties of undefined when the content type is not application/json (e.g., multipart/form-data, text/plain, or missing entirely).

Before: Direct property access crashes or returns undefined
After: Optional chaining requestBody.content?.["application/json"]?.schema safely handles missing content types

Bug 2: Incorrect error for endpoints without request bodies

When compileAjv() returns undefined (because the method has no requestBody, like GET/DELETE, or the requestBody has no JSON schema), the middleware threw:

Request body validation is not supported for "/path" path with "method" method.

This is wrong. Methods without request bodies simply don't need body validation — it's not an error condition. Endpoints with non-JSON content types should silently skip rather than error.

Before: Throws 422 error
After: Silently passes through to document validation

Testing

This mirrors the fix in #2128 but is more surgical — only touches validation.middleware.ts and does not include the unrelated URL parsing changes from #1940 (which is addressed separately in #2187).

Scope

This PR only fixes the request body validation bug (#1987). It does not touch the URL parsing or file extension detection (those are in #2187 for #1940).

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 6, 2026

🦋 Changeset detected

Latest commit: 8dee99f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@asyncapi/cli Patch

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

@github-project-automation github-project-automation Bot moved this to To Triage in CLI - Kanban May 6, 2026
bobbiejaxn added 2 commits May 7, 2026 22:10
Fixes asyncapi#1987

Two bugs fixed:

1. Unsafe access to requestBody.content['application/json'].schema
   crashes with TypeError when application/json is not a content type
   (e.g., multipart/form-data, text/plain). Fixed with optional
   chaining to safely check content type before accessing schema.

2. When compileAjv returns undefined (no requestBody or no JSON schema),
   the middleware incorrectly threw 'Request body validation is not
   supported' error. This is wrong - methods without request bodies
   (like GET, DELETE) simply don't need body validation, and endpoints
   with non-JSON content types should silently skip rather than error.
   Fixed to pass through instead of throwing.
@bobbiejaxn bobbiejaxn force-pushed the fix/issue-1987-request-body-validation branch from d6053e5 to 8dee99f Compare May 7, 2026 20:11
@bobbiejaxn
Copy link
Copy Markdown
Author

Rebased on latest master and added changeset as requested by the changeset bot. All checks pass, SonarCloud clean.

Ready for review: @Souvikns @Shurtu-gal @AayushSaini101

This is part of the MICROGRANT Program 2026-05.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 7, 2026

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

Labels

None yet

Projects

Status: To Triage

Development

Successfully merging this pull request may close these issues.

[BUG] Request body validation is skipped for some paths or HTTP methods

1 participant