| 1 |
from typing import TYPE_CHECKING |
| 2 |
|
| 3 |
import pytest |
| 4 |
from rest_framework import serializers |
| 5 |
|
| 6 |
from drf_yasg import openapi |
| 7 |
from drf_yasg.inspectors import SerializerMethodFieldInspector |
| 8 |
from drf_yasg.openapi import ReferenceResolver |
| 9 |
|
| 10 |
if TYPE_CHECKING: |
| 11 |
from uuid import UUID |
| 12 |
|
| 13 |
|
| 14 |
def test_missing_runtime_annotations(): |
| 15 |
class UserSerializer(serializers.Serializer): |
| 16 |
uuid = serializers.SerializerMethodField() |
| 17 |
|
| 18 |
def get_uuid(self, obj) -> "UUID": |
| 19 |
return obj.uuid |
| 20 |
|
| 21 |
field = UserSerializer().fields["uuid"] |
| 22 |
components = ReferenceResolver("definitions", "parameters", force_init=True) |
| 23 |
|
| 24 |
inspector = SerializerMethodFieldInspector( |
| 25 |
view=None, |
| 26 |
path="/", |
| 27 |
method="GET", |
| 28 |
components=components, |
| 29 |
request=None, |
| 30 |
field_inspectors=[], |
| 31 |
) |
| 32 |
|
| 33 |
with pytest.warns(UserWarning, match="Use `swagger_serializer_method`"): |
| 34 |
assert ( |
| 35 |
inspector.field_to_swagger_object(field, openapi.Schema, True).type |
| 36 |
== "string" |
| 37 |
) |