Assignment: Building a Product Inventory API with FastAPI and Pydantic#
Objective: Implement a FastAPI service that demonstrates proficiency in defining routes, handling different parameter types (path and query), enforcing data validation using Pydantic, and managing complex request bodies and responses.
Section 1: Route Definition and Path Parameters (Sources)#
Task 1.1: Basic and Validated Path Parameters (15 Points)
Create a FastAPI application instance.
Define a
GETendpoint for/products/{product_id}.The
product_idmust be captured as a path parameter.It must be automatically converted to an
integer.Implement validation using
Path()to ensure theproduct_idis greater than zero (gt=0) and is required (...).The function should return the product ID in JSON format.
Hint: Refer to the mapping table pattern provided in the sources.
Task 1.2: Capturing Paths (10 Points)
Define a
GETendpoint for/docs/{file_path:path}.Use the
:pathannotation in the URL pattern to ensure thefile_pathparameter captures the entire path, including forward slashes (/).The function should return the captured file path.
Section 2: Query Parameters and Validation#
Task 2.1: Default and Optional Query Parameters (15 Points)
Define a
GETendpoint for/inventory/.Implement two query parameters for pagination, following the basic example pattern:
skip: An integer that defaults to0.limit: An integer that defaults to10.
Implement an optional boolean query parameter named
in_stock.This parameter should default to
False.Demonstrate that FastAPI handles boolean type conversion (e.g., accepts “1”, “true”, or “yes”).
Task 2.2: Required Query Parameters and Advanced Validation (15 Points)
Define a
GETendpoint for/search/.The endpoint must accept a required query parameter named
keyword.Use Pydantic’s
Query()function to enforce string length and pattern validation onkeyword:Minimum length must be
3.Maximum length must be
50.
Add a numerical query parameter
min_ratingand useQuery()to enforce range validation:The rating must be greater than or equal to 0 (
ge=0).
Section 3: Data Modeling, Request Body, and Validation#
Task 3.1: Pydantic Data Model for Request Body (15 Points)
Define a Pydantic
BaseModelnamedSupplier.The
Suppliermodel must contain:name(string).rating(integer, must be greater than 0).is_primary(boolean, defaults toTrue).
Create a
POSTendpoint/suppliers/that accepts theSupplierobject in the JSON request body.Demonstrate that FastAPI automatically validates the request body against the Pydantic model and returns a 422 error if validation fails.
Task 3.2: Nested Models (10 Points)
Create a system for processing bulk orders by defining nested Pydantic models:
Model
OrderItem: fieldsproduct_name,price,quantity.Model
CustomerInfo: fieldsname,email.Model
BulkOrder: must containcustomer(which usesCustomerInfo) and a list of items (items: List[OrderItem]).
Create a
POSTendpoint/orders/that accepts theBulkOrdermodel in the request body.
Section 4: Output Filtering#
Task 4.1: Securing Output with response_model (20 Points)
You are registering new system administrators. You must accept a password but never return it in the response.
Define two Pydantic models:
AdminIn: Includesusernameandpassword.AdminOut: Includes onlyusername.
Create a
POSTendpoint/admin/register.The endpoint should accept the complete
AdminInmodel in the request body.Use the
response_modelparameter in the route decorator to ensure that the function’s return value is filtered to match the structure defined inAdminOut.Prove that even if the function internally returns the full input (including the password), the API response only contains the fields specified by
AdminOut.
Summary of Key Concepts Tested#
This assignment requires students to utilize the following core concepts covered in the sources:
Path vs. Query: Distinguishing parameter locations.
Automatic Type Conversion and Validation: Using Python type hints (e.g.,
int).Advanced Validation: Applying Pydantic functions (
Path(),Query()) for constraints likegt=0,min_length, andge.Required vs. Optional Parameters: Handling parameters with and without default values (
= Noneor no default).Data Modeling: Structuring complex inputs using
BaseModel.Complex Payloads: Handling hierarchical data using Nested Pydantic Models in the request body.
Security/Filtering: Controlling the structure of the API output using the
response_model.
Analogy for Pydantic Models: Think of a Pydantic model for a request body like a pre-flight customs form for data. When data (the traveler) arrives at your API (the border), it must fill out the form (the Pydantic model). If the data doesn’t meet the requirements (wrong field types, missing required information, or values outside acceptable ranges like a password being too short), FastAPI (the customs agent) automatically rejects it with an error (422) before it even reaches your main processing logic.