Skip to content
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

✨ Support dataclasses in responses #3576

Merged
merged 5 commits into from
Jul 21, 2021
Merged

Conversation

tiangolo
Copy link
Member

✨ Support dataclasses in responses, e.g.:

from typing import List, Optional

from fastapi import FastAPI

from pydantic.dataclasses import dataclass

app = FastAPI()


@dataclass
class Item:
    name: str
    price: Optional[float] = None
    owner_ids: Optional[List[int]] = None


@app.get("/items/foo")
def get_item():
    return Item(name="foo", price=1.0, owner_ids=[1, 2, 3])

This is a continuation of #2722. I do it in a new branch because I'm not allowed to push commits to the branch in the original PR.

I added extra tests covering other cases, when not using response_model, and added/fixed support for them.

@tiangolo tiangolo enabled auto-merge (squash) July 21, 2021 14:38
@codecov
Copy link

codecov bot commented Jul 21, 2021

Codecov Report

Merging #3576 (7ccad39) into master (fa7e3c9) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##            master     #3576    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          246       248     +2     
  Lines         7591      7746   +155     
==========================================
+ Hits          7591      7746   +155     
Impacted Files Coverage Δ
fastapi/encoders.py 100.00% <100.00%> (ø)
fastapi/routing.py 100.00% <100.00%> (ø)
tests/test_serialize_response_dataclass.py 100.00% <100.00%> (ø)
tests/main.py 100.00% <0.00%> (ø)
fastapi/utils.py 100.00% <0.00%> (ø)

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 4d91f97...7ccad39. Read the comment docs.

@github-actions
Copy link
Contributor

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.

1 participant