0
0
FastAPIframework~20 mins

Example data in schema in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Example Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this FastAPI schema example?
Given this Pydantic model with example data, what will the OpenAPI schema show for the example?
FastAPI
from pydantic import BaseModel, Field

class User(BaseModel):
    id: int = Field(..., example=123)
    name: str = Field(..., example="Alice")

user_example = User.schema()['properties']
A{"id": {"title": "Id", "type": "integer", "example": 123}, "name": {"title": "Name", "type": "string", "example": "Alice"}}
B{"id": {"title": "Id", "type": "string", "example": "123"}, "name": {"title": "Name", "type": "string", "example": "Alice"}}
C{"id": {"title": "Id", "type": "integer"}, "name": {"title": "Name", "type": "string"}}
D{"id": {"title": "Id", "type": "integer", "example": "Alice"}, "name": {"title": "Name", "type": "string", "example": 123}}
Attempts:
2 left
💡 Hint
Look at the Field example values and their types.
📝 Syntax
intermediate
2:00remaining
Which option correctly sets example data in a Pydantic schema?
Choose the code snippet that correctly uses Field to add example data for a Pydantic model.
A
class Item(BaseModel):
    price: float = Field(..., example=19.99)
B
class Item(BaseModel):
    price: float = Field(example=19.99)
C
class Item(BaseModel):
    price: float = Field(..., example='19.99')
D
class Item(BaseModel):
    price: float = Field(..., example=price)
Attempts:
2 left
💡 Hint
Example should match the field type and use ... for required fields.
🔧 Debug
advanced
2:00remaining
What error occurs with this example data in schema?
This Pydantic model has example data that does not match the field type. What error will FastAPI raise when generating docs?
FastAPI
from pydantic import BaseModel, Field

class Product(BaseModel):
    quantity: int = Field(..., example="ten")
ANo error, example is accepted as string
BSyntaxError: invalid syntax in Field example
CValidationError: example value is not valid for type int
DTypeError: example must be int, got str
Attempts:
2 left
💡 Hint
Example must match the declared field type.
state_output
advanced
2:00remaining
What is the example output in OpenAPI for nested Pydantic models?
Given nested models with example data, what does the OpenAPI example show for the nested field?
FastAPI
from pydantic import BaseModel, Field

class Address(BaseModel):
    city: str = Field(..., example="Paris")

class User(BaseModel):
    name: str = Field(..., example="Bob")
    address: Address = Field(...)

user_schema = User.schema()
A{"name": {"example": "Bob"}, "address": {}}
B{"name": {"example": "Bob"}, "address": {"$ref": "#/definitions/Address"}}
C{"name": {"example": "Bob"}, "address": {"example": "Paris"}}
D{"name": {"example": "Bob"}, "address": {"example": {"city": "Paris"}}}
Attempts:
2 left
💡 Hint
Nested models use $ref in schema, examples are inside referenced model.
🧠 Conceptual
expert
2:00remaining
How does FastAPI use example data in schema for API docs?
Why is setting example data in Pydantic models important for FastAPI-generated OpenAPI docs?
AIt enforces runtime validation of incoming data to match the example values exactly.
BIt automatically generates database seed data from the examples.
CIt disables schema validation and trusts example data as the only valid input.
DIt provides sample request and response data shown in the API docs to help users understand the API.
Attempts:
2 left
💡 Hint
Think about the purpose of examples in documentation.