Home / File/ test_union_body_discriminator_annotated.py — fastapi Source File

test_union_body_discriminator_annotated.py — fastapi Source File

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

File python FastAPI Applications 6 imports 4 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  2085b234_c53e_638c_3dcf_8d81fbaa1f3d["test_union_body_discriminator_annotated.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  2085b234_c53e_638c_3dcf_8d81fbaa1f3d --> 0dda2280_3359_8460_301c_e98c77e78185
  5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"]
  2085b234_c53e_638c_3dcf_8d81fbaa1f3d --> 5befe8bf_65d1_d058_6b78_4a597a8488e9
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  2085b234_c53e_638c_3dcf_8d81fbaa1f3d --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  2085b234_c53e_638c_3dcf_8d81fbaa1f3d --> a7c04dee_ee23_5891_b185_47ff6bed036d
  a7f4e7b0_9725_db90_5cbc_7ca8211b323a["inline_snapshot"]
  2085b234_c53e_638c_3dcf_8d81fbaa1f3d --> a7f4e7b0_9725_db90_5cbc_7ca8211b323a
  6913fbd4_39df_d14b_44bb_522e99b65b90["pydantic"]
  2085b234_c53e_638c_3dcf_8d81fbaa1f3d --> 6913fbd4_39df_d14b_44bb_522e99b65b90
  style 2085b234_c53e_638c_3dcf_8d81fbaa1f3d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

# Ref: https://github.com/fastapi/fastapi/discussions/14495

from typing import Annotated, Union

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


@pytest.fixture(name="client")
def client_fixture() -> TestClient:
    from fastapi import Body
    from pydantic import Discriminator, Tag

    class Cat(BaseModel):
        pet_type: str = "cat"
        meows: int

    class Dog(BaseModel):
        pet_type: str = "dog"
        barks: float

    def get_pet_type(v):
        assert isinstance(v, dict)
        return v.get("pet_type", "")

    Pet = Annotated[
        Union[Annotated[Cat, Tag("cat")], Annotated[Dog, Tag("dog")]],
        Discriminator(get_pet_type),
    ]

    app = FastAPI()

    @app.post("/pet/assignment")
    async def create_pet_assignment(pet: Pet = Body()):
        return pet

    @app.post("/pet/annotated")
    async def create_pet_annotated(pet: Annotated[Pet, Body()]):
        return pet

    client = TestClient(app)
    return client


def test_union_body_discriminator_assignment(client: TestClient) -> None:
    response = client.post("/pet/assignment", json={"pet_type": "cat", "meows": 5})
    assert response.status_code == 200, response.text
    assert response.json() == {"pet_type": "cat", "meows": 5}


def test_union_body_discriminator_annotated(client: TestClient) -> None:
    response = client.post("/pet/annotated", json={"pet_type": "dog", "barks": 3.5})
    assert response.status_code == 200, response.text
    assert response.json() == {"pet_type": "dog", "barks": 3.5}


def test_openapi_schema(client: TestClient) -> None:
// ... (144 more lines)

Domain

Subdomains

Classes

Dependencies

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free