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
Add example for using dataclasses with marshmallow-dataclass #305
Conversation
44c61e6
to
e408f37
Compare
Thank you for working on this. However, after reading marshmallow-dataclass's docs and issue list, I think it's not a good idea to add direct support in APIFlask for it. For example:
IMO, It's not mature enough. Users can just install this library and convert the dataclasses to schema before passing it to Could you refactor this PR to keep only the docs and the example application? Besides, as I said in the review comment, the custom field should be set with from dataclasses import dataclass, field
import marshmallow_dataclass
import marshmallow.validate
@dataclass
class Person:
name: str = field(
metadata=dict(
load_only=True, metadata=dict(description="The person's first name")
)
)
height: float = field(metadata=dict(validate=marshmallow.validate.Range(min=0))) Sorry for changing my mind, I didn't think it through when I create that issue. |
np. likely I'll work on it tomorrow This is how I found out, is it true?
do I have to delete previous commits or you are with Squash merging PR to |
For the sake of PR cleanliness, I resolve reviews |
Yes, we introduce how to use marshmallow-dataclass with APIFlask, the docs will like this:
You can rebase the commits or I will squash them. |
9dae846
to
af00e97
Compare
@greyli I've done |
af00e97
to
57222d6
Compare
❌ Deploy Preview for apiflask failed.
|
3f8fa2c
to
5a18368
Compare
@greyli I do not know exactly how to solve this problem. Do you have a suggestion? Requirements files are also created by pip-compile-multi. |
Just revert the changes to requirements files, I will update them later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some typos and grammar errors in the docs, and the example application is not actually working. Please fix these issues.
docs/schema.md
Outdated
pip install marshmallow-dataclass | ||
``` | ||
|
||
when you use dataclass, allthings same as the normal schema, but your must pass `Dataclass.Schema` to some cases: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The object passed to APIFlask should always be a schema (i.e. dataclass.Schema
).
docs/schema.md
Outdated
|
||
|
||
@app.get('/pets/<int:pet_id>') | ||
@app.output(Pet) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Pet
should be Pet.Schema
.
examples/dataclass/app.py
Outdated
from apiflask import APIFlask, abort | ||
from apiflask.validators import Length, OneOf | ||
|
||
if dataclass is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This if
statement makes no sense.
examples/dataclass/app.py
Outdated
|
||
|
||
@app.get('/pets/<int:pet_id>') | ||
@app.output(PetOutDataclass) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The object passed to APIFlask should always be a schema (i.e. dataclass.Schema
).
docs/schema.md
Outdated
|
||
in this example we not need to use `Pet.Schema()` in the output decorator, because the pets object is a dataclass. | ||
|
||
another example but with `Pet.Schema`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description here does not seem to be related to the following example.
examples/dataclass/app.py
Outdated
def update_pet(pet_id, data): | ||
if pet_id > len(pets) - 1: | ||
abort(404) | ||
data = PetInDataclass.Schema().dump(data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems unnecessary to convert the data objec to dict, why don't just do this:
pets[pet_id].name = data.name
pets[pet_id].category = data.category
…#305 Co-authored-by: Grey Li <withlihui@gmail.com>
f53733d
to
61062b6
Compare
…#305 Co-authored-by: Grey Li <withlihui@gmail.com>
61062b6
to
2fdc554
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check the two remaining review comments.
…#305 Co-authored-by: Grey Li <withlihui@gmail.com>
2fdc554
to
819787d
Compare
yes, sorry. I think All is done. If there is a problem, please tell me to solve it |
Merged, thanks! |
Checklist:
docs
folder and in code docstring.CHANGES.md
summarizing the change and linking to the issue and your username.*Version changed*
or*Version added*
note in any relevant docs and docstring.pytest
andtox
, no tests failed.