Skip to content

Conversation

@burnash
Copy link

@burnash burnash commented Nov 22, 2025

Description

OpenAPI generation now correctly creates requestBody when using the body: bytes parameter. Previously only data parameter was checked.

  • Check both data and body fields in path_item.py
  • Default to application/octet-stream for bytes type
  • Add tests for all body parameter syntaxes

Closes

Fixes #4482

OpenAPI generation now correctly creates requestBody when using the `body: bytes` parameter. Previously only `data` parameter was checked.

- Check both data and body fields in path_item.py
- Default to application/octet-stream for bytes type
- Add tests for all body parameter syntaxes

Fixes litestar-org#4482
@burnash burnash requested review from a team as code owners November 22, 2025 21:57
@github-actions github-actions bot added area/openapi This PR involves changes to the OpenAPI schema area/private-api This PR involves changes to the privatized API size: small type/bug pr/external Triage Required 🏥 This requires triage labels Nov 22, 2025
@codecov
Copy link

codecov bot commented Nov 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.83%. Comparing base (8b74278) to head (2b21104).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4488   +/-   ##
=======================================
  Coverage   97.83%   97.83%           
=======================================
  Files         296      296           
  Lines       15295    15300    +5     
  Branches     1713     1714    +1     
=======================================
+ Hits        14964    14969    +5     
  Misses        189      189           
  Partials      142      142           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/4488

request_body_field = body_field

raises_validation_error = bool(data_field or self._path_item.parameters or parameters)
raises_validation_error = bool(request_body_field or self._path_item.parameters or parameters)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should set raises_validation_error=True in this case, as the body parameter is only allowed to be bytes and will always receive the raw request body

A RequestBody instance.
"""
media_type: RequestEncodingType | str = RequestEncodingType.JSON
media_type: RequestEncodingType | str = (
Copy link
Member

Choose a reason for hiding this comment

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

This is incorrect; body: bytes is just the raw request body, it does not signify a binary payload. That would be data: bytes. So no changes should be made here

Copy link
Author

Choose a reason for hiding this comment

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

I'm new to the framework and not fully confident about the correct place to enforce the expected behavior.
Could you outline where in the existing code (e.g. specific modules or functions) the fix should be implemented, so I can update the PR accordingly?

Copy link
Member

@provinzkraut provinzkraut Nov 23, 2025

Choose a reason for hiding this comment

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

I think there might be a slight misunderstanding!

There's no need to change anything here, you just need to revert the changes you've made in this section.

Currently, (with your changes implemented - this line here) the proper way to achieve what you want would be to do something like:

@post("/")
async def handler(body: bytes = Body(content_encoding="application/octet-stream")) -> None:
    pass

Am I correct in assuming that what you're asking is to be able to omit the Body param, and default to application/octet-stream for a bare body: bytes?

}
}
}

Copy link
Member

Choose a reason for hiding this comment

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

I think these tests would be more readable if you used pytest.mark.parametrize

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

Labels

area/openapi This PR involves changes to the OpenAPI schema area/private-api This PR involves changes to the privatized API pr/external size: small Triage Required 🏥 This requires triage type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: OpenAPI generation with binary request bodies

2 participants