Browse Source

100% test coverage

pull/97/head
Mohammed 6 years ago
parent
commit
7514ac6fb0
  1. 39
      fastapi/routing.py
  2. 3
      tests/test_additional_responses.py

39
fastapi/routing.py

@ -147,36 +147,19 @@ class APIRoute(routing.Route):
), f"(Duplicated Status Code): Response with status code [{add_response.status_code}] already defined!" ), f"(Duplicated Status Code): Response with status code [{add_response.status_code}] already defined!"
existed_codes.append(add_response.status_code) existed_codes.append(add_response.status_code)
response_models: List[Any] = [m for m in add_response.models] response_models: List[Any] = [m for m in add_response.models]
valid_response_models = True schema_field = None
try:
valid_response_models = all(
[issubclass(m, BaseModel) for m in response_models]
)
except TypeError:
valid_response_models = False
if not valid_response_models:
raise ValueError(
"All response models must be "
"a subclass of `pydantic.BaseModel` "
"model."
)
if add_response.content_type == "application/json" or lenient_issubclass( if add_response.content_type == "application/json" or lenient_issubclass(
content_type, JSONResponse content_type, JSONResponse
): ) and len(response_models):
if len(response_models): schema_field = Field(
schema_field = Field( name=f"Additional_response_{add_response.status_code}",
name=f"Additional_response_{add_response.status_code}", type_=Union[tuple(response_models)],
type_=Union[tuple(response_models)], class_validators=[],
class_validators=[], default=None,
default=None, required=False,
required=False, model_config=UnconstrainedConfig,
model_config=UnconstrainedConfig, schema=Schema(None),
schema=Schema(None), )
)
else:
schema_field = None
else:
schema_field = None
add_resp_description = AdditionalResponseDescription( add_resp_description = AdditionalResponseDescription(
description=add_response.description, description=add_response.description,
content_type=add_response.content_type, content_type=add_response.content_type,

3
tests/test_additional_responses.py

@ -2,6 +2,7 @@ import pytest
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.openapi.models import AdditionalResponse from fastapi.openapi.models import AdditionalResponse
from pydantic import BaseModel from pydantic import BaseModel
from pydantic.error_wrappers import ValidationError
from starlette.responses import JSONResponse from starlette.responses import JSONResponse
from starlette.testclient import TestClient from starlette.testclient import TestClient
@ -419,7 +420,7 @@ def test_uncompatible_response_model_undecorated():
response_403 = AdditionalResponse( response_403 = AdditionalResponse(
status_code=403, description="Forbidden", models=[NotBaseModel] status_code=403, description="Forbidden", models=[NotBaseModel]
) )
with pytest.raises(ValueError): with pytest.raises(RuntimeError):
app.add_api_route("/", get_not_decorated, additional_responses=[response_403]) app.add_api_route("/", get_not_decorated, additional_responses=[response_403])

Loading…
Cancel
Save