Skip to content

Commit 2827300

Browse files
committed
feat:兼容swaager返回值为列表
1 parent efd9291 commit 2827300

3 files changed

Lines changed: 18 additions & 22 deletions

File tree

app/api/cms/log.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from app.api import lindoc
1212
from app.validator.schema import (
1313
AuthorizationSchema,
14-
LogListSchema,
14+
LogPageSchema,
1515
LogQuerySearchSchema,
16-
NameListSchema,
16+
StringList,
1717
)
1818

1919
log_api = Redprint("log")
@@ -26,7 +26,7 @@
2626
@lindoc.validate(
2727
headers=AuthorizationSchema,
2828
query=LogQuerySearchSchema,
29-
resp=DocResponse(http_200=LogListSchema),
29+
resp=DocResponse(http_200=LogPageSchema),
3030
before=LogQuerySearchSchema.offset_handler,
3131
tags=["日志"],
3232
)
@@ -49,7 +49,7 @@ def get_logs():
4949
)
5050
total_page = math.ceil(total / g.count)
5151

52-
return LogListSchema(
52+
return LogPageSchema(
5353
page=g.page, count=g.count, total=total, items=items, total_page=total_page
5454
)
5555

@@ -59,7 +59,7 @@ def get_logs():
5959
@group_required
6060
@lindoc.validate(
6161
headers=AuthorizationSchema,
62-
resp=DocResponse(http_200=NameListSchema),
62+
resp=DocResponse(http_200=StringList),
6363
tags=["日志"],
6464
)
6565
def get_users_for_log():
@@ -73,4 +73,4 @@ def get_users_for_log():
7373
.having(text("count(username) > 0"))
7474
.all()
7575
)
76-
return NameListSchema(items=[u.username for u in usernames])
76+
return StringList.parse_obj([u.username for u in usernames])

app/api/v1/book.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from app.model.v1.book import Book
1717
from app.validator.schema import (
1818
AuthorizationSchema,
19-
BookListSchema,
19+
BookSchemaList,
2020
BookQuerySearchSchema,
2121
BookSchema,
2222
)
@@ -41,23 +41,21 @@ def get_book(id: int):
4141

4242
@book_api.route("", methods=["GET"])
4343
@lindoc.validate(
44-
resp=DocResponse(http_200=BookListSchema),
44+
resp=DocResponse(http_200=BookSchemaList),
4545
tags=["图书"],
4646
)
4747
def get_books():
4848
"""
4949
获取图书列表
5050
"""
5151
books = Book.get(one=False)
52-
# TODO JSON
53-
# return BookListSchema(items=books)
54-
return books
52+
return BookSchemaList.parse_obj(books)
5553

5654

5755
@book_api.route("/search", methods=["GET"])
5856
@lindoc.validate(
5957
query=BookQuerySearchSchema,
60-
resp=DocResponse(BookNotFound, http_200=BookListSchema),
58+
resp=DocResponse(BookNotFound, http_200=BookSchemaList),
6159
tags=["图书"],
6260
)
6361
def search():
@@ -68,10 +66,8 @@ def search():
6866
Book.title.like("%" + g.q + "%"), Book.delete_time == None
6967
).all()
7068
if books:
71-
return books
69+
return BookSchemaList.parse_obj(books)
7270
raise BookNotFound
73-
# TODO JSON
74-
# return BookListSchema(items=books)
7571

7672

7773
@book_api.route("", methods=["POST"])

app/validator/schema.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class AuthorizationSchema(BaseModel):
1616

1717

1818
class BookQuerySearchSchema(BaseModel):
19-
q: Optional[str] = None
19+
q: Optional[str] = str()
2020

2121

22-
class NameListSchema(BaseModel):
23-
items: List[str] = list()
22+
class StringList(BaseModel):
23+
__root__: List[str]
2424

2525

2626
class LogQuerySearchSchema(BaseModel):
@@ -53,15 +53,15 @@ class LogSchema(BaseModel):
5353
permission: str
5454

5555

56-
class BaseListSchema(BaseModel):
56+
class BasePageSchema(BaseModel):
5757
page: int
5858
count: int
5959
total: int
6060
total_page: int
6161
items: List[Any]
6262

6363

64-
class LogListSchema(BaseListSchema):
64+
class LogPageSchema(BasePageSchema):
6565
items: List[LogSchema]
6666

6767

@@ -72,8 +72,8 @@ class BookSchema(BaseModel):
7272
summary: str
7373

7474

75-
class BookListSchema(BaseModel):
76-
items: List[BookSchema]
75+
class BookSchemaList(BaseModel):
76+
__root__: List[BookSchema]
7777

7878

7979
class Language(str, Enum):

0 commit comments

Comments
 (0)