Skip to content

Commit 9ea10f5

Browse files
committed
propagate parameters and responses per-operation
We now pick up "parameters" from methods and view classes. Responses, can also be specified per-class or per-method (or both).
1 parent e21c6e2 commit 9ea10f5

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/labthings/apispec/plugins.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from .. import fields
1212
from ..json.schemas import schema_to_json
13-
from ..schema import EventSchema, ActionSchema, Schema, build_action_schema
13+
from ..schema import EventSchema, ActionSchema
1414
from ..utilities import get_docstring, get_summary, merge
1515
from .utilities import ensure_schema, get_marshamallow_plugin
1616
from ..views import ActionView, EventView, PropertyView, View
@@ -54,6 +54,7 @@ class MarshmallowPlugin(_MarshmallowPlugin):
5454

5555
class FlaskLabThingsPlugin(BasePlugin):
5656
"""APIspec plugin for Flask LabThings"""
57+
spec = None
5758

5859
def init_spec(self, spec):
5960
self.spec = spec
@@ -67,14 +68,18 @@ def spec_for_interaction(cls, interaction):
6768
if hasattr(interaction, method):
6869
prop = getattr(interaction, method)
6970
d[method] = {
70-
"description": getattr(prop, "description", None)
71-
or get_docstring(prop, remove_newlines=False)
72-
or getattr(interaction, "description", None)
73-
or get_docstring(interaction, remove_newlines=False),
74-
"summary": getattr(prop, "summary", None)
75-
or get_summary(prop)
76-
or getattr(interaction, "summary", None)
77-
or get_summary(interaction),
71+
"description": (
72+
getattr(prop, "description", None)
73+
or get_docstring(prop, remove_newlines=False)
74+
or getattr(interaction, "description", None)
75+
or get_docstring(interaction, remove_newlines=False)
76+
),
77+
"summary": (
78+
getattr(prop, "summary", None)
79+
or get_summary(prop)
80+
or getattr(interaction, "summary", None)
81+
or get_summary(interaction)
82+
),
7883
"tags": list(interaction.get_tags()),
7984
"responses": {
8085
"5XX": {
@@ -92,9 +97,14 @@ def spec_for_interaction(cls, interaction):
9297
},
9398
}
9499
},
100+
"parameters": [],
95101
}
96-
if hasattr(prop, "responses"):
97-
d[method]["responses"].update(prop.responses)
102+
# Allow custom responses from the class, overridden by the method
103+
d[method]["responses"].update(getattr(interaction, "responses", {}))
104+
d[method]["responses"].update(getattr(prop, "responses", {}))
105+
# Allow custom parameters from the class & method
106+
d[method]["parameters"].extend(getattr(interaction, "parameters", {}))
107+
d[method]["parameters"].extend(getattr(prop, "parameters", {}))
98108
return d
99109

100110
@classmethod

0 commit comments

Comments
 (0)