Skip to content

Add other Pydantic dict params to path operations - #264

Merged
tiangolo merged 6 commits into
masterfrom
pydantic-dict-params
May 27, 2019
Merged

tiangolo merged 6 commits into
masterfrom
pydantic-dict-params

Conversation

@tiangolo

Copy link
Copy Markdown
Member

Add other Pydantic dict params to path operations.

To include all the parameters supported by Pydantic's model's .dict().

@codecov

codecov Bot commented May 27, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #264 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           master   #264    +/-   ##
======================================
  Coverage     100%   100%            
======================================
  Files         183    190     +7     
  Lines        4539   4649   +110     
======================================
+ Hits         4539   4649   +110
Impacted Files Coverage Ξ”
...t_tutorial/test_response_model/test_tutorial004.py 100% <ΓΈ> (ΓΈ) ⬆️
docs/src/body_updates/tutorial001.py 100% <100%> (ΓΈ)
docs/src/response_model/tutorial006.py 100% <100%> (ΓΈ)
docs/src/response_model/tutorial005.py 100% <100%> (ΓΈ)
fastapi/routing.py 100% <100%> (ΓΈ) ⬆️
fastapi/applications.py 100% <100%> (ΓΈ) ⬆️
docs/src/response_model/tutorial004.py 100% <100%> (ΓΈ) ⬆️
...est_tutorial/test_body_updates/test_tutorial001.py 100% <100%> (ΓΈ)
...t_tutorial/test_response_model/test_tutorial006.py 100% <100%> (ΓΈ)
tests/test_operations_signatures.py 100% <100%> (ΓΈ)
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Ξ” = absolute <relative> (impact), ΓΈ = not affected, ? = missing data
Powered by Codecov. Last update 747ae82...59b6ca4. Read the comment docs.

@tiangolo
tiangolo merged commit 7b63bc5 into master May 27, 2019
@tiangolo
tiangolo deleted the pydantic-dict-params branch May 27, 2019 12:08
@bs32g1038

bs32g1038 commented May 27, 2019 •

Copy link
Copy Markdown

To be honest, these are good features, but I think it makes get, or other request functions bloated and less expansive.

  • too many fields are confusing. The function should keep simple. if we need select fields, that may be provide a extra higher order function to copy with the Pydantic model. what's more, directly using the the Pydantic model combination.
  • It doesn't make the code clearer and effectively reduce code duplication. most of data are composed of multiple models in daily use.

In my opinion, if the docs can auto generate the doc data by the real data, it will be a great thing.


class Item(BaseModel):
    name: str = None
    description: str = None
	
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar"},
}
	

@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]


# result
only show the name field in the doc model

because the response result only include the name field in the model. 

Besides, Fastapi can provide a util function to reduce code repeat.


class Name(BaseModel):
    name: str = None

class Description(BaseModel):
    description: str = None

class Base(BaseModel):
    code: int = None
    data: combination({
        items: List[combination(Name, Description)],
        count: int
    }) = None

items = {
    "foo": {
        "code": 0,
        "data": {
            "items": [],
            "count": 0
        }
    }
}
	

@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]

To sum up, I look forward to these features very much.

@tiangolo

Copy link
Copy Markdown
Member Author

@bs32g1038 these additional parameters are mainly for completeness.

The name of the parameters is temporal, as I think it's quite long. But I don't expect users to use them a lot. That's why they are at the end of the function.

But for completeness, it might help some use cases.

About your first use case, you can enable that behavior with one of these parameters (from a previous PR): response_model_skip_defaults=True: https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_skip_defaults-parameter

That's something that can help some use cases, but others might need to keep the default values.

@bs32g1038

bs32g1038 commented May 29, 2019 •

Copy link
Copy Markdown

@tiangolo thanks, I got it!

lmignon pushed a commit to acsone/fastapi that referenced this pull request Sep 19, 2024
Signed-off-by lmignon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants