mirror of
https://github.com/fastapi/fastapi.git
synced 2026-11-21 10:13:53 +08:00
24 lines
427 B
Python
24 lines
427 B
Python
from typing import Annotated, Literal
|
|
|
|
from fastapi import FastAPI, Form
|
|
from pydantic import BaseModel
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class Person(BaseModel):
|
|
kind: Literal["person"]
|
|
name: str
|
|
|
|
|
|
class Company(BaseModel):
|
|
kind: Literal["company"]
|
|
company_name: str
|
|
|
|
|
|
@app.post("/contacts/")
|
|
async def create_contact(
|
|
contact: Annotated[Person | Company | None, Form(discriminator="kind")] = None,
|
|
):
|
|
return contact
|