Skip to content

Commit cc777d9

Browse files
author
Joel Collins
committed
Fixed recursion in schema2json
1 parent 6a3b259 commit cc777d9

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

labthings/server/spec/__init__.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,22 @@ def convert_schema(schema, spec: APISpec):
141141
if not schema:
142142
return schema
143143

144-
# Call callables
145-
if callable(schema):
146-
working_schema = schema()
147-
else:
148-
working_schema = schema
149-
150144
# Expand/convert actual schema data
151-
if isinstance(working_schema, BaseSchema):
152-
return working_schema
153-
elif isinstance(working_schema, Mapping):
154-
return map2properties(working_schema, spec)
155-
elif isinstance(working_schema, Field):
156-
return field2property(working_schema, spec)
145+
if isinstance(schema, BaseSchema):
146+
return schema
147+
elif isinstance(schema, Mapping):
148+
return map2properties(schema, spec)
149+
elif isinstance(schema, Field):
150+
return field2property(schema, spec)
157151
else:
158152
raise TypeError(
159-
f"Unsupported schema type {working_schema}. Ensure schema is a Schema class, or dictionary of Field objects"
153+
f"Unsupported schema type {schema}. Ensure schema is a Schema class, or dictionary of Field objects"
160154
)
161155

162156

163157
def map2properties(schema, spec: APISpec):
164158
"""
165-
Convert any dictionary-like map of Marshmallow fields into a dictionary describing it's JSON schema
159+
Recursively convert any dictionary-like map of Marshmallow fields into a dictionary describing it's JSON schema
166160
"""
167161
marshmallow_plugin = next(
168162
plugin for plugin in spec.plugins if isinstance(plugin, MarshmallowPlugin)
@@ -195,12 +189,10 @@ def field2property(field, spec: APISpec):
195189

196190
def schema2json(schema, spec: APISpec):
197191
"""
198-
Convert any Marshmallow schema, field, or dictionary of fields stright to a JSON schema
192+
Convert any Marshmallow schema stright to a JSON schema.
199193
This should not be used when generating APISpec documentation, otherwise schemas wont
200194
be listed in the "schemas" list. This is used, for example, in the Thing Description.
201195
"""
202-
if not isinstance(schema, BaseSchema):
203-
schema = convert_schema(schema, spec)
204196

205197
if isinstance(schema, BaseSchema):
206198
marshmallow_plugin = next(

0 commit comments

Comments
 (0)