Skip to content

Commit 996e9ad

Browse files
author
Patrick Schemitz
committed
Allow deprecating endpoints
1 parent d2d0bc9 commit 996e9ad

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

openapi_bridge/__init__.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def _replace_const(d):
106106

107107

108108
def _patch_dict(d, key, transform, filter_null=False):
109-
if isinstance(d, dict):
109+
if isinstance(d, dict): # noqa:PLR1702 (too-many-nested-blocks)
110110
for k, v in list(d.items()): # so we can pop
111111
if isinstance(v, (dict, list)):
112112
_patch_dict(v, key, transform)
@@ -156,6 +156,13 @@ class endpoint:
156156
Allows for almost seamless integration of pydantic models with OpenAPI,
157157
generating YAML for the endpoint from type hints.
158158
159+
# Parameters
160+
161+
- method: HTTP verb, defaults to "get"
162+
- path_prefix: The optional prefix, to support multiple APIs (e.g. internal vs. external).
163+
- security: A custom security, defaults to basic auth
164+
- response_model_exclude_none: Whether to remove Nones from the response (defaults to False)
165+
- deprecated: Whether to mark this endpoint as deprecated
159166
160167
# Collecting Endpoint Information
161168
@@ -247,16 +254,19 @@ def create_app():
247254
def __init__(
248255
self,
249256
path,
257+
*,
250258
method="get",
251259
path_prefix="default",
252260
security=None,
253261
response_model_exclude_none=False,
262+
deprecated=False,
254263
):
255264
self.path = path
256265
self.method = method.lower()
257266
self.path_prefix = path_prefix
258267
self.security = security if security is not None else endpoint.SECURITY_BASIC
259268
self.response_model_exclude_none = response_model_exclude_none
269+
self.deprecated = deprecated
260270

261271
def __call__(self, fn):
262272
parameters = []
@@ -322,9 +332,7 @@ def __call__(self, fn):
322332
request_body["content"]["multipart/form-data"]["schema"]["properties"][arg] = {"description": docs["param"][arg], **param["schema"]}
323333
else:
324334
parameters.append(param)
325-
PATHS.setdefault(self.path_prefix, {}).setdefault(self.path, {})[
326-
self.method
327-
] = {
335+
endpoint_config = {
328336
"parameters": parameters,
329337
"summary": summary,
330338
"description": description,
@@ -342,6 +350,11 @@ def __call__(self, fn):
342350
**self.security,
343351
**({"requestBody": request_body} if request_body else {}),
344352
}
353+
if self.deprecated:
354+
endpoint_config["deprecated"] = True
355+
PATHS.setdefault(self.path_prefix, {}).setdefault(self.path, {})[
356+
self.method
357+
] = endpoint_config
345358

346359
@functools.wraps(fn)
347360
def wrapper(*args, **kwargs):
@@ -355,7 +368,7 @@ def wrapper(*args, **kwargs):
355368

356369
return wrapper
357370

358-
def _get_schema(self, annotation, default, in_, name=None, example=None):
371+
def _get_schema(self, annotation, default, in_, name=None, example=None): # noqa:PLR0912 (too-many-branches)
359372
constraints = None
360373
origin = typing.get_origin(annotation)
361374
if str(annotation).startswith("typing.Optional"):

0 commit comments

Comments
 (0)