1+ from inspect import isclass
12from typing import Dict , Union
23
34from 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
1718def 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