Bird
Raised Fist0
FastAPIframework~20 mins

OpenAPI schema customization in FastAPI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
OpenAPI Schema Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the effect of setting openapi_url=None in FastAPI?
Consider this FastAPI app initialization:
app = FastAPI(openapi_url=None)

What will happen when you try to access /openapi.json?
FastAPI
from fastapi import FastAPI
app = FastAPI(openapi_url=None)
AThe app will raise an error on startup because openapi_url cannot be None.
BThe OpenAPI JSON schema will be available at /openapi.json as usual.
CThe OpenAPI JSON schema will be available but at a different URL automatically.
DThe OpenAPI JSON schema will not be available; accessing /openapi.json returns 404.
Attempts:
2 left
💡 Hint
Think about what disabling the OpenAPI schema URL does to the app's documentation endpoints.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly adds a custom OpenAPI schema title in FastAPI?
You want to customize the OpenAPI schema title to "My API". Which snippet does this correctly?
Aapp = FastAPI(title="My API")
Bapp = FastAPI(openapi_title="My API")
Capp = FastAPI(openapi_schema_title="My API")
Dapp = FastAPI(schema_title="My API")
Attempts:
2 left
💡 Hint
Check the FastAPI constructor parameters for setting the API title.
🔧 Debug
advanced
3:00remaining
Why does this custom OpenAPI schema function cause a recursion error?
Given this code snippet:
def custom_openapi():
    return app.openapi()

app.openapi = custom_openapi

What causes the recursion error when calling app.openapi()?
FastAPI
def custom_openapi():
    return app.openapi()

app.openapi = custom_openapi
AThe app.openapi attribute is not callable, causing a TypeError.
BThe custom_openapi calls itself recursively without a base case.
CThe function is missing a return statement.
DThe app object is not defined in the function scope.
Attempts:
2 left
💡 Hint
Think about what happens when custom_openapi calls app.openapi after reassigning it.
🧠 Conceptual
advanced
3:00remaining
How can you add a custom field to the OpenAPI schema info section in FastAPI?
You want to add a custom field x-logo with a URL inside the OpenAPI info section. Which approach is correct?
AAdd a route that returns the custom OpenAPI JSON with the extra field.
BPass x-logo as a parameter to FastAPI constructor.
COverride the app.openapi() method to modify the schema dictionary before returning it.
DUse a middleware to inject the x-logo field into the response headers.
Attempts:
2 left
💡 Hint
Think about how to customize the OpenAPI JSON output directly.
❓ state_output
expert
3:00remaining
What is the output of this FastAPI OpenAPI customization code?
Given this code snippet:
from fastapi import FastAPI

app = FastAPI()

original_openapi = app.openapi

def custom_openapi():
    if not hasattr(app, "_custom_openapi_schema"):
        schema = original_openapi()
        schema["info"]["title"] = "Custom API"
        app._custom_openapi_schema = schema
    return app._custom_openapi_schema

app.openapi = custom_openapi

print(app.openapi()["info"]["title"])

What will be printed?
FastAPI
from fastapi import FastAPI

app = FastAPI()

original_openapi = app.openapi

def custom_openapi():
    if not hasattr(app, "_custom_openapi_schema"):
        schema = original_openapi()
        schema["info"]["title"] = "Custom API"
        app._custom_openapi_schema = schema
    return app._custom_openapi_schema

app.openapi = custom_openapi

print(app.openapi()["info"]["title"])
ACustom API
BFastAPI
CNone
DRaises AttributeError
Attempts:
2 left
💡 Hint
Look at how the title is changed and cached in the custom_openapi function.

Practice

(1/5)
1. What is the main purpose of customizing the OpenAPI schema in a FastAPI application?
easy
A. To provide clear API documentation with custom titles, descriptions, and versions
B. To speed up the API response time
C. To change the database connection settings
D. To automatically generate frontend UI components

Solution

  1. Step 1: Understand OpenAPI schema role

    The OpenAPI schema defines how the API is documented and described for users and tools.
  2. Step 2: Identify customization benefits

    Customizing it lets you add titles, descriptions, versions, and contact info to make docs clearer.
  3. Final Answer:

    To provide clear API documentation with custom titles, descriptions, and versions -> Option A
  4. Quick Check:

    OpenAPI schema customization = clear docs [OK]
Hint: Think: OpenAPI schema = API documentation details [OK]
Common Mistakes:
  • Confusing schema customization with performance tuning
  • Thinking it changes database or backend logic
  • Assuming it auto-generates frontend code
