18
18
19
19
from aws_lambda_powertools .event_handler import content_types
20
20
from aws_lambda_powertools .event_handler .exceptions import NotFoundError , ServiceError
21
+ from aws_lambda_powertools .event_handler .openapi .config import OpenAPIConfig
21
22
from aws_lambda_powertools .event_handler .openapi .constants import DEFAULT_API_VERSION , DEFAULT_OPENAPI_VERSION
22
23
from aws_lambda_powertools .event_handler .openapi .exceptions import RequestValidationError , SchemaValidationError
23
24
from aws_lambda_powertools .event_handler .openapi .types import (
@@ -1530,6 +1531,7 @@ def __init__(
1530
1531
self .context : dict = {} # early init as customers might add context before event resolution
1531
1532
self .processed_stack_frames = []
1532
1533
self ._response_builder_class = ResponseBuilder [BaseProxyEvent ]
1534
+ self .openapi_config = OpenAPIConfig () # starting an empty dataclass
1533
1535
1534
1536
# Allow for a custom serializer or a concise json serialization
1535
1537
self ._serializer = serializer or partial (json .dumps , separators = ("," , ":" ), cls = Encoder )
@@ -1544,7 +1546,7 @@ def __init__(
1544
1546
def get_openapi_schema (
1545
1547
self ,
1546
1548
* ,
1547
- title : str = "Powertools API" ,
1549
+ title : str = "Powertools for AWS Lambda (Python) API" ,
1548
1550
version : str = DEFAULT_API_VERSION ,
1549
1551
openapi_version : str = DEFAULT_OPENAPI_VERSION ,
1550
1552
summary : str | None = None ,
@@ -1596,6 +1598,29 @@ def get_openapi_schema(
1596
1598
The OpenAPI schema as a pydantic model.
1597
1599
"""
1598
1600
1601
+ # DEPRECATION: Will be removed in v4.0.0. Use configure_api() instead.
1602
+ # Maintained for backwards compatibility.
1603
+ # See: https://github.com/aws-powertools/powertools-lambda-python/issues/6122
1604
+ if title == "Powertools for AWS Lambda (Python) API" and self .openapi_config .title :
1605
+ title = self .openapi_config .title
1606
+
1607
+ if version == DEFAULT_API_VERSION and self .openapi_config .version :
1608
+ version = self .openapi_config .version
1609
+
1610
+ if openapi_version == DEFAULT_OPENAPI_VERSION and self .openapi_config .openapi_version :
1611
+ openapi_version = self .openapi_config .openapi_version
1612
+
1613
+ summary = summary or self .openapi_config .summary
1614
+ description = description or self .openapi_config .description
1615
+ tags = tags or self .openapi_config .tags
1616
+ servers = servers or self .openapi_config .servers
1617
+ terms_of_service = terms_of_service or self .openapi_config .terms_of_service
1618
+ contact = contact or self .openapi_config .contact
1619
+ license_info = license_info or self .openapi_config .license_info
1620
+ security_schemes = security_schemes or self .openapi_config .security_schemes
1621
+ security = security or self .openapi_config .security
1622
+ openapi_extensions = openapi_extensions or self .openapi_config .openapi_extensions
1623
+
1599
1624
from aws_lambda_powertools .event_handler .openapi .compat import (
1600
1625
GenerateJsonSchema ,
1601
1626
get_compat_model_name_map ,
@@ -1726,7 +1751,7 @@ def _determine_openapi_version(openapi_version: str):
1726
1751
def get_openapi_json_schema (
1727
1752
self ,
1728
1753
* ,
1729
- title : str = "Powertools API" ,
1754
+ title : str = "Powertools for AWS Lambda (Python) API" ,
1730
1755
version : str = DEFAULT_API_VERSION ,
1731
1756
openapi_version : str = DEFAULT_OPENAPI_VERSION ,
1732
1757
summary : str | None = None ,
@@ -1777,6 +1802,7 @@ def get_openapi_json_schema(
1777
1802
str
1778
1803
The OpenAPI schema as a JSON serializable dict.
1779
1804
"""
1805
+
1780
1806
from aws_lambda_powertools .event_handler .openapi .compat import model_json
1781
1807
1782
1808
return model_json (
@@ -1800,6 +1826,89 @@ def get_openapi_json_schema(
1800
1826
indent = 2 ,
1801
1827
)
1802
1828
1829
+ def configure_openapi (
1830
+ self ,
1831
+ title : str = "Powertools for AWS Lambda (Python) API" ,
1832
+ version : str = DEFAULT_API_VERSION ,
1833
+ openapi_version : str = DEFAULT_OPENAPI_VERSION ,
1834
+ summary : str | None = None ,
1835
+ description : str | None = None ,
1836
+ tags : list [Tag | str ] | None = None ,
1837
+ servers : list [Server ] | None = None ,
1838
+ terms_of_service : str | None = None ,
1839
+ contact : Contact | None = None ,
1840
+ license_info : License | None = None ,
1841
+ security_schemes : dict [str , SecurityScheme ] | None = None ,
1842
+ security : list [dict [str , list [str ]]] | None = None ,
1843
+ openapi_extensions : dict [str , Any ] | None = None ,
1844
+ ):
1845
+ """Configure OpenAPI specification settings for the API.
1846
+
1847
+ Sets up the OpenAPI documentation configuration that can be later used
1848
+ when enabling Swagger UI or generating OpenAPI specifications.
1849
+
1850
+ Parameters
1851
+ ----------
1852
+ title: str
1853
+ The title of the application.
1854
+ version: str
1855
+ The version of the OpenAPI document (which is distinct from the OpenAPI Specification version or the API
1856
+ openapi_version: str, default = "3.0.0"
1857
+ The version of the OpenAPI Specification (which the document uses).
1858
+ summary: str, optional
1859
+ A short summary of what the application does.
1860
+ description: str, optional
1861
+ A verbose explanation of the application behavior.
1862
+ tags: list[Tag, str], optional
1863
+ A list of tags used by the specification with additional metadata.
1864
+ servers: list[Server], optional
1865
+ An array of Server Objects, which provide connectivity information to a target server.
1866
+ terms_of_service: str, optional
1867
+ A URL to the Terms of Service for the API. MUST be in the format of a URL.
1868
+ contact: Contact, optional
1869
+ The contact information for the exposed API.
1870
+ license_info: License, optional
1871
+ The license information for the exposed API.
1872
+ security_schemes: dict[str, SecurityScheme]], optional
1873
+ A declaration of the security schemes available to be used in the specification.
1874
+ security: list[dict[str, list[str]]], optional
1875
+ A declaration of which security mechanisms are applied globally across the API.
1876
+ openapi_extensions: Dict[str, Any], optional
1877
+ Additional OpenAPI extensions as a dictionary.
1878
+
1879
+ Example
1880
+ --------
1881
+ >>> api.configure_openapi(
1882
+ ... title="My API",
1883
+ ... version="1.0.0",
1884
+ ... description="API for managing resources",
1885
+ ... contact=Contact(
1886
+ ... name="API Support",
1887
+
1888
+ ... )
1889
+ ... )
1890
+
1891
+ See Also
1892
+ --------
1893
+ enable_swagger : Method to enable Swagger UI using these configurations
1894
+ OpenAPIConfig : Data class containing all OpenAPI configuration options
1895
+ """
1896
+ self .openapi_config = OpenAPIConfig (
1897
+ title = title ,
1898
+ version = version ,
1899
+ openapi_version = openapi_version ,
1900
+ summary = summary ,
1901
+ description = description ,
1902
+ tags = tags ,
1903
+ servers = servers ,
1904
+ terms_of_service = terms_of_service ,
1905
+ contact = contact ,
1906
+ license_info = license_info ,
1907
+ security_schemes = security_schemes ,
1908
+ security = security ,
1909
+ openapi_extensions = openapi_extensions ,
1910
+ )
1911
+
1803
1912
def enable_swagger (
1804
1913
self ,
1805
1914
* ,
@@ -1867,6 +1976,7 @@ def enable_swagger(
1867
1976
openapi_extensions: dict[str, Any], optional
1868
1977
Additional OpenAPI extensions as a dictionary.
1869
1978
"""
1979
+
1870
1980
from aws_lambda_powertools .event_handler .openapi .compat import model_json
1871
1981
from aws_lambda_powertools .event_handler .openapi .models import Server
1872
1982
from aws_lambda_powertools .event_handler .openapi .swagger_ui import (
0 commit comments