version=1.7.2 fix oas.spec schema is broken after reloading the page

This commit is contained in:
Vincent Maillol 2021-02-27 08:08:12 +01:00
parent 4ff9739293
commit c6b979dcaf
3 changed files with 21 additions and 8 deletions

View File

@ -1,5 +1,5 @@
from .view import PydanticView
__version__ = "1.7.1"
__version__ = "1.7.2"
__all__ = ("PydanticView", "__version__")

View File

@ -44,7 +44,9 @@ class _OASResponseBuilder:
def _handle_pydantic_base_model(self, obj):
if is_pydantic_base_model(obj):
response_schema = obj.schema(ref_template="#/components/schemas/{model}")
response_schema = obj.schema(
ref_template="#/components/schemas/{model}"
).copy()
if def_sub_schemas := response_schema.pop("definitions", None):
self._oas.components.schemas.update(def_sub_schemas)
return response_schema
@ -104,8 +106,10 @@ def _add_http_method_to_oas(
status_code_descriptions = {}
if body_args:
body_schema = next(iter(body_args.values())).schema(
ref_template="#/components/schemas/{model}"
body_schema = (
next(iter(body_args.values()))
.schema(ref_template="#/components/schemas/{model}")
.copy()
)
if def_sub_schemas := body_schema.pop("definitions", None):
oas.components.schemas.update(def_sub_schemas)

View File

@ -77,10 +77,19 @@ async def generated_oas(aiohttp_client, loop) -> web.Application:
oas.setup(app)
client = await aiohttp_client(app)
response = await client.get("/oas/spec")
assert response.status == 200
assert response.content_type == "application/json"
return await response.json()
response_1 = await client.get("/oas/spec")
assert response_1.content_type == "application/json"
assert response_1.status == 200
content_1 = await response_1.json()
# Reload the page to ensure that content is always the same
# note: pydantic can return a cached dict, if a view updates
# the dict the output will be incoherent
response_2 = await client.get("/oas/spec")
content_2 = await response_2.json()
assert content_1 == content_2
return content_2
async def test_generated_oas_should_have_components_schemas(generated_oas):