Skip to content

Commit ef26292

Browse files
author
Joel Collins
committed
Tidied up spec inheritance
1 parent a256ed1 commit ef26292

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

labthings/server/spec/apispec.py

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,42 @@
33

44
from ...core.utilities import get_docstring, get_summary, rupdate
55
from .paths import rule_to_path, rule_to_params
6-
from .utilities import convert_schema
6+
from .utilities import convert_schema, update_spec
77

88
from werkzeug.routing import Rule
99
from http import HTTPStatus
1010

1111

12+
def build_spec(view, inherit_from=None):
13+
# Create empty spec if missing so we can work safely with it
14+
if not hasattr(view, "__apispec__"):
15+
view.__apispec__ = {}
16+
# Check for a spec to inherit from
17+
inherited_spec = getattr(inherit_from, "__apispec__", {})
18+
19+
# Build a description
20+
description = (
21+
getattr(view, "__apispec__").get("description")
22+
or get_docstring(view)
23+
or inherited_spec.get("description")
24+
)
25+
26+
# Build a summary
27+
summary = (
28+
getattr(view, "__apispec__").get("summary")
29+
or inherited_spec.get("summary")
30+
or description
31+
)
32+
33+
# Build tags
34+
tags = getattr(view, "__apispec__").get("tags", [])
35+
tags.extend(inherited_spec.get("tags", []))
36+
37+
return update_spec(
38+
view, {"description": description, "summary": summary, "tags": tags}
39+
)
40+
41+
1242
def rule_to_apispec_path(rule: Rule, view: View, spec: APISpec):
1343
"""Generate APISpec Path arguments from a flask Rule and View
1444
@@ -20,22 +50,15 @@ def rule_to_apispec_path(rule: Rule, view: View, spec: APISpec):
2050
Returns:
2151
dict: APISpec `path` funtion argument dictionary
2252
"""
23-
if hasattr(view, "__apispec__"):
24-
description = getattr(view, "__apispec__").get( # Look for view class API spec
25-
"description"
26-
) or get_docstring( # Or view class docstring
27-
view
28-
)
29-
else:
30-
description = get_docstring(view) # Or view class docstring
53+
54+
# Populate missing spec parameters
55+
build_spec(view)
3156

3257
params = {
3358
"path": rule_to_path(rule),
34-
"operations": view_to_apispec_operations(
35-
view, spec, view_description=description
36-
),
37-
"description": description,
38-
"summary": get_summary(view),
59+
"operations": view_to_apispec_operations(view, spec),
60+
"description": getattr(view, "__apispec__").get("description"),
61+
"summary": getattr(view, "__apispec__").get("summary"),
3962
}
4063

4164
# Add URL arguments
@@ -51,7 +74,7 @@ def rule_to_apispec_path(rule: Rule, view: View, spec: APISpec):
5174
return params
5275

5376

54-
def view_to_apispec_operations(view: View, spec: APISpec, view_description=None):
77+
def view_to_apispec_operations(view: View, spec: APISpec):
5578
"""Generate APISpec `operations` argument from a flask View
5679
5780
Args:
@@ -61,31 +84,25 @@ def view_to_apispec_operations(view: View, spec: APISpec, view_description=None)
6184
Returns:
6285
dict: APISpec `operations` dictionary
6386
"""
64-
# Operations inherit tags from parent
65-
inherited_tags = []
66-
if hasattr(view, "__apispec__"):
67-
inherited_tags = getattr(view, "__apispec__").get("tags", [])
6887

6988
# Build dictionary of operations (HTTP methods)
7089
ops = {}
7190
for method in View.methods:
7291
if hasattr(view, method):
7392
ops[method] = {}
7493
method_function = getattr(view, method)
75-
description = (
76-
getattr(method_function, "__apispec__").get(
77-
"description"
78-
) # Look for APISpec
79-
or get_docstring(method_function) # Or function docstring
80-
or view_description # Or inherit from view class
81-
)
94+
95+
# Populate missing spec parameters
96+
build_spec(method_function, inherit_from=view)
8297

8398
rupdate(
8499
ops[method],
85100
{
86-
"description": description,
87-
"summary": get_summary(method_function),
88-
"tags": inherited_tags,
101+
"description": getattr(method_function, "__apispec__").get(
102+
"description"
103+
),
104+
"summary": getattr(method_function, "__apispec__").get("summary"),
105+
"tags": getattr(method_function, "__apispec__").get("tags"),
89106
},
90107
)
91108

0 commit comments

Comments
 (0)