Note: This is a brief, AI-generated summary based only on the available title information. Readers are encouraged to consult the original source for complete and verified details.
Due to technical issues, we were unable to fetch the full article from the provided source URL. However, we've prepared a brief summary of the topic for your convenience. Please note that the details below are based on the title and may not be entirely accurate without further verification. We strongly encourage you to visit the original source for a comprehensive understanding of the subject.
What is 'include_in_schema' in FastAPI?
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. In this analysis, we focus on the 'include_in_schema' feature in FastAPI.
Implications of 'include_in_schema'
- 'include_in_schema' is a decorator in FastAPI that allows developers to customize the schema of a response by specifying which fields should be included in the JSON response.
- By default, FastAPI includes all public attributes of a Pydantic model in the response schema. However, 'include_in_schema' can be used to exclude certain fields or include additional ones.
- This feature is particularly useful when dealing with sensitive data, as it enables developers to limit the amount of information exposed in the API response.
Example Usage
from fastapi import FastAPI, APIResponse app = FastAPI() class Item(BaseModel): name: str description: str price: float # ... additional fields @app.get("/items/{item_id}") async def read_item(item_id: int, response: APIResponse): item = {"name": "Foo", "description": "A very nice item", "price": 100.0, "hidden_field": "secret"} response.headers["X-Custom-Header"] = "Custom Value" # Include only 'name' and 'price' in the response schema return response(Item(name=item["name"], price=item["price"]), headers=response.headers, include_in_schema=False) The above example demonstrates the usage of 'include_in_schema' to customize the response schema by including only the 'name' and 'price' fields of the Item model. Additionally, it sets a custom header in the response.
Once again, we encourage you to check the original source for a more detailed explanation and examples of using 'include_in_schema' in FastAPI.