Coverage for audoma/drf/decorators.py: 100%

1 from functools import wraps

2 from typing import (

3 Any,

4 Callable,

5)

6

7 from drf_spectacular.utils import extend_schema_field

8 from rest_framework import serializers

9

10

11 def document_and_format(serializer_or_field: Any) -> Callable:

12 def decorator(func: Callable) -> Callable:

13 @wraps(func)

14 @extend_schema_field(serializer_or_field)

15 def wrapper(*args, **kwargs) -> Any:

16 value = func(*args, **kwargs)

17 f = (

18 serializer_or_field

19 if isinstance(serializer_or_field, serializers.Field)

20 else serializer_or_field()

21 )

22 f.parent = args[0]

23 return f.to_representation(value) if value is not None else None

24

25 return wrapper

26

27 return decorator