2. Which of the following is the correct way to set a custom title and version for your FastAPI app's OpenAPI schema?
easy
A. app = FastAPI(schema_title="My API", schema_version="1.0.0")
B. app = FastAPI(openapi_title="My API", openapi_version="1.0.0")
C. app = FastAPI(title="My API", version="1.0.0")
D. app = FastAPI(api_title="My API", api_version="1.0.0")

Solution

  1. Step 1: Recall FastAPI app parameters

    FastAPI accepts title and version as direct parameters in its constructor.
  2. Step 2: Match correct parameter names

    The correct parameters are exactly title and version, not schema_title or openapi_title.
  3. Final Answer:

    app = FastAPI(title="My API", version="1.0.0") -> Option C
  4. Quick Check:

    FastAPI(title, version) = correct syntax [OK]
Hint: Use 'title' and 'version' directly in FastAPI() [OK]
Common Mistakes:
  • Using incorrect parameter names like openapi_title
  • Trying to set schema properties after app creation
  • Confusing app config with route decorators
3. Given this FastAPI app code, what will be the OpenAPI docs URL by default?
from fastapi import FastAPI
app = FastAPI(title="Test API")

@app.get("/items/")
async def read_items():
    return [{"item_id": "foo"}]
medium
A. /api-docs
B. /openapi.json
C. /swagger
D. /docs

Solution

  1. Step 1: Recall FastAPI default docs path

    FastAPI by default serves Swagger UI at the path /docs.
  2. Step 2: Differentiate docs and schema URLs

    /openapi.json is the raw schema JSON, not the interactive docs UI.
  3. Final Answer:

    /docs -> Option D
  4. Quick Check:

    Default docs URL = /docs [OK]
Hint: Swagger UI docs default path is /docs in FastAPI [OK]
Common Mistakes:
  • Confusing /openapi.json with docs UI
  • Assuming /swagger or /api-docs are default
  • Not knowing FastAPI defaults for docs
4. What is wrong with this FastAPI app code if the goal is to hide the automatic docs UI?
app = FastAPI(docs_url=None)

@app.get("/users")
async def get_users():
    return [{"name": "Alice"}]
medium
A. docs_url should be set to False, not None
B. You must also set redoc_url=None to hide all docs
C. docs_url parameter does not exist in FastAPI
D. docs_url=None is correct and will hide docs UI

Solution

  1. Step 1: Understand docs_url and redoc_url

    FastAPI has two docs UIs: Swagger UI at docs_url and ReDoc at redoc_url.
  2. Step 2: Check hiding docs UI fully

    Setting docs_url=None hides Swagger UI but ReDoc remains unless redoc_url=None is also set.
  3. Final Answer:

    You must also set redoc_url=None to hide all docs -> Option B
  4. Quick Check:

    Hide all docs = docs_url=None + redoc_url=None [OK]
Hint: Set both docs_url and redoc_url to None to hide docs [OK]
Common Mistakes:
  • Assuming docs_url=None hides all docs
  • Using invalid values like False for docs_url
  • Not knowing about ReDoc docs URL
5. You want to customize your FastAPI app's OpenAPI schema to include a contact email and license info. Which code snippet correctly achieves this?
hard
A. app = FastAPI(contact={"email": "support@example.com"}, license_info={"name": "MIT"})
B. app = FastAPI(openapi_info={"contact": {"email": "support@example.com"}, "license": {"name": "MIT"}})
C. app = FastAPI(openapi_contact={"email": "support@example.com"}, openapi_license={"name": "MIT"})
D. app = FastAPI(openapi_contact_email="support@example.com", openapi_license_name="MIT")

Solution

  1. Step 1: Recall FastAPI parameters for contact and license

    FastAPI uses contact and license_info parameters to customize OpenAPI schema contact and license sections.
  2. Step 2: Match correct parameter names and structure

    contact and license_info expect dictionaries with keys like email and name respectively.
  3. Final Answer:

    app = FastAPI(contact={"email": "support@example.com"}, license_info={"name": "MIT"}) -> Option A
  4. Quick Check:

    Use contact and license_info dicts in FastAPI() [OK]
Hint: Use contact and license_info dicts in FastAPI() constructor [OK]
Common Mistakes:
  • Using openapi_contact or openapi_license which are invalid
  • Trying to nest contact inside openapi_info
  • Passing strings instead of dicts for contact/license
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard