1
+ from typing import Iterable
2
+
1
3
from fastapi import APIRouter , status
2
- from pydantic import BaseModel , RootModel , ConfigDict
4
+ from pydantic import BaseModel , ConfigDict
3
5
4
6
from domains .books import BookService , dto
5
7
@@ -20,23 +22,25 @@ class CreateBookResponse(BaseModel):
20
22
)
21
23
22
24
23
- class ListBooksResponse (RootModel ):
24
- root : list [dto .Book ]
25
+ class ListBooksResponse (BaseModel ):
26
+ books : Iterable [dto .Book ]
25
27
model_config = ConfigDict (
26
28
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
+ }
40
44
}
41
45
)
42
46
@@ -65,11 +69,12 @@ class CreateBookRequest(BaseModel):
65
69
into the format needed for the proper HTTP Response
66
70
"""
67
71
72
+
68
73
@router_v1 .get ("/" , status_code = status .HTTP_200_OK )
69
74
async def list_books () -> ListBooksResponse :
70
75
book_service = BookService ()
71
76
books = await book_service .list_books ()
72
- return ListBooksResponse (root = books )
77
+ return ListBooksResponse (books = books )
73
78
74
79
75
80
@router_v1 .post ("/" , status_code = status .HTTP_201_CREATED )
0 commit comments