Skip to content

Commit

Permalink
Merge pull request #106 from Wormfriend/fix_issue_86
Browse files Browse the repository at this point in the history
Fix issue #86 and issue #103 combined
  • Loading branch information
apryor6 authored Jun 6, 2021
2 parents d6ce0bf + ed01149 commit d850de1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion flask_accepts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def unpack_nested(val, api, model_name: str = None, operation: str = "dump"):
return unpack_nested_self(val, api, model_name, operation)

model_name = get_default_model_name(val.nested)

if val.many:
return fr.List(
fr.Nested(
map_type(val.nested, api, model_name, operation), **_ma_field_to_fr_field(val)
)
)

return fr.Nested(
map_type(val.nested, api, model_name, operation), **_ma_field_to_fr_field(val)
)
Expand Down Expand Up @@ -160,7 +168,7 @@ def get_default_model_name(schema: Optional[Union[Schema, Type[Schema]]] = None)
def _ma_field_to_fr_field(value: ma.Field) -> dict:
fr_field_parameters = {}

if hasattr(value, "default"):
if hasattr(value, "default") and type(value.default) != ma.utils._Missing:
fr_field_parameters["example"] = value.default

if hasattr(value, "required"):
Expand Down
18 changes: 18 additions & 0 deletions flask_accepts/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ class IntegerSchema(Schema):
assert result


def test_unpack_nested_many():
app = Flask(__name__)
api = Api(app)

class NestedSchema(Schema):
my_string = ma.String()

class IntegerSchema(Schema):
my_int = ma.Integer()
children = ma.Nested(NestedSchema, many=True)

schema = IntegerSchema()

result = utils.unpack_nested(schema.fields.get("children"), api=api)

assert type(result) == fr.List


def test_unpack_nested_self():
app = Flask(__name__)
api = Api(app)
Expand Down

0 comments on commit d850de1

Please sign in to comment.