Skip to content

fix(spec): handle requestBody with no content without crashing#10837

Open
yogeshwaran-c wants to merge 2 commits intoswagger-api:masterfrom
yogeshwaran-c:fix/request-body-no-content-crash
Open

fix(spec): handle requestBody with no content without crashing#10837
yogeshwaran-c wants to merge 2 commits intoswagger-api:masterfrom
yogeshwaran-c:fix/request-body-no-content-crash

Conversation

@yogeshwaran-c
Copy link
Copy Markdown

Description

When a requestBody object has no content property (only description and required), the requiredParameters selector in src/core/plugins/spec/selectors.js crashes with:

TypeError: Cannot read properties of undefined (reading 'entrySeq')

This happens because requestBody.getIn(["content"]) returns undefined when there is no content key, and .entrySeq() is then called on undefined.

The fix adds a null check before iterating over content entries, returning the partial result early when content is absent.

Motivation and Context

Fixes #10767

While the OpenAPI spec expects requestBody to have a content field, real-world specs sometimes omit it (e.g., when using code generators or incomplete specs). Swagger UI should handle this gracefully instead of crashing.

How Has This Been Tested?

  • Verified the fix prevents the TypeError when requestBody has no content
  • The early return preserves any required field that was already extracted from the requestBody
  • Existing behavior is unchanged when content is present

Screenshots (if appropriate):

N/A

Checklist

My PR contains...

  • No code changes (src/ is unmodified: changes to documentation, CI, metadata, etc.)
  • Dependency changes (any modification to dependencies in package.json)
  • Bug fixes (non-breaking change which fixes an issue)
  • Improvements (misc. changes to existing features)
  • Features (non-breaking change which adds functionality)

My changes...

  • are breaking changes to a public API (config options, System API, major UI change, etc).
  • are breaking changes to a private API (Redux, component props, utility functions, etc.).
  • are breaking changes to a developer API (npm script behavior changes, new dev system dependencies, etc).
  • are not breaking changes.

Documentation

  • My changes do not require a change to the project documentation.
  • My changes require a change to the project documentation.
  • If yes to above: I have updated the documentation accordingly.

Automated tests

  • My changes can not or do not need to be tested.
  • My changes can and should be tested by unit and/or integration tests.
  • If yes to above: I have added tests to cover my changes.
  • If yes to above: I have taken care to cover edge cases in my tests.
  • All new and existing tests passed.

When a requestBody object has no content property (only description
and required), the requiredParameters selector would crash with
TypeError: Cannot read properties of undefined (reading 'entrySeq')
because requestBody.getIn(["content"]) returns undefined.

This adds a null check before iterating over content entries,
returning the partial result early when content is absent.

Fixes swagger-api#10767
@lukaszzazulak
Copy link
Copy Markdown
Contributor

lukaszzazulak commented Apr 16, 2026

@yogeshwaran-c Although the content field is required (https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#request-body-object), the fix is valid since the app should not crash despite errors in the document; please just include a unit test for this case to prevent future regressions.

Cover the case where requestBody has no content property to prevent
regression of the no-content crash fix.
@yogeshwaran-c
Copy link
Copy Markdown
Author

@lukaszzazulak Thanks for the review and for confirming the fix is valid. I've added a unit test for getOAS3RequiredRequestBodyContentType in test/unit/core/plugins/spec/selectors.js that covers the no-content case (along with a couple of related cases for context) — pushed in 9aa5940.

@lukaszzazulak lukaszzazulak self-requested a review April 17, 2026 09:11
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.

A requestBody with no content causes console error "TypeError: Cannot read properties of undefined (reading 'entrySeq')

2 participants