1+ from typing import Iterable
2+
13from fastapi import APIRouter , status
2- from pydantic import BaseModel , RootModel , ConfigDict
4+ from pydantic import BaseModel , ConfigDict
35
46from domains .books import BookService , dto
57
@@ -20,23 +22,25 @@ class CreateBookResponse(BaseModel):
2022 )
2123
2224
23- class ListBooksResponse (RootModel ):
24- root : list [dto .Book ]
25+ class ListBooksResponse (BaseModel ):
26+ books : Iterable [dto .Book ]
2527 model_config = ConfigDict (
2628 json_schema_extra = {
27- "example" :
28- [
29- {
30- "title" : "The Hitchhiker's Guide to the Galaxy" ,
31- "author_name" : "Douglas Adams" ,
32- "book_id" : 123 ,
33- },
34- {
35- "title" : "Clean Architecture: A Craftsman's Guide to Software Structure and Design" ,
36- "author_name" : "Robert C. 'Uncle Bob' Martin" ,
37- "book_id" : 321 ,
38- },
39- ]
29+ "example" : {
30+ "books" : [
31+ {
32+ "title" : "The Hitchhiker's Guide to the Galaxy" ,
33+ "author_name" : "Douglas Adams" ,
34+ "book_id" : 123 ,
35+ },
36+ {
37+ "title" : "Clean Architecture: "
38+ "A Craftsman's Guide to Software Structure and Design" ,
39+ "author_name" : "Robert C. 'Uncle Bob' Martin" ,
40+ "book_id" : 321 ,
41+ },
42+ ]
43+ }
4044 }
4145 )
4246
@@ -65,11 +69,12 @@ class CreateBookRequest(BaseModel):
6569 into the format needed for the proper HTTP Response
6670"""
6771
72+
6873@router_v1 .get ("/" , status_code = status .HTTP_200_OK )
6974async def list_books () -> ListBooksResponse :
7075 book_service = BookService ()
7176 books = await book_service .list_books ()
72- return ListBooksResponse (root = books )
77+ return ListBooksResponse (books = books )
7378
7479
7580@router_v1 .post ("/" , status_code = status .HTTP_201_CREATED )
0 commit comments