Browse Source

♻️ Update Pydantic usage, types, values, minor structure changes (#164)

pull/168/head
Sebastián Ramírez 6 years ago
committed by GitHub
parent
commit
546d233dec
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      fastapi/dependencies/utils.py
  2. 11
      fastapi/routing.py
  3. 7
      fastapi/utils.py

22
fastapi/dependencies/utils.py

@ -22,8 +22,8 @@ from fastapi.dependencies.models import Dependant, SecurityRequirement
from fastapi.security.base import SecurityBase from fastapi.security.base import SecurityBase
from fastapi.security.oauth2 import OAuth2, SecurityScopes from fastapi.security.oauth2 import OAuth2, SecurityScopes
from fastapi.security.open_id_connect_url import OpenIdConnect from fastapi.security.open_id_connect_url import OpenIdConnect
from fastapi.utils import UnconstrainedConfig, get_path_param_names from fastapi.utils import get_path_param_names
from pydantic import Schema, create_model from pydantic import BaseConfig, Schema, create_model
from pydantic.error_wrappers import ErrorWrapper from pydantic.error_wrappers import ErrorWrapper
from pydantic.errors import MissingError from pydantic.errors import MissingError
from pydantic.fields import Field, Required, Shape from pydantic.fields import Field, Required, Shape
@ -203,8 +203,8 @@ def add_param_to_fields(
default=None if required else default_value, default=None if required else default_value,
alias=alias, alias=alias,
required=required, required=required,
model_config=UnconstrainedConfig, model_config=BaseConfig,
class_validators=[], class_validators={},
schema=schema, schema=schema,
) )
if schema.in_ == params.ParamTypes.path: if schema.in_ == params.ParamTypes.path:
@ -237,8 +237,8 @@ def add_param_to_body_fields(*, param: inspect.Parameter, dependant: Dependant)
default=None if required else default_value, default=None if required else default_value,
alias=schema.alias or param.name, alias=schema.alias or param.name,
required=required, required=required,
model_config=UnconstrainedConfig, model_config=BaseConfig,
class_validators=[], class_validators={},
schema=schema, schema=schema,
) )
dependant.body_params.append(field) dependant.body_params.append(field)
@ -336,7 +336,7 @@ def request_params_to_args(
ErrorWrapper( ErrorWrapper(
MissingError(), MissingError(),
loc=(schema.in_.value, field.alias), loc=(schema.in_.value, field.alias),
config=UnconstrainedConfig, config=BaseConfig,
) )
) )
else: else:
@ -379,9 +379,7 @@ async def request_body_to_args(
if field.required: if field.required:
errors.append( errors.append(
ErrorWrapper( ErrorWrapper(
MissingError(), MissingError(), loc=("body", field.alias), config=BaseConfig
loc=("body", field.alias),
config=UnconstrainedConfig,
) )
) )
else: else:
@ -456,8 +454,8 @@ def get_body_field(*, dependant: Dependant, name: str) -> Optional[Field]:
type_=BodyModel, type_=BodyModel,
default=None, default=None,
required=required, required=required,
model_config=UnconstrainedConfig, model_config=BaseConfig,
class_validators=[], class_validators={},
alias="body", alias="body",
schema=BodySchema(None), schema=BodySchema(None),
) )

11
fastapi/routing.py

@ -7,8 +7,7 @@ from fastapi import params
from fastapi.dependencies.models import Dependant from fastapi.dependencies.models import Dependant
from fastapi.dependencies.utils import get_body_field, get_dependant, solve_dependencies from fastapi.dependencies.utils import get_body_field, get_dependant, solve_dependencies
from fastapi.encoders import jsonable_encoder from fastapi.encoders import jsonable_encoder
from fastapi.utils import UnconstrainedConfig from pydantic import BaseConfig, BaseModel, Schema
from pydantic import BaseModel, Schema
from pydantic.error_wrappers import ErrorWrapper, ValidationError from pydantic.error_wrappers import ErrorWrapper, ValidationError
from pydantic.fields import Field from pydantic.fields import Field
from pydantic.utils import lenient_issubclass from pydantic.utils import lenient_issubclass
@ -59,7 +58,7 @@ def get_app(
if body_bytes: if body_bytes:
body = await request.json() body = await request.json()
except Exception as e: except Exception as e:
logging.error("Error getting request body", e) logging.error(f"Error getting request body: {e}")
raise HTTPException( raise HTTPException(
status_code=400, detail="There was an error parsing the body" status_code=400, detail="There was an error parsing the body"
) )
@ -126,10 +125,10 @@ class APIRoute(routing.Route):
self.response_field: Optional[Field] = Field( self.response_field: Optional[Field] = Field(
name=response_name, name=response_name,
type_=self.response_model, type_=self.response_model,
class_validators=[], class_validators={},
default=None, default=None,
required=False, required=False,
model_config=UnconstrainedConfig, model_config=BaseConfig,
schema=Schema(None), schema=Schema(None),
) )
else: else:
@ -155,7 +154,7 @@ class APIRoute(routing.Route):
class_validators=None, class_validators=None,
default=None, default=None,
required=False, required=False,
model_config=UnconstrainedConfig, model_config=BaseConfig,
schema=Schema(None), schema=Schema(None),
) )
response_fields[additional_status_code] = response_field response_fields[additional_status_code] = response_field

7
fastapi/utils.py

@ -3,17 +3,12 @@ from typing import Any, Dict, List, Sequence, Set, Type
from fastapi import routing from fastapi import routing
from fastapi.openapi.constants import REF_PREFIX from fastapi.openapi.constants import REF_PREFIX
from pydantic import BaseConfig, BaseModel from pydantic import BaseModel
from pydantic.fields import Field from pydantic.fields import Field
from pydantic.schema import get_flat_models_from_fields, model_process_schema from pydantic.schema import get_flat_models_from_fields, model_process_schema
from starlette.routing import BaseRoute from starlette.routing import BaseRoute
class UnconstrainedConfig(BaseConfig):
min_anystr_length = None
max_anystr_length = None
def get_flat_models_from_routes( def get_flat_models_from_routes(
routes: Sequence[Type[BaseRoute]] routes: Sequence[Type[BaseRoute]]
) -> Set[Type[BaseModel]]: ) -> Set[Type[BaseModel]]:

Loading…
Cancel
Save