Home / File/ test_union_inherited_body.py — fastapi Source File

test_union_inherited_body.py — fastapi Source File

Architecture documentation for test_union_inherited_body.py, a python file in the fastapi codebase. 5 imports, 0 dependents.

File python FastAPI Routing 5 imports 4 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  3eabdbc0_2eb6_83c7_313a_f061f310f0ab["test_union_inherited_body.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  3eabdbc0_2eb6_83c7_313a_f061f310f0ab --> 0dda2280_3359_8460_301c_e98c77e78185
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  3eabdbc0_2eb6_83c7_313a_f061f310f0ab --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  3eabdbc0_2eb6_83c7_313a_f061f310f0ab --> a7c04dee_ee23_5891_b185_47ff6bed036d
  a7f4e7b0_9725_db90_5cbc_7ca8211b323a["inline_snapshot"]
  3eabdbc0_2eb6_83c7_313a_f061f310f0ab --> a7f4e7b0_9725_db90_5cbc_7ca8211b323a
  6913fbd4_39df_d14b_44bb_522e99b65b90["pydantic"]
  3eabdbc0_2eb6_83c7_313a_f061f310f0ab --> 6913fbd4_39df_d14b_44bb_522e99b65b90
  style 3eabdbc0_2eb6_83c7_313a_f061f310f0ab fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Optional, Union

from fastapi import FastAPI
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: Optional[str] = None


class ExtendedItem(Item):
    age: int


@app.post("/items/")
def save_union_different_body(item: Union[ExtendedItem, Item]):
    return {"item": item}


client = TestClient(app)


def test_post_extended_item():
    response = client.post("/items/", json={"name": "Foo", "age": 5})
    assert response.status_code == 200, response.text
    assert response.json() == {"item": {"name": "Foo", "age": 5}}


def test_post_item():
    response = client.post("/items/", json={"name": "Foo"})
    assert response.status_code == 200, response.text
    assert response.json() == {"item": {"name": "Foo"}}


def test_openapi_schema():
    response = client.get("/openapi.json")
    assert response.status_code == 200, response.text
    assert response.json() == snapshot(
        {
            "openapi": "3.1.0",
            "info": {"title": "FastAPI", "version": "0.1.0"},
            "paths": {
                "/items/": {
                    "post": {
                        "responses": {
                            "200": {
                                "description": "Successful Response",
                                "content": {"application/json": {"schema": {}}},
                            },
                            "422": {
                                "description": "Validation Error",
                                "content": {
                                    "application/json": {
                                        "schema": {
                                            "$ref": "#/components/schemas/HTTPValidationError"
                                        }
// ... (85 more lines)

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does test_union_inherited_body.py do?
test_union_inherited_body.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Routing subdomain.
What functions are defined in test_union_inherited_body.py?
test_union_inherited_body.py defines 4 function(s): save_union_different_body, test_openapi_schema, test_post_extended_item, test_post_item.
What does test_union_inherited_body.py depend on?
test_union_inherited_body.py imports 5 module(s): __init__.py, inline_snapshot, pydantic, testclient.py, typing.
Where is test_union_inherited_body.py in the architecture?
test_union_inherited_body.py is located at tests/test_union_inherited_body.py (domain: FastAPI, subdomain: Routing, directory: tests).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free