44from typing import Any , Literal
55
66from odmantic import query
7+ from pydantic import TypeAdapter
78
89from src .dependencies import Db
910from src .models import Idea , IdeaDownvote , IdeaPublic , IdeasPublic , IdeaUpvote , User
1011
12+ idea_list_adapter = TypeAdapter (list [IdeaPublic ])
13+
1114
1215async def count_ideas (db : Db , user : User | None = None ) -> int :
1316 if user is not None :
@@ -45,7 +48,7 @@ async def get_ideas(db: Db, skip: int, limit: int, sort: str | None = None):
4548 else :
4649 ideas = await db .find (Idea , limit = limit , skip = skip , sort = Idea .name )
4750 return IdeasPublic (
48- data = [ IdeaPublic ( ** idea . model_dump ()) for idea in ideas ] ,
51+ data = idea_list_adapter . validate_python ( ideas , from_attributes = True ) ,
4952 count = await count_ideas (db ),
5053 )
5154
@@ -62,7 +65,7 @@ async def get_user_ideas(
6265 count = await count_ideas (db , user )
6366
6467 return IdeasPublic (
65- data = [ IdeaPublic ( ** idea . model_dump ()) for idea in ideas ] , count = count
68+ data = idea_list_adapter . validate_python ( ideas , from_attributes = True ) , count = count
6669 )
6770
6871
@@ -78,7 +81,8 @@ async def get_voted_ideas(
7881 Idea , query .in_ (Idea .id , votes ), limit = limit , skip = skip , sort = Idea .name
7982 )
8083 return IdeasPublic (
81- data = [IdeaPublic (** idea .model_dump ()) for idea in ideas ], count = len (votes )
84+ data = idea_list_adapter .validate_python (ideas , from_attributes = True ),
85+ count = len (votes ),
8286 )
8387
8488
0 commit comments