mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-28 16:49:26 -05:00
* ✨ Update OpenAPI models for JSON Schema 2020-12 and OpenAPI 3.1.0 * ✨ Add support for summary and webhooks * ✨ Update JSON Schema for UploadFiles * ⏪️ Revert making paths optional, to ensure always correctness * ⏪️ Keep UploadFile as format: binary for compatibility with the rest of Pydantic bytes fields in v1 * ✨ Update version of OpenAPI generated to 3.1.0 * ✨ Update the version of Swagger UI * 📝 Update docs about extending OpenAPI * 📝 Update docs and links to refer to OpenAPI 3.1.0 * ✨ Update logic for handling webhooks * ♻️ Update parameter functions and classes, deprecate example and make examples the main field * ✅ Update tests for OpenAPI 3.1.0 * 📝 Update examples for OpenAPI metadata * ✅ Add and update tests for OpenAPI metadata * 📝 Add source example for webhooks * 📝 Update docs for metadata * 📝 Update docs for Schema extra * 📝 Add docs for webhooks * 🔧 Add webhooks docs to MkDocs * ✅ Update tests for extending OpenAPI * ✅ Add tests for webhooks * ♻️ Refactor generation of OpenAPI and JSON Schema with params * 📝 Update source examples for field examples * ✅ Update tests for examples * ➕ Make sure the minimum version of typing-extensions installed has deprecated() (already a dependency of Pydantic) * ✏️ Fix typo in Webhooks example code * 🔥 Remove commented out code of removed nullable field * 🗑️ Add deprecation warnings for example argument * ✅ Update tests to check for deprecation warnings * ✅ Add test for webhooks with security schemes, for coverage * 🍱 Update image for metadata, with new summary * 🍱 Add docs image for Webhooks * 📝 Update docs for webhooks, add docs UI image
119 lines
3.1 KiB
Python
119 lines
3.1 KiB
Python
import pytest
|
|
from fastapi import APIRouter, FastAPI
|
|
from fastapi.routing import APIRoute
|
|
from fastapi.testclient import TestClient
|
|
from starlette.routing import Route
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class APIRouteA(APIRoute):
|
|
x_type = "A"
|
|
|
|
|
|
class APIRouteB(APIRoute):
|
|
x_type = "B"
|
|
|
|
|
|
class APIRouteC(APIRoute):
|
|
x_type = "C"
|
|
|
|
|
|
router_a = APIRouter(route_class=APIRouteA)
|
|
router_b = APIRouter(route_class=APIRouteB)
|
|
router_c = APIRouter(route_class=APIRouteC)
|
|
|
|
|
|
@router_a.get("/")
|
|
def get_a():
|
|
return {"msg": "A"}
|
|
|
|
|
|
@router_b.get("/")
|
|
def get_b():
|
|
return {"msg": "B"}
|
|
|
|
|
|
@router_c.get("/")
|
|
def get_c():
|
|
return {"msg": "C"}
|
|
|
|
|
|
router_b.include_router(router=router_c, prefix="/c")
|
|
router_a.include_router(router=router_b, prefix="/b")
|
|
app.include_router(router=router_a, prefix="/a")
|
|
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"path,expected_status,expected_response",
|
|
[
|
|
("/a", 200, {"msg": "A"}),
|
|
("/a/b", 200, {"msg": "B"}),
|
|
("/a/b/c", 200, {"msg": "C"}),
|
|
],
|
|
)
|
|
def test_get_path(path, expected_status, expected_response):
|
|
response = client.get(path)
|
|
assert response.status_code == expected_status
|
|
assert response.json() == expected_response
|
|
|
|
|
|
def test_route_classes():
|
|
routes = {}
|
|
for r in app.router.routes:
|
|
assert isinstance(r, Route)
|
|
routes[r.path] = r
|
|
assert getattr(routes["/a/"], "x_type") == "A" # noqa: B009
|
|
assert getattr(routes["/a/b/"], "x_type") == "B" # noqa: B009
|
|
assert getattr(routes["/a/b/c/"], "x_type") == "C" # noqa: B009
|
|
|
|
|
|
def test_openapi_schema():
|
|
response = client.get("/openapi.json")
|
|
assert response.status_code == 200, response.text
|
|
assert response.json() == {
|
|
"openapi": "3.1.0",
|
|
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
"paths": {
|
|
"/a/": {
|
|
"get": {
|
|
"responses": {
|
|
"200": {
|
|
"description": "Successful Response",
|
|
"content": {"application/json": {"schema": {}}},
|
|
}
|
|
},
|
|
"summary": "Get A",
|
|
"operationId": "get_a_a__get",
|
|
}
|
|
},
|
|
"/a/b/": {
|
|
"get": {
|
|
"responses": {
|
|
"200": {
|
|
"description": "Successful Response",
|
|
"content": {"application/json": {"schema": {}}},
|
|
}
|
|
},
|
|
"summary": "Get B",
|
|
"operationId": "get_b_a_b__get",
|
|
}
|
|
},
|
|
"/a/b/c/": {
|
|
"get": {
|
|
"responses": {
|
|
"200": {
|
|
"description": "Successful Response",
|
|
"content": {"application/json": {"schema": {}}},
|
|
}
|
|
},
|
|
"summary": "Get C",
|
|
"operationId": "get_c_a_b_c__get",
|
|
}
|
|
},
|
|
},
|
|
}
|