Skip to content

Commit

Permalink
Fix #466 (#467)
Browse files Browse the repository at this point in the history
- Fix #466
- Prepare for release 2.0.5 to resolve the regression
* Fix test for older versions of Python
  • Loading branch information
RobertoPrevato authored Jan 12, 2024
1 parent 1753097 commit 44d04e4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.5] - 2024-01-12 :pie:

- Fixes [#466](https://github.com/Neoteroi/BlackSheep/issues/466), regression
introduced in 2.0.4 when using sub-routers, reported by @ruancan.

## [2.0.4] - 2023-12-31 :fireworks:

- Adds a `is_disconnected()` method to the `Request` class, similar to the one
Expand Down
2 changes: 1 addition & 1 deletion blacksheep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
used types to reduce the verbosity of the imports statements.
"""
__author__ = "Roberto Prevato <[email protected]>"
__version__ = "2.0.4"
__version__ = "2.0.5"

from .contents import Content as Content
from .contents import FormContent as FormContent
Expand Down
10 changes: 10 additions & 0 deletions blacksheep/server/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,16 @@ def __iter__(self):
for router in self._sub_routers:
yield from router

def iter_with_methods(self):
"""
Iters through the routes defined in this Router, yielding each route
and its HTTP method.
"""
yield from super().__iter__() # type: ignore

for router in self._sub_routers:
yield from router.iter_with_methods()

def get_match(self, request: Request) -> Optional[RouteMatch]:
for router in self._sub_routers:
match = router.get_match(request)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -4143,3 +4143,39 @@ def test_mounting_apps_using_the_same_router_raises_error():

with pytest.raises(SharedRouterError):
Application(router=single_router)


@pytest.mark.asyncio
async def test_application_sub_router_normalization():
router = Router()
app = FakeApplication(router=Router(sub_routers=[router]))

# https://github.com/Neoteroi/BlackSheep/issues/466
@dataclass
class Person:
id: Optional[int] = None
name: str = ""

@router.post("/")
async def hello(request: Request, p: Person):
return f"{request.client_ip}:Hello, {p.name}!"

content = b'{"id": 1, "name": "Charlie Brown"}'

await app.start()
await app(
get_example_scope(
"POST",
"/",
[
(b"content-length", str(len(content)).encode()),
(b"content-type", b"application/json"),
],
),
MockReceive([content]),
MockSend(),
)

response = app.response
assert response is not None
assert response.status == 200

0 comments on commit 44d04e4

Please sign in to comment.