Merged
Conversation
- Add UnsupportedMediaType (HTTP 415) exception to blacksheep/exceptions.py - Add FromBody[T] BoundValue type: accepts JSON and form bodies by default - Add MultiFormatBodyBinder: holds a list of inner BodyBinders, delegates get_value to the first whose matches_content_type returns True; raises UnsupportedMediaType (415) when required and no inner binder matches - Add FromBodyBinder: the concrete binder backing FromBody[T] - Normalization: detect union-of-BodyBinder annotations such as FromJSON[T] | FromForm[T] and automatically build a MultiFormatBodyBinder; optional unions (FromJSON[T] | FromForm[T] | None) set required=False - Export FromBody from the top-level blacksheep package - OpenAPI: existing _get_body_binder_content_type split-on-semicolon logic handles MultiFormatBodyBinder transparently via its content_type property Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Verify that FromJSON[T] | FromForm[T] and FromBody[T] both emit all accepted content types in the requestBody, and that the optional variant emits required: false. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Add FromXML[T] BoundValue type; exported from top-level blacksheep - Add XMLBinder: parses application/xml and text/xml bodies using defusedxml, protecting against XXE injection, entity expansion (billion laughs), and DTD-based attacks - defusedxml security exceptions (DTDForbidden, EntitiesForbidden, …) propagate unmodified; genuine XML parse errors become InvalidRequestBody - Add _element_to_dict() helper: strips namespaces, collects repeated sibling tags as lists, merges element attributes - ClassConverter already coerces string values to typed fields (int, float, UUID, datetime, …), so XML parsing works end-to-end with the same converter chain as JSON and form binders - Add defusedxml>=0.7.1 to [xml] and [full] optional extras in pyproject.toml - FromXML participates in the MultiFormatBodyBinder union syntax: FromJSON[T] | FromXML[T] works out of the box - Add XMLBinder tests: parsing, content-type matching, malformed XML, XXE and billion-laughs rejection, _element_to_dict edge cases - Add OpenAPI tests: FromXML[T] emits application/xml + text/xml; FromJSON[T] | FromXML[T] emits all three content types Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…L support Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
multiple request body formats on a single endpoint.
New
MultiFormatBodyBinder: holds an ordered list of innerBodyBinders anddispatches
get_valueto the first whosematches_content_typereturnsTrue.Returns HTTP 415 Unsupported Media Type when the binder is required and no
inner binder matches the request's
Content-Type.Union annotation syntax — the normalization layer detects a union of
body-binder
BoundValuetypes and automatically builds aMultiFormatBodyBinder:New
FromBody[T]convenience type: equivalent toFromJSON[T] | FromForm[T], useful when you want to accept both structuredformats without being explicit:
New
FromXML[T]andXMLBinder: parseapplication/xml/text/xmlrequest bodies using
defusedxml,protecting against XXE injection, entity expansion (billion laughs),
and DTD-based attacks. Security exceptions propagate unmodified so the
application can distinguish attack attempts from ordinary malformed input.
Install the extra with
pip install blacksheep[xml].All new types compose freely:
OpenAPI Specification is generated correctly for all combinations: each
accepted content type appears as a separate entry under
requestBody.content,all referencing the same schema.
See https://github.com/Neoteroi/BlackSheep-Examples/tree/main/content-types