Closed jean-edouard-boulanger closed 1 year ago
Hi @jean-edouard-boulanger, if I understand it correctly, your expectation is to return a list like [User(user_id="1"), User(user_id="2")] directly. I think it's possible but will need to use some tricks.
Yes this is absolutely correct! I don't particularly like the alternative (using gen_list_model
in the view) and I think it'd be much nicer if spectree allowed returning a list of models directly.
Yes this is absolutely correct! I don't particularly like the alternative (using
gen_list_model
in the view) and I think it'd be much nicer if spectree allowed returning a list of models directly.
Yes, returning a list directly is more convenient.
As for the skip_validation
part, if it can return both BaseModel instances and dictionaries, it might be hard to decide whether to validate it or not.
With the commit I have just pushed, we're getting much closer to your suggestion:
.dict()
)User
), validation is skipped@app.route("/api/example1", methods=["GET"])
@api.validate(resp=Response(HTTP_200=List[User]))
def return_list():
return [User(id=1), User(id=2)]
@app.route("/api/example2", methods=["GET"])
@api.validate(resp=Response(HTTP_200=List[User]))
def return_list():
return [User(id=1), {"id": 2}]
@app.route("/api/example3", methods=["GET"])
@api.validate(resp=Response(HTTP_200=List[User]))
def return_list():
return [{"id": 1}, {"id": 2}]
@app.route("/api/example4", methods=["GET"])
@api.validate(resp=Response(HTTP_200=List[User]))
def return_list():
return [OtherUserModel(id=1), OtherUserModel(id=2)]
@kemingy Common validation logic now implemented for both Falcon sync/async implementations (see FalconPlugin.response_validation
). Tests were updated accordingly.
Thanks for the help merging this @kemingy 🙌
Btw, I have noticed that returning Pydantic root models from views is also not supported. Would you be interested in a fix for this too?
Btw, I have noticed that returning Pydantic root models from views is also not supported. Would you be interested in a fix for this too?
I'll take #329. You can fix the views if you're interested. Thanks.
Description
Currently,
spectree
only partially supports validation of list responses. For example:You'll notice in particular that individual entries must currently be serialized before returning or
spectree
crashes. This is inconsistent with the rest of the interface, because it's perfectly okay to return models (without serializing them) - so why not list of models?The change basically updates individual plugins to serialize individual list entries (when needed) before the final response is created. The change is conservative and the above logic is only applied when all of the below applies:
BaseModel
subclass.I have not changed any of the existing validation logic in this case: we still rely on the generated root model for validation.
Change summary:
Response(HTTP_200=List[User])
no longer triggers IDEs / type validation frameworks.