Skip to content

Commit 8f1cc14

Browse files
committed
Cope with missing input/output schemas
1 parent 67a9257 commit 8f1cc14

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/labthings/apispec/plugins.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,12 @@ def spec_for_property(cls, prop):
110110
d.get(method, {}),
111111
{
112112
"requestBody": {
113-
"content": {
114-
prop.content_type: { "schema": class_schema }
115-
}
113+
"content": {prop.content_type: {"schema": class_schema}}
116114
},
117115
"responses": {
118116
200: {
119117
"content": {
120-
prop.content_type: { "schema": class_schema }
118+
prop.content_type: {"schema": class_schema}
121119
},
122120
"description": "Write property",
123121
}
@@ -132,9 +130,7 @@ def spec_for_property(cls, prop):
132130
{
133131
"responses": {
134132
200: {
135-
"content": {
136-
prop.content_type: { "schema": class_schema }
137-
},
133+
"content": {prop.content_type: {"schema": class_schema}},
138134
"description": "Read property",
139135
}
140136
},
@@ -149,30 +145,42 @@ def spec_for_property(cls, prop):
149145

150146
def spec_for_action(self, action):
151147
action_input = ensure_schema(action.args, name=f"{action.__name__}InputSchema")
152-
action_output = ensure_schema(action.schema, name=f"{action.__name__}OutputSchema")
148+
action_output = ensure_schema(
149+
action.schema, name=f"{action.__name__}OutputSchema"
150+
)
153151
# We combine input/output parameters with ActionSchema using an
154152
# allOf directive, so we don't end up duplicating the schema
155153
# for every action.
156154
if action_output or action_input:
157155
# It would be neater to combine the schemas in OpenAPI with allOf
158156
# I think the code below does it - but I'm not yet convinced it is working
159-
#TODO: add tests to validate this
157+
# TODO: add tests to validate this
160158
plugin = get_marshamallow_plugin(self.spec)
159+
action_input_dict = (
160+
plugin.resolver.resolve_schema_dict(action_input)
161+
if action_input
162+
else {}
163+
)
164+
action_output_dict = (
165+
plugin.resolver.resolve_schema_dict(action_output)
166+
if action_output
167+
else {}
168+
)
161169
action_schema = {
162170
"allOf": [
163171
plugin.resolver.resolve_schema_dict(ActionSchema),
164172
{
165173
"type": "object",
166-
"parameters": {
167-
"input": plugin.resolver.resolve_schema_dict(action_input),
168-
"output": plugin.resolver.resolve_schema_dict(action_output),
169-
}
170-
}
174+
"properties": {
175+
"input": action_input_dict,
176+
"output": action_output_dict,
177+
},
178+
},
171179
]
172180
}
173181
# The line below builds an ActionSchema subclass. This works and
174182
# is valid, but results in ActionSchema being duplicated many times...
175-
#action_schema = build_action_schema(action_output, action_input)
183+
# action_schema = build_action_schema(action_output, action_input)
176184
else:
177185
action_schema = ActionSchema
178186

@@ -197,16 +205,12 @@ def spec_for_action(self, action):
197205
"description": "Action completed immediately",
198206
# Allow customising 200 (immediate response) content type?
199207
# TODO: I'm not convinced it's still possible to customise this.
200-
"content": {
201-
"application/json": { "schema": action_schema }
202-
},
208+
"content": {"application/json": {"schema": action_schema}},
203209
},
204210
201: {
205211
"description": "Action started",
206212
# Our POST 201 MUST be application/json
207-
"content": {
208-
"application/json": { "schema": action_schema }
209-
},
213+
"content": {"application/json": {"schema": action_schema}},
210214
},
211215
},
212216
},

0 commit comments

Comments
 (0)