-
-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature]Can't support list type swagger response #96
Comments
One possible way is to use class Item(BaseModel):
name: str
price: float
class ItemList(BaseModel):
__root__: List[Item]
@app.route("/", method=["POST"])
@api.validate(json=Item, resp=Response(HTTP_200=ItemList), tags=["demo"])
def demo():
item = request.context.json
return ItemList.parse_obj([item, item, item]) By the way, you don't need to parse |
I really appreciate your help. 😄 Thanks again |
This syntax looks nice. But I feel it can be difficult to support. Because it means the library should be able to verify any types:
I'm not sure if there is an easy way to do this. Let me know if you have any ideas. |
FastAPI seems to support this syntax, I will create a pr when i complete it if needed. |
Sure. Thanks for your help.
…On Mon, 14 Dec 2020 at 12:08, 水月 ***@***.***> wrote:
FastAPI seems to support this syntax, I will create a pr when i complete
it if needed.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#96 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADC7UXMQ55SA5MTMXABDSP3SUWFVTANCNFSM4UZUQYUA>
.
|
Data validation is easy. Pydantic provides a function to load data of any supported type or a combination thereof. One just needs to handle registry into https://pydantic-docs.helpmanual.io/usage/models/#parsing-data-into-a-specified-type |
Has anyone undertaken this issue yet? It’s something I need and I was wondering if I should take a swing at it or if it’s already underway. |
This issue has been inactive for a long time. @onecrayon Feel free to create a WIP PR and link to this issue. Thanks for your help. |
Hey, guys! This feature is incredibly necessary! When are you going to do it? I really need it! |
If we narrow it down to support Hi, @onecrayon are you still willing to take this one? |
Possibly; my app that leverages SpecTree has been lying fallow for a bit here, but I’ve been hoping to get back into it. I’ll let you know if I start actual coding on this, but don’t want to make any solid promises at this point. |
After reading the source code of from typing import List, Type
from flask import Flask, jsonify
from spectree import SpecTree, Response
from pydantic import BaseModel
class User(BaseModel):
name: str
page: int
app = Flask(__name__)
spec = SpecTree("flask")
@app.route("/")
@spec.validate(resp=Response(HTTP_200=User))
def index():
return User(name="John", page=1)
def gen_list_model(model: Type[BaseModel]):
assert issubclass(model, BaseModel)
ListModel = type(
f"{model.__name__}List",
(BaseModel,),
{
"__annotations__": {"__root__": List[model]},
},
)
return ListModel
@app.route("/list")
@spec.validate(resp=Response(HTTP_200=gen_list_model(User)))
def list_user():
return jsonify([{"name": "John", "page": 1}, {"name": "Jane", "page": 2}])
if __name__ == "__main__":
spec.register(app)
app.run() |
@kemingy It looks like some kind of crutches...and looks not 'pythonic'. |
An error occurs when I try to run this code:
|
Sorry I cannot reproduce this with Python 3.10 |
I have created a pre-release: https://github.com/0b01001001/spectree/releases/tag/v0.10.0a1 Can try to use this one to see if it can fulfill your requirements. |
@kemingy what about update type hinting? ^^ |
@alexted you know it's an open-source project, right? |
like:
The text was updated successfully, but these errors were encountered: