1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
from ninja import NinjaAPI, Router
def test_openapi_info_defined():
"Test appending schema.info"
extra_info = {
"termsOfService": "https://example.com/terms/",
"title": "Test API",
}
api = NinjaAPI(openapi_extra={"info": extra_info}, version="1.0.0")
schema = api.get_openapi_schema()
assert schema["info"]["termsOfService"] == "https://example.com/terms/"
assert schema["info"]["title"] == "Test API"
assert schema["info"]["version"] == "1.0.0"
def test_openapi_no_additional_info():
api = NinjaAPI(title="Test API")
schema = api.get_openapi_schema()
assert schema["info"]["title"] == "Test API"
assert "termsOfService" not in schema["info"]
def test_openapi_extra():
"Test adding extra attribute to the schema"
api = NinjaAPI(
openapi_extra={
"externalDocs": {
"description": "Find more info here",
"url": "https://example.com",
}
},
version="1.0.0",
)
schema = api.get_openapi_schema()
assert schema == {
"openapi": "3.1.0",
"info": {"title": "NinjaAPI", "version": "1.0.0", "description": ""},
"paths": {},
"components": {"schemas": {}},
"servers": [],
"externalDocs": {
"description": "Find more info here",
"url": "https://example.com",
},
}
def test_router_openapi_extra_extends():
"""
Test for #1505.
When adding an extra parameter to a route via openapi_extra, this should be combined with the route's own parameters.
"""
api = NinjaAPI()
test_router = Router()
api.add_router("", test_router)
extra_param = {
"in": "header",
"name": "X-HelloWorld",
"required": False,
"schema": {
"type": "string",
"format": "uuid",
},
}
@test_router.get("/path/{item_id}", openapi_extra={"parameters": [extra_param]})
def get_path_item_id(request, item_id: int):
pass
schema = api.get_openapi_schema()
assert len(schema["paths"]["/api/path/{item_id}"]["get"]["parameters"]) == 2
assert schema["paths"]["/api/path/{item_id}"]["get"]["parameters"] == [
{
"in": "path",
"name": "item_id",
"required": True,
"schema": {
"title": "Item Id",
"type": "integer",
},
},
extra_param,
]
|