-
-
Notifications
You must be signed in to change notification settings - Fork 503
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,9 @@ def create_request_body( | |
| Returns: | ||
| A RequestBody instance. | ||
| """ | ||
| media_type: RequestEncodingType | str = RequestEncodingType.JSON | ||
| media_type: RequestEncodingType | str = ( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect;
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| "application/octet-stream" if data_field.is_subclass_of(bytes) else RequestEncodingType.JSON | ||
| ) | ||
| schema_creator = SchemaCreator.from_openapi_context(context, prefer_alias=True) | ||
| if isinstance(data_field.kwarg_definition, BodyKwarg) and data_field.kwarg_definition.media_type: | ||
| media_type = data_field.kwarg_definition.media_type | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -217,3 +217,69 @@ async def handler( | |
| } | ||
| } | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these tests would be more readable if you used |
||
|
|
||
| def test_body_parameter_binary_request() -> None: | ||
| @post("/upload/") | ||
| async def handle_binary_upload(body: bytes) -> None: | ||
| return None | ||
|
|
||
| app = Litestar([handle_binary_upload]) | ||
| schema = app.openapi_schema.to_schema() | ||
|
|
||
| assert "requestBody" in schema["paths"]["/upload"]["post"] | ||
| assert schema["paths"]["/upload"]["post"]["requestBody"] == { | ||
| "required": True, | ||
| "content": {"application/octet-stream": {"schema": {"type": "string"}}}, | ||
| } | ||
|
|
||
|
|
||
| def test_body_parameter_with_body_annotation() -> None: | ||
| @post("/upload/") | ||
| async def handle_binary_upload( | ||
| body: Annotated[bytes, Body(media_type="application/octet-stream", title="Binary Data")], | ||
| ) -> None: | ||
| return None | ||
|
|
||
| app = Litestar([handle_binary_upload]) | ||
| schema = app.openapi_schema.to_schema() | ||
|
|
||
| assert "requestBody" in schema["paths"]["/upload"]["post"] | ||
| assert schema["paths"]["/upload"]["post"]["requestBody"] == { | ||
| "required": True, | ||
| "content": {"application/octet-stream": {"schema": {"type": "string", "title": "Binary Data"}}}, | ||
| } | ||
|
|
||
|
|
||
| def test_body_parameter_with_default_value() -> None: | ||
| @post("/upload/") | ||
| async def handle_binary_upload( | ||
| body: bytes = Body(media_type="application/octet-stream", title="Binary Data"), | ||
| ) -> None: | ||
| return None | ||
|
|
||
| app = Litestar([handle_binary_upload]) | ||
| schema = app.openapi_schema.to_schema() | ||
|
|
||
| assert "requestBody" in schema["paths"]["/upload"]["post"] | ||
| assert schema["paths"]["/upload"]["post"]["requestBody"] == { | ||
| "required": True, | ||
| "content": {"application/octet-stream": {"schema": {"type": "string", "title": "Binary Data"}}}, | ||
| } | ||
|
|
||
|
|
||
| def test_body_parameter_with_custom_media_type() -> None: | ||
| @post("/upload/") | ||
| async def handle_binary_upload( | ||
| body: Annotated[bytes, Body(media_type="application/x-custom-binary")], | ||
| ) -> None: | ||
| return None | ||
|
|
||
| app = Litestar([handle_binary_upload]) | ||
| schema = app.openapi_schema.to_schema() | ||
|
|
||
| assert "requestBody" in schema["paths"]["/upload"]["post"] | ||
| assert schema["paths"]["/upload"]["post"]["requestBody"] == { | ||
| "required": True, | ||
| "content": {"application/x-custom-binary": {"schema": {"type": "string"}}}, | ||
| } | ||
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=Truein this case, as thebodyparameter is only allowed to bebytesand will always receive the raw request body