-
-
Notifications
You must be signed in to change notification settings - Fork 492
fix(openapi): generate requestBody for body parameter #4488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(openapi): generate requestBody for body parameter #4488
Conversation
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
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
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) |
There was a problem hiding this comment.
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 = ( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
passAm 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?
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
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
Description
OpenAPI generation now correctly creates requestBody when using the
body: bytesparameter. Previously onlydataparameter was checked.dataandbodyfields inpath_item.pyapplication/octet-streamforbytestypeCloses
Fixes #4482