feat: update pydantic

This commit is contained in:
Georg K
2023-07-14 04:40:57 +03:00
parent a94c9d4863
commit 93e391b7b2
7 changed files with 72 additions and 74 deletions

View File

@@ -85,7 +85,7 @@ class BodyGetter(AbstractInjector):
# to a dict. Prevent this by requiring the body to be a dict for object models.
if self._expect_object and not isinstance(body, dict):
raise HTTPBadRequest(
text='[{"in": "body", "loc": ["__root__"], "msg": "value is not a '
text='[{"loc_in": "body", "loc": ["root"], "msg": "value is not a '
'valid dict", "type": "type_error.dict"}]',
content_type="application/json",
) from None

View File

@@ -5,7 +5,7 @@ from typing import List, Type, Optional, get_type_hints
from aiohttp.web import Response, json_response
from aiohttp.web_app import Application
from pydantic import BaseModel
from pydantic import BaseModel, RootModel
from aiohttp_pydantic.oas.struct import OpenApiSpec3, OperationObject, PathItem
from . import docstring_parser
@@ -32,7 +32,7 @@ class _OASResponseBuilder:
response_schema = obj.schema(
ref_template="#/components/schemas/{model}"
).copy()
if def_sub_schemas := response_schema.pop("definitions", None):
if def_sub_schemas := response_schema.pop("$defs", None):
self._oas.components.schemas.update(def_sub_schemas)
self._oas.components.schemas.update({response_schema['title']: response_schema})
return {'$ref': f'#/components/schemas/{response_schema["title"]}'}
@@ -99,7 +99,7 @@ def _add_http_method_to_oas(
.schema(ref_template="#/components/schemas/{model}")
.copy()
)
if def_sub_schemas := body_schema.pop("definitions", None):
if def_sub_schemas := body_schema.pop("$defs", None):
oas.components.schemas.update(def_sub_schemas)
oas_operation.request_body.content = {
@@ -117,19 +117,19 @@ def _add_http_method_to_oas(
oas_operation.parameters[i].in_ = args_location
oas_operation.parameters[i].name = name
attrs = {"__annotations__": {"__root__": type_}}
attrs = {"__annotations__": {"root": type_}}
if name in defaults:
attrs["__root__"] = defaults[name]
attrs["root"] = defaults[name]
oas_operation.parameters[i].required = False
else:
oas_operation.parameters[i].required = True
oas_operation.parameters[i].schema = type(name, (BaseModel,), attrs).schema(
oas_operation.parameters[i].schema = type(name, (RootModel,), attrs).schema(
ref_template="#/components/schemas/{model}"
)
# move definitions
if def_sub_schemas := oas_operation.parameters[i].schema.pop("definitions", None):
if def_sub_schemas := oas_operation.parameters[i].schema.pop("$defs", None):
oas.components.schemas.update(def_sub_schemas)
return_type = get_type_hints(handler).get("return")