Browse Source

Merge 72dd549f71 into 6df50d40fe

pull/13713/merge
Carlos Mario Toro 4 days ago
committed by GitHub
parent
commit
35b5b889b5
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 28
      fastapi/applications.py
  2. 3
      fastapi/openapi/utils.py
  3. 7
      tests/main.py
  4. 4
      tests/test_application.py

28
fastapi/applications.py

@ -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

3
fastapi/openapi/utils.py

@ -489,6 +489,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:
@ -566,4 +567,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

7
tests/main.py

@ -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")

4
tests/test_application.py

@ -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…
Cancel
Save