mirror of https://github.com/tiangolo/fastapi.git
✨ Add OpenAPI `external_docs` parameter to `FastAPI` (#13713)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
This commit is contained in:
parent
14168b43eb
commit
bc5013cd56
|
|
@ -810,6 +810,32 @@ class FastAPI(Starlette):
|
||||||
"""
|
"""
|
||||||
),
|
),
|
||||||
] = True,
|
] = True,
|
||||||
|
openapi_external_docs: Annotated[
|
||||||
|
Optional[Dict[str, Any]],
|
||||||
|
Doc(
|
||||||
|
"""
|
||||||
|
This field allows you to provide additional external documentation links.
|
||||||
|
If provided, it must be a dictionary containing:
|
||||||
|
|
||||||
|
* `description`: A brief description of the external documentation.
|
||||||
|
* `url`: The URL pointing to the external documentation. The value **MUST**
|
||||||
|
be a valid URL format.
|
||||||
|
|
||||||
|
**Example**:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
external_docs = {
|
||||||
|
"description": "Detailed API Reference",
|
||||||
|
"url": "https://example.com/api-docs",
|
||||||
|
}
|
||||||
|
|
||||||
|
app = FastAPI(openapi_external_docs=external_docs)
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
] = None,
|
||||||
**extra: Annotated[
|
**extra: Annotated[
|
||||||
Any,
|
Any,
|
||||||
Doc(
|
Doc(
|
||||||
|
|
@ -838,6 +864,7 @@ class FastAPI(Starlette):
|
||||||
self.swagger_ui_parameters = swagger_ui_parameters
|
self.swagger_ui_parameters = swagger_ui_parameters
|
||||||
self.servers = servers or []
|
self.servers = servers or []
|
||||||
self.separate_input_output_schemas = separate_input_output_schemas
|
self.separate_input_output_schemas = separate_input_output_schemas
|
||||||
|
self.openapi_external_docs = openapi_external_docs
|
||||||
self.extra = extra
|
self.extra = extra
|
||||||
self.openapi_version: Annotated[
|
self.openapi_version: Annotated[
|
||||||
str,
|
str,
|
||||||
|
|
@ -992,6 +1019,7 @@ class FastAPI(Starlette):
|
||||||
tags=self.openapi_tags,
|
tags=self.openapi_tags,
|
||||||
servers=self.servers,
|
servers=self.servers,
|
||||||
separate_input_output_schemas=self.separate_input_output_schemas,
|
separate_input_output_schemas=self.separate_input_output_schemas,
|
||||||
|
external_docs=self.openapi_external_docs,
|
||||||
)
|
)
|
||||||
return self.openapi_schema
|
return self.openapi_schema
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -488,6 +488,7 @@ def get_openapi(
|
||||||
contact: Optional[Dict[str, Union[str, Any]]] = None,
|
contact: Optional[Dict[str, Union[str, Any]]] = None,
|
||||||
license_info: Optional[Dict[str, Union[str, Any]]] = None,
|
license_info: Optional[Dict[str, Union[str, Any]]] = None,
|
||||||
separate_input_output_schemas: bool = True,
|
separate_input_output_schemas: bool = True,
|
||||||
|
external_docs: Optional[Dict[str, Any]] = None,
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
info: Dict[str, Any] = {"title": title, "version": version}
|
info: Dict[str, Any] = {"title": title, "version": version}
|
||||||
if summary:
|
if summary:
|
||||||
|
|
@ -565,4 +566,6 @@ def get_openapi(
|
||||||
output["webhooks"] = webhook_paths
|
output["webhooks"] = webhook_paths
|
||||||
if tags:
|
if tags:
|
||||||
output["tags"] = tags
|
output["tags"] = tags
|
||||||
|
if external_docs:
|
||||||
|
output["externalDocs"] = external_docs
|
||||||
return jsonable_encoder(OpenAPI(**output), by_alias=True, exclude_none=True) # type: ignore
|
return jsonable_encoder(OpenAPI(**output), by_alias=True, exclude_none=True) # type: ignore
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,12 @@ from typing import FrozenSet, List, Optional
|
||||||
|
|
||||||
from fastapi import FastAPI, Path, Query
|
from fastapi import FastAPI, Path, Query
|
||||||
|
|
||||||
app = FastAPI()
|
external_docs = {
|
||||||
|
"description": "External API documentation.",
|
||||||
|
"url": "https://docs.example.com/api-general",
|
||||||
|
}
|
||||||
|
|
||||||
|
app = FastAPI(openapi_external_docs=external_docs)
|
||||||
|
|
||||||
|
|
||||||
@app.api_route("/api_route")
|
@app.api_route("/api_route")
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,10 @@ def test_openapi_schema():
|
||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
"openapi": "3.1.0",
|
"openapi": "3.1.0",
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||||
|
"externalDocs": {
|
||||||
|
"description": "External API documentation.",
|
||||||
|
"url": "https://docs.example.com/api-general",
|
||||||
|
},
|
||||||
"paths": {
|
"paths": {
|
||||||
"/api_route": {
|
"/api_route": {
|
||||||
"get": {
|
"get": {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue