Skip to content

Commit 2e28d14

Browse files
author
Joel Collins
committed
Migrated OpenAPI away from APISpec library
1 parent 9490527 commit 2e28d14

File tree

3 files changed

+13
-109
lines changed

3 files changed

+13
-109
lines changed

src/labthings/apispec/apispec.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def rule_to_apispec_path(rule: Rule, view, apispec: APISpec):
2323

2424
params = {
2525
"path": rule_to_path(rule),
26-
"operations": view_to_apispec_operations(view, apispec),
26+
"operations": view.get_apispec(),
2727
}
2828

2929
# Add URL arguments to operations
@@ -33,27 +33,3 @@ def rule_to_apispec_path(rule: Rule, view, apispec: APISpec):
3333
params["operations"][op].update({"parameters": rule_to_params(rule)})
3434

3535
return params
36-
37-
38-
def view_to_apispec_operations(view, apispec: APISpec):
39-
specs = {}
40-
if hasattr(view, "get_apispec"):
41-
specs = view.get_apispec()
42-
43-
for method, spec in specs.items():
44-
if "responses" in spec:
45-
for response_code, response in spec["responses"].items():
46-
if "schema" in response:
47-
response["schema"] = convert_to_schema_or_json(
48-
response["schema"], apispec
49-
)
50-
51-
if "requestBody" in spec:
52-
for content_type, description in (
53-
spec["requestBody"].get("content", {}).items()
54-
):
55-
description["schema"] = convert_to_schema_or_json(
56-
description["schema"], apispec
57-
)
58-
59-
return specs

src/labthings/view/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ..schema import Schema, ActionSchema, build_action_schema
1616
from ..tasks import taskify
1717
from ..deque import Deque, resize_deque
18+
from ..json.schemas import schema_to_json
1819

1920
from gevent.timeout import Timeout
2021

@@ -145,7 +146,9 @@ def get_apispec(cls):
145146
"responses": {
146147
# Our POST 201 will usually be application/json
147148
201: {
148-
"schema": build_action_schema(cls.schema, cls.args)(),
149+
"schema": schema_to_json(
150+
build_action_schema(cls.schema, cls.args)()
151+
),
149152
"content_type": "application/json",
150153
"description": "Action started",
151154
}
@@ -158,7 +161,9 @@ def get_apispec(cls):
158161
"responses": {
159162
# Our GET 200 will usually be application/json
160163
200: {
161-
"schema": build_action_schema(cls.schema, cls.args)(many=True),
164+
"schema": schema_to_json(
165+
build_action_schema(cls.schema, cls.args)(many=True)
166+
),
162167
"content_type": "application/json",
163168
"description": "Action started",
164169
}
@@ -222,6 +227,7 @@ class PropertyView(View):
222227
@classmethod
223228
def get_apispec(cls):
224229
d = {}
230+
class_json_schema = schema_to_json(cls.schema) if cls.schema else None
225231

226232
# writeproperty methods
227233
for method in ("put", "post"):
@@ -232,11 +238,11 @@ def get_apispec(cls):
232238
"summary": getattr(cls, "summary", None) or get_summary(cls),
233239
"tags": list(cls.get_tags()),
234240
"requestBody": {
235-
"content": {"application/json": {"schema": cls.schema}}
241+
"content": {"application/json": {"schema": class_json_schema}}
236242
},
237243
"responses": {
238244
200: {
239-
"schema": cls.schema,
245+
"schema": class_json_schema,
240246
"content_type": "application/json",
241247
"description": "Write property",
242248
}
@@ -250,7 +256,7 @@ def get_apispec(cls):
250256
"tags": list(cls.get_tags()),
251257
"responses": {
252258
200: {
253-
"schema": cls.schema,
259+
"schema": class_json_schema,
254260
"content_type": "application/json",
255261
"description": "Read property",
256262
}
@@ -260,6 +266,7 @@ def get_apispec(cls):
260266
# Enable custom responses from all methods
261267
for method in d.keys():
262268
d[method]["responses"].update(cls.responses)
269+
263270
return d
264271

265272
def dispatch_request(self, *args, **kwargs):

tests/test_apispec_apispec.py

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,6 @@
44
from labthings import fields
55

66

7-
def test_view_to_apispec_operations_no_spec(spec):
8-
class Index(View):
9-
"""Index docstring"""
10-
11-
def get(self):
12-
return "GET"
13-
14-
def post(self):
15-
return "POST"
16-
17-
assert apispec.view_to_apispec_operations(Index, spec) == {
18-
"post": {
19-
"description": "Index docstring",
20-
"summary": "Index docstring",
21-
"tags": [],
22-
},
23-
"get": {
24-
"description": "Index docstring",
25-
"summary": "Index docstring",
26-
"tags": [],
27-
},
28-
}
29-
30-
31-
def test_view_to_apispec_tags(spec):
32-
class Index(View):
33-
"""Index docstring"""
34-
35-
tags = set(["tag1", "tag2"])
36-
37-
def get(self):
38-
return "GET"
39-
40-
def post(self):
41-
return "POST"
42-
43-
spec_ops = apispec.view_to_apispec_operations(Index, spec)
44-
45-
assert set(spec_ops["get"]["tags"]) == Index.tags
46-
assert set(spec_ops["post"]["tags"]) == Index.tags
47-
48-
49-
def test_dict_to_apispec_operations_params(spec):
50-
class Index(PropertyView):
51-
"""Index docstring"""
52-
53-
schema = {"integer": fields.Int()}
54-
55-
def put(self):
56-
return "PUT"
57-
58-
assert (apispec.view_to_apispec_operations(Index, spec))["put"]["requestBody"] == {
59-
"content": {
60-
"application/json": {
61-
"schema": {
62-
"type": "object",
63-
"properties": {"integer": {"type": "integer", "format": "int32"}},
64-
}
65-
}
66-
}
67-
}
68-
69-
70-
def test_dict_to_apispec_operations_schema(spec):
71-
class Index(PropertyView):
72-
73-
schema = {"integer": fields.Int()}
74-
75-
def get(self):
76-
return "GET"
77-
78-
assert apispec.view_to_apispec_operations(Index, spec)["get"]["responses"][200][
79-
"schema"
80-
] == {
81-
"type": "object",
82-
"properties": {"integer": {"type": "integer", "format": "int32"}},
83-
}
84-
85-
867
def test_rule_to_apispec_path(app, spec):
878
class Index(View):
889
"""Index docstring"""

0 commit comments

Comments
 (0)