Skip to content

Commit a2302fe

Browse files
author
Joel Collins
committed
Move content_type into class responses dictionary
1 parent 6d4c873 commit a2302fe

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/labthings/server/spec/apispec.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,19 @@ def view_to_apispec_operations(view, apispec: APISpec):
6363
if hasattr(view, "get_responses"):
6464
ops[op]["responses"] = {}
6565

66-
for code, schema in view.get_responses().items():
66+
for code, response in view.get_responses().items():
6767
ops[op]["responses"][code] = {
68-
"description": HTTPStatus(code).phrase,
68+
"description": response.get("description")
69+
or HTTPStatus(code).phrase,
6970
"content": {
70-
getattr(view, "content_type", "application/json"): {
71-
"schema": convert_to_schema_or_json(schema, apispec)
71+
# See if response description specifies a content_type
72+
# If not, assume application/json
73+
response.get("content_type", "application/json"): {
74+
"schema": convert_to_schema_or_json(
75+
response.get("schema"), apispec
76+
)
7277
}
73-
if schema
78+
if response.get("schema")
7479
else {} # If no schema is defined, don't include one in the APISpec
7580
},
7681
}

src/labthings/server/view/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ class View(MethodView):
4444
tags: list = []
4545
title: None
4646

47-
# Content type of response, usually application/json
48-
content_type: str = "application/json"
49-
5047
responses: dict = {}
5148

5249
def __init__(self, *args, **kwargs):
@@ -58,7 +55,7 @@ def __init__(self, *args, **kwargs):
5855

5956
@classmethod
6057
def get_responses(cls):
61-
r = {200: cls.schema}
58+
r = {200: {"schema": cls.schema, "content_type": "application/json",}}
6259
r.update(cls.responses)
6360
return r
6461

@@ -131,7 +128,13 @@ class ActionView(View):
131128
@classmethod
132129
def get_responses(cls):
133130
"""Build an output schema that includes the Action wrapper object"""
134-
r = {201: build_action_schema(cls.schema, cls.args)()}
131+
r = {
132+
201: {
133+
"schema": build_action_schema(cls.schema, cls.args)(),
134+
"content_type": "application/json",
135+
"description": "Action started",
136+
}
137+
}
135138
r.update(cls.responses)
136139
return r
137140

0 commit comments

Comments
 (0)