route_response_media_type: Optional[str] = current_response_class.media_type
if route.include_in_schema:
for method in route.methods:
+ if method == "HEAD" and "GET" in route.methods:
+ continue
operation = get_openapi_operation_metadata(
route=route, method=method, operation_ids=operation_ids
)
self.name = get_name(endpoint) if name is None else name
self.path_regex, self.path_format, self.param_convertors = compile_path(path)
if methods is None:
- methods = ["GET"]
+ methods = ["GET", "HEAD"]
+ elif "GET" in methods:
+ methods = set(methods) | {"HEAD"}
self.methods: Set[str] = {method.upper() for method in methods}
if isinstance(generate_unique_id_function, DefaultPlaceholder):
current_generate_unique_id: Callable[[APIRoute], str] = (
response_description=response_description,
responses=responses,
deprecated=deprecated,
- methods=["GET"],
+ methods=["GET", "HEAD"],
operation_id=operation_id,
response_model_include=response_model_include,
response_model_exclude=response_model_exclude,
price: Optional[float] = None
+@app.head("/items/{item_id}")
+def head_item(item_id: str):
+ return JSONResponse(None, headers={"x-fastapi-item-id": item_id})
+
+
@app.api_route("/items/{item_id}", methods=["GET"])
def get_items(item_id: str):
return {"item_id": item_id}
return {"item_id": item_id, "item": item}
-@app.head("/items/{item_id}")
-def head_item(item_id: str):
- return JSONResponse(None, headers={"x-fastapi-item-id": item_id})
-
-
@app.options("/items/{item_id}")
def options_item(item_id: str):
return JSONResponse(None, headers={"x-fastapi-item-id": item_id})
("/nonexistent", 404, {"detail": "Not Found"}),
],
)
-def test_get_path(path, expected_status, expected_response):
+def test_get_path(path: str, expected_status: int, expected_response: dict[str, str]):
response = client.get(path)
assert response.status_code == expected_status
assert response.json() == expected_response
+ response = client.head(path)
+ assert response.status_code == expected_status
+
def test_openapi_schema():
response = client.get("/openapi.json")