mirror of
https://github.com/fastapi/fastapi.git
synced 2026-11-23 01:48:43 +08:00
24 lines
413 B
Python
24 lines
413 B
Python
from typing import 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: Person | Company | None = Form(default=None, discriminator="kind"),
|
|
):
|
|
return contact
|