From 7a1853ce0216a40b32ba033ef7e59aaa4d45188a Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Mon, 26 May 2025 15:17:49 +0300 Subject: [PATCH] Features: 1) Add `@extend_schema_field` for `get_recently_viewed` to improve OpenAPI schema generation. Fixes: 1) Correct logic in `get_recently_viewed` method to properly handle `recently_viewed` items. Extra: 1) Add missing import for `drf_spectacular.utils.extend_schema_field`; 2) Update method docstring for clarity; 3) Adjust typing in `get_recently_viewed` to use `List[Dict[str, Any]]`. --- vibes_auth/serializers.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/vibes_auth/serializers.py b/vibes_auth/serializers.py index 0c764775..03470884 100644 --- a/vibes_auth/serializers.py +++ b/vibes_auth/serializers.py @@ -1,12 +1,13 @@ import logging from contextlib import suppress -from typing import Any, Dict, Optional, Type +from typing import Any, Dict, List, Optional, Type from constance import config from django.contrib.auth import authenticate from django.contrib.auth.models import update_last_login from django.contrib.auth.password_validation import validate_password from django.utils.translation import gettext_lazy as _ +from drf_spectacular.utils import extend_schema_field from rest_framework.exceptions import AuthenticationFailed, ValidationError from rest_framework.fields import ( BooleanField, @@ -87,8 +88,15 @@ class UserSerializer(ModelSerializer): validate_password(attrs["password"]) return attrs - def get_recently_viewed(self, obj) -> ProductSimpleSerializer.data: - return ProductSimpleSerializer(Product.objects.filter(uuid__in=([] or obj.recently_viewed)), many=True).data + @extend_schema_field(ProductSimpleSerializer(many=True)) + def get_recently_viewed(self, obj) -> List[Dict[str, Any]]: + """ + Returns a list of serialized ProductSimpleSerializer representations + for the UUIDs in obj.recently_viewed. + """ + queryset = Product.objects.filter(uuid__in=obj.recently_viewed) + serializer = ProductSimpleSerializer(queryset, many=True) + return serializer.data class TokenObtainSerializer(Serializer):