from fastapi import FastAPI, status
from pydantic import BaseModel
app = FastAPI()
# 1. Define the input/internal schema
class EmployeeInternal(BaseModel):
id: int
name: str
department: str
base_salary: float
tax_id: str
# 2. Define the public output schema (exclude salary & tax_id)
class EmployeePublic(BaseModel):
id: int
name: str
department: str
# Sample Mock Database
EMPLOYEES = {
1: EmployeeInternal(id=1, name="Alice", department="Engineering", base_salary=8500.0, tax_id="TAX123"),
2: EmployeeInternal(id=2, name="Bob", department="HR", base_salary=6000.0, tax_id="TAX456")
}
# 3. Use response_model to filter data
@app.get("/employees/{employee_id}", response_model=EmployeePublic)
def get_employee(employee_id: int):
# Even though we return EmployeeInternal (with salary & tax_id),
# FastAPI automatically filters the response to match EmployeePublic!
return EMPLOYEES.get(employee_id)