Skip to content

Commit

Permalink
Add test, fix iteration error
Browse files Browse the repository at this point in the history
  • Loading branch information
khvn26 committed Apr 18, 2024
1 parent de516b8 commit 63b244f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/api/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_response_schemas(
) -> dict[str, Response]:
result = {}

for status_code in response_serializers:
for status_code in list(response_serializers):
if isinstance(response_serializers[status_code], type) and issubclass(
model_cls := response_serializers[status_code], BaseModel
):
Expand Down
71 changes: 71 additions & 0 deletions api/tests/unit/api/test_unit_openapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import pydantic
from drf_yasg.openapi import (
SCHEMA_DEFINITIONS,
ReferenceResolver,
Response,
Schema,
)
from pytest_mock import MockerFixture

from api.openapi import PydanticResponseCapableSwaggerAutoSchema


def test_pydantic_response_capable_auto_schema__renders_expected(
mocker: MockerFixture,
) -> None:
# Given
class Nested(pydantic.BaseModel):
usual_str: str
optional_int: int | None = None

class ResponseModel(pydantic.BaseModel):
nested_once: Nested
nested_list: list[Nested]

auto_schema = PydanticResponseCapableSwaggerAutoSchema(
view=mocker.MagicMock(),
path=mocker.MagicMock(),
method=mocker.MagicMock(),
components=ReferenceResolver("definitions", force_init=True),
request=mocker.MagicMock(),
overrides=mocker.MagicMock(),
)

# When
response_schemas = auto_schema.get_response_schemas({200: ResponseModel})

# Then
assert response_schemas == {
"200": Response(
description="ResponseModel",
schema=Schema(
title="ResponseModel",
required=["nested_once", "nested_list"],
type="object",
properties={
"nested_list": {
"items": {"$ref": "#/definitions/Nested"},
"title": "Nested List",
"type": "array",
},
"nested_once": {"$ref": "#/definitions/Nested"},
},
),
),
}
nested_schema = auto_schema.components.with_scope(SCHEMA_DEFINITIONS).get("Nested")
assert nested_schema == Schema(
title="Nested",
required=["usual_str"],
type="object",
properties={
"optional_int": {
"default": None,
"title": "Optional Int",
"type": "integer",
"x-nullable": True,
},
"usual_str": {"title": "Usual Str", "type": "string"},
},
x_nullable=True,
)

0 comments on commit 63b244f

Please sign in to comment.