Skip to content

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

Open
kouzitech wants to merge 1 commit into
asyncapi:masterfrom
kouzitech:fix/request-body-validation
Open

fix: skip request body validation gracefully instead of throwing error#2204
kouzitech wants to merge 1 commit into
asyncapi:masterfrom
kouzitech:fix/request-body-validation

Conversation

@kouzitech
Copy link
Copy Markdown

Fix for Issue #1987: Request body validation is skipped for some paths or HTTP methods

Summary

This PR fixes the request body validation bug where validation was incorrectly reported as unsupported for certain paths or HTTP methods.

The Bug

Two specific issues occurred:

  1. Unsafe property access to requestBody.content["application/json"].schema caused TypeError when the content type was not application/json.

  2. When compileAjv() returned undefined, the middleware incorrectly threw a Request body validation is not supported error.

The Solution

  1. Added findContentSchema() helper that safely finds a schema from any content type.

  2. Changed validation middleware to skip body validation (not throw error) when no schema is defined.

Benefits

  • Request body validation now works for all paths/methods
  • No false unsupported errors for endpoints without request bodies
  • Invalid request bodies are properly validated when schema exists
  • Non-JSON content types with schemas are properly supported
  • Added comprehensive unit tests (17 test cases)

Fixes #1987

This PR is part of the MICROGRANT Program 2026-05 #2125

This commit fixes request body validation being skipped or reported as
unsupported for certain paths or HTTP methods (Issue asyncapi#1987).

Two bugs were fixed:

1. Unsafe access to requestBody.content['application/json'].schema
   caused TypeError when application/json was not a content type
   (e.g., multipart/form-data, text/plain, or missing entirely).

2. When compileAjv() returned undefined (because the method has no
   requestBody, like GET/DELETE, or the requestBody has no JSON schema),
   the middleware incorrectly threw 'Request body validation is not
   supported' error. This is wrong - methods without request bodies
   simply don't need body validation, and endpoints with non-JSON
   content types should silently skip rather than error.

Changes:
- Added findContentSchema() helper function that safely finds a schema
  from any content type, prioritizing application/json but falling back
  to any available schema
- Changed validation middleware to skip body validation (not throw
  error) when no schema is defined
- Added comprehensive tests for all edge cases

This ensures:
- Request body validation works for all paths/methods
- No false 'unsupported' errors for endpoints without request bodies
- Invalid request bodies are properly validated when schema exists
- Non-JSON content types with schemas are properly supported

Fixes asyncapi#1987
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 17, 2026

🦋 Changeset detected

Latest commit: 828a680

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

@sonarqubecloud
Copy link
Copy Markdown

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