0
0
FastAPIframework~20 mins

OpenAPI schema customization in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 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.