Skip to content

Commit

Permalink
Eliminate built action schemas
Browse files Browse the repository at this point in the history
build_action_schema was returning empty schemas.  I'm now just
directly composing input, output, and Action schemas
with allOf which works nicely.

This may need to be fixed for the thing description, though.
  • Loading branch information
rwb27 committed Jul 13, 2021
1 parent 8654d02 commit 67a9257
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/labthings/apispec/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,16 @@ def spec_for_action(self, action):
# I think the code below does it - but I'm not yet convinced it is working
#TODO: add tests to validate this
plugin = get_marshamallow_plugin(self.spec)
action_io_schema = build_action_schema(
action_output,
action_input,
base_class=Schema,
name=f"{action.__name__}InputOutputSchema"
)
action_schema = {
"allOf": [
plugin.resolver.resolve_schema_dict(ActionSchema),
plugin.resolver.resolve_schema_dict(action_io_schema),
{
"type": "object",
"parameters": {
"input": plugin.resolver.resolve_schema_dict(action_input),
"output": plugin.resolver.resolve_schema_dict(action_output),
}
}
]
}
# The line below builds an ActionSchema subclass. This works and
Expand Down
1 change: 1 addition & 0 deletions src/labthings/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def build_action_schema(
:param name: str: (Default value = None)
"""
#FIXME: this seems to lose the schemas. I suspect this is down to `nest_if_needed`.
# Create a name for the generated schema
if not name:
name = str(id(output_schema))
Expand Down
8 changes: 8 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ def test_build_action_schema_typeerror():
output_schema = object()
with pytest.raises(TypeError):
action_schema = schema.build_action_schema(input_schema, output_schema)

def test_nest_if_needed():
nested_schema = schema.nest_if_needed(schema.ActionSchema())
assert isinstance(nested_schema, fields.Field)
nested_dict = schema.nest_if_needed({"name": fields.Integer()})
assert isinstance(nested_schema, fields.Field)
nested_field = schema.nest_if_needed(fields.Boolean())
assert isinstance(nested_schema, fields.Field)

0 comments on commit 67a9257

Please sign in to comment.