tezvyn:

Add summary and description to a FastAPI endpoint for Swagger UI

AI-drafted, machine-checkedSource: fastapi.tiangolo.combeginner
Add summary and description to a FastAPI endpoint for Swagger UI

This tests FastAPI path operation decorator configuration. Pass summary and description to @app.get, or use function docstring for description. A red flag is setting metadata inside the function body or confusing docs with Pydantic Field descriptions.

WHAT THIS TESTS: This question checks whether you understand FastAPI path operation configuration and the distinction between decorator arguments and function implementation details. The interviewer wants to see that you know how OpenAPI metadata is generated and that you are familiar with the developer experience features FastAPI provides out of the box. Specifically they care if you know that Swagger UI documentation for an endpoint is controlled at the decorator level rather than inside the function body.

A GOOD ANSWER COVERS: A strong response should mention that you pass summary and description directly to the path operation decorator such as app.get or app.post as keyword arguments. You should note that summary produces the short human readable headline in Swagger UI while description provides the longer explanatory text. You should also mention the docstring shortcut: if you write a docstring under the path operation function FastAPI automatically uses it as the description field in the OpenAPI schema. A senior candidate might briefly note that response_description is a separate parameter for documenting the response itself and that tags help group endpoints in the UI.

COMMON WRONG ANSWERS: Red flags include trying to return documentation strings from the function attempting to set these fields inside the function body or confusing endpoint level metadata with Pydantic model Field descriptions. Another mistake is believing you must manually construct the OpenAPI schema or edit JSON to change Swagger UI text. Some candidates incorrectly suggest using Pydantic models or response classes to hold summary text. These errors reveal a misunderstanding of where FastAPI looks for endpoint metadata.

LIKELY FOLLOW-UPS: The interviewer might ask how to add tags to organize endpoints in Swagger UI. They might ask how to mark an endpoint as deprecated. They could also ask what happens when both a docstring and a description parameter are provided or how to customize the response description separately from the main endpoint description. A deeper follow up might explore how to add additional OpenAPI metadata such as operation_id or how to hide an endpoint from the docs.

ONE CONCRETE EXAMPLE: Imagine an endpoint that retrieves a user profile. You could write at app.get slash users slash user_id summary equals Get user profile description equals Retrieve detailed information about a registered user including preferences and activity history. Alternatively you could omit the description argument and instead place a triple quoted docstring under the function that says the same thing. When you open Swagger UI the endpoint will show Get user profile as the bold title and the longer text underneath. If you also set tags equals users the endpoint appears under the users section in the generated documentation.

Source: fastapi.tiangolo.com

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.