Skip to content

Commit b9f9fc9

Browse files
committed
Fixes from unit testing
I've made a few fixes: * corrected a typo in get_marshamallow_plugin (extra a). * fixed field2property so it doesn't return empty dictionaries for simple types * Added new cases to ensure_schema so it won't fail if given a class.
1 parent f6ba74e commit b9f9fc9

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/labthings/apispec/plugins.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from ..schema import ActionSchema, EventSchema
1212
from ..utilities import get_docstring, get_summary, merge
1313
from ..views import ActionView, EventView, PropertyView, View
14-
from .utilities import ensure_schema, get_marshamallow_plugin
14+
from .utilities import ensure_schema, get_marshmallow_plugin
1515

1616

1717
class ExtendedOpenAPIConverter(OpenAPIConverter):
@@ -166,7 +166,7 @@ def spec_for_action(self, action):
166166
# It would be neater to combine the schemas in OpenAPI with allOf
167167
# I think the code below does it - but I'm not yet convinced it is working
168168
# TODO: add tests to validate this
169-
plugin = get_marshamallow_plugin(self.spec)
169+
plugin = get_marshmallow_plugin(self.spec)
170170
action_input_dict = (
171171
plugin.resolver.resolve_schema_dict(action_input)
172172
if action_input

src/labthings/apispec/utilities.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from inspect import isclass
12
from typing import Dict, Union
23

34
from apispec.ext.marshmallow import MarshmallowPlugin
@@ -11,29 +12,42 @@ def field2property(field):
1112
"""Convert a marshmallow Field to OpenAPI dictionary"""
1213
converter = FieldConverterMixin()
1314
converter.init_attribute_functions()
14-
return converter.field2pattern(field)
15+
return converter.field2property(field)
1516

1617

1718
def ensure_schema(
1819
schema: Union[fields.Field, Schema, Dict[str, Union[fields.Field, type]]],
1920
name: str = "GeneratedFromDict",
2021
) -> Union[dict, Schema]:
21-
"""Create a Schema object, or OpenAPI dictionary, given a Field, Schema, or Dict."""
22+
"""Create a Schema object, or OpenAPI dictionary, given a Field, Schema, or Dict.
23+
24+
The output from this function should be suitable to include in a dictionary
25+
that is passed to APISpec. Fields won't get processed by the Marshmallow
26+
plugin, and can't be converted to Schemas without adding a field name, so
27+
we convert them directly to the dictionary representation.
28+
29+
Other Schemas are returned as Marshmallow Schema instances, which will be
30+
converted to references by the plugin.
31+
"""
2232
if schema is None:
2333
return None
2434
if isinstance(schema, fields.Field):
2535
return field2property(schema)
36+
if isclass(schema) and issubclass(schema, fields.Field):
37+
return field2property(schema())
2638
elif isinstance(schema, dict):
2739
return Schema.from_dict(schema, name=name)()
2840
elif isinstance(schema, Schema):
2941
return schema
42+
elif isclass(schema) and issubclass(schema, Schema):
43+
return schema()
3044
else:
3145
raise TypeError(
3246
f"Invalid schema type {type(schema)}. Must be a Schema or Mapping/dict"
3347
)
3448

3549

36-
def get_marshamallow_plugin(spec):
50+
def get_marshmallow_plugin(spec):
3751
"""Extract the marshmallow plugin object from an APISpec"""
3852
for p in spec.plugins:
3953
if isinstance(p, MarshmallowPlugin):

0 commit comments

Comments
 (0)