Anton Ryzhov 2 weeks ago
committed by GitHub
parent
commit
d6b3768e46
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 12
      fastapi/routing.py
  2. 98
      tests/test_dependency_contextmanager.py

12
fastapi/routing.py

@ -297,7 +297,12 @@ def get_request_handler(
embed_body_fields=embed_body_fields, embed_body_fields=embed_body_fields,
) )
errors = solved_result.errors errors = solved_result.errors
if not errors: if errors:
validation_error = RequestValidationError(
_normalize_errors(errors), body=body
)
raise validation_error
else:
raw_response = await run_endpoint_function( raw_response = await run_endpoint_function(
dependant=dependant, dependant=dependant,
values=solved_result.values, values=solved_result.values,
@ -339,11 +344,6 @@ def get_request_handler(
if not is_body_allowed_for_status_code(response.status_code): if not is_body_allowed_for_status_code(response.status_code):
response.body = b"" response.body = b""
response.headers.raw.extend(solved_result.response.headers.raw) response.headers.raw.extend(solved_result.response.headers.raw)
if errors:
validation_error = RequestValidationError(
_normalize_errors(errors), body=body
)
raise validation_error
if response is None: if response is None:
raise FastAPIError( raise FastAPIError(
"No response object was returned. There's a high chance that the " "No response object was returned. There's a high chance that the "

98
tests/test_dependency_contextmanager.py

@ -2,7 +2,7 @@ import json
from typing import Dict from typing import Dict
import pytest import pytest
from fastapi import BackgroundTasks, Depends, FastAPI from fastapi import BackgroundTasks, Depends, FastAPI, Query
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
@ -12,6 +12,8 @@ state = {
"/sync": "generator not started", "/sync": "generator not started",
"/async_raise": "asyncgen raise not started", "/async_raise": "asyncgen raise not started",
"/sync_raise": "generator raise not started", "/sync_raise": "generator raise not started",
"/async_rewrite_exception": "asyncgen rewrite exception not started",
"/sync_rewrite_exception": "generator rewrite exception not started",
"context_a": "not started a", "context_a": "not started a",
"context_b": "not started b", "context_b": "not started b",
"bg": "not set", "bg": "not set",
@ -71,6 +73,28 @@ def generator_state_try(state: Dict[str, str] = Depends(get_state)):
state["/sync_raise"] = "generator raise finalized" state["/sync_raise"] = "generator raise finalized"
async def asyncgen_state_rewrite_exception(state: Dict[str, str] = Depends(get_state)):
state["/async_rewrite_exception"] = "asyncgen rewrite exception started"
try:
yield state["/async_rewrite_exception"]
except Exception as error:
errors.append("/async_rewrite_exception")
raise OtherDependencyError() from error
finally:
state["/async_rewrite_exception"] = "asyncgen rewrite exception finalized"
def generator_state_rewrite_exception(state: Dict[str, str] = Depends(get_state)):
state["/sync_rewrite_exception"] = "generator rewrite exception started"
try:
yield state["/sync_rewrite_exception"]
except Exception as error:
errors.append("/sync_rewrite_exception")
raise OtherDependencyError() from error
finally:
state["/sync_rewrite_exception"] = "generator rewrite exception finalized"
async def context_a(state: dict = Depends(get_state)): async def context_a(state: dict = Depends(get_state)):
state["context_a"] = "started a" state["context_a"] = "started a"
try: try:
@ -121,6 +145,26 @@ async def get_sync_raise_other(state: str = Depends(generator_state_try)):
raise OtherDependencyError() raise OtherDependencyError()
@app.get("/async_rewrite_exception")
async def get_async_rewrite_exception(
state: str = Depends(asyncgen_state_rewrite_exception),
do_raise: bool = Query(),
):
assert state == "asyncgen rewrite exception started"
if do_raise:
raise AsyncDependencyError()
@app.get("/sync_rewrite_exception")
def get_sync_rewrite_exception(
state: str = Depends(generator_state_rewrite_exception),
do_raise: bool = Query(),
):
assert state == "generator rewrite exception started"
if do_raise:
raise SyncDependencyError()
@app.get("/context_b") @app.get("/context_b")
async def get_context_b(state: dict = Depends(context_b)): async def get_context_b(state: dict = Depends(context_b)):
return state return state
@ -263,6 +307,58 @@ def test_async_raise_server_error():
errors.clear() errors.clear()
def test_async_rewrite_exception_no_raise():
state["/async_rewrite_exception"] = "asyncgen rewrite exception not started"
client.get("/async_rewrite_exception?do_raise=false")
assert state["/async_rewrite_exception"] == "asyncgen rewrite exception finalized"
assert "/async_rewrite_exception" not in errors
errors.clear()
def test_async_rewrite_exception_handler_raise():
state["/async_rewrite_exception"] = "asyncgen rewrite exception not started"
with pytest.raises(OtherDependencyError):
client.get("/async_rewrite_exception?do_raise=true")
assert state["/async_rewrite_exception"] == "asyncgen rewrite exception finalized"
assert "/async_rewrite_exception" in errors
errors.clear()
def test_async_rewrite_exception_validator_raise():
state["/async_rewrite_exception"] = "asyncgen rewrite exception not started"
with pytest.raises(OtherDependencyError):
client.get("/async_rewrite_exception?do_raise=invalid_value")
assert state["/async_rewrite_exception"] == "asyncgen rewrite exception finalized"
assert "/async_rewrite_exception" in errors
errors.clear()
def test_sync_rewrite_exception_no_raise():
state["/sync_rewrite_exception"] = "generator rewrite exception not started"
client.get("/sync_rewrite_exception?do_raise=false")
assert state["/sync_rewrite_exception"] == "generator rewrite exception finalized"
assert "/sync_rewrite_exception" not in errors
errors.clear()
def test_sync_rewrite_exception_handler_raise():
state["/sync_rewrite_exception"] = "generator rewrite exception not started"
with pytest.raises(OtherDependencyError):
client.get("/sync_rewrite_exception?do_raise=true")
assert state["/sync_rewrite_exception"] == "generator rewrite exception finalized"
assert "/sync_rewrite_exception" in errors
errors.clear()
def test_sync_rewrite_exception_validator_raise():
state["/sync_rewrite_exception"] = "generator rewrite exception not started"
with pytest.raises(OtherDependencyError):
client.get("/sync_rewrite_exception?do_raise=invalid_value")
assert state["/sync_rewrite_exception"] == "generator rewrite exception finalized"
assert "/sync_rewrite_exception" in errors
errors.clear()
def test_context_b(): def test_context_b():
response = client.get("/context_b") response = client.get("/context_b")
data = response.json() data = response.json()

Loading…
Cancel
Save