Skip to content

Commit

Permalink
propagate parameters and responses per-operation
Browse files Browse the repository at this point in the history
We now pick up "parameters" from methods and view classes.
Responses, can also be specified per-class or per-method (or both).
  • Loading branch information
rwb27 committed Jul 19, 2021
1 parent e21c6e2 commit 9ea10f5
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/labthings/apispec/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from .. import fields
from ..json.schemas import schema_to_json
from ..schema import EventSchema, ActionSchema, Schema, build_action_schema
from ..schema import EventSchema, ActionSchema
from ..utilities import get_docstring, get_summary, merge
from .utilities import ensure_schema, get_marshamallow_plugin
from ..views import ActionView, EventView, PropertyView, View
Expand Down Expand Up @@ -54,6 +54,7 @@ class MarshmallowPlugin(_MarshmallowPlugin):

class FlaskLabThingsPlugin(BasePlugin):
"""APIspec plugin for Flask LabThings"""
spec = None

def init_spec(self, spec):
self.spec = spec
Expand All @@ -67,14 +68,18 @@ def spec_for_interaction(cls, interaction):
if hasattr(interaction, method):
prop = getattr(interaction, method)
d[method] = {
"description": getattr(prop, "description", None)
or get_docstring(prop, remove_newlines=False)
or getattr(interaction, "description", None)
or get_docstring(interaction, remove_newlines=False),
"summary": getattr(prop, "summary", None)
or get_summary(prop)
or getattr(interaction, "summary", None)
or get_summary(interaction),
"description": (
getattr(prop, "description", None)
or get_docstring(prop, remove_newlines=False)
or getattr(interaction, "description", None)
or get_docstring(interaction, remove_newlines=False)
),
"summary": (
getattr(prop, "summary", None)
or get_summary(prop)
or getattr(interaction, "summary", None)
or get_summary(interaction)
),
"tags": list(interaction.get_tags()),
"responses": {
"5XX": {
Expand All @@ -92,9 +97,14 @@ def spec_for_interaction(cls, interaction):
},
}
},
"parameters": [],
}
if hasattr(prop, "responses"):
d[method]["responses"].update(prop.responses)
# Allow custom responses from the class, overridden by the method
d[method]["responses"].update(getattr(interaction, "responses", {}))
d[method]["responses"].update(getattr(prop, "responses", {}))
# Allow custom parameters from the class & method
d[method]["parameters"].extend(getattr(interaction, "parameters", {}))
d[method]["parameters"].extend(getattr(prop, "parameters", {}))
return d

@classmethod
Expand Down

0 comments on commit 9ea10f5

Please sign in to comment.