Skip to content

Commit

Permalink
Merge pull request #61 from obmarg/master
Browse files Browse the repository at this point in the history
Add support for marshalling dict-like objects.
  • Loading branch information
analogue committed Feb 24, 2016
2 parents 81f0524 + e5724e4 commit 628b164
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions bravado_core/schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from bravado_core.exception import SwaggerMappingError

try:
from collections import Mapping
except ImportError:
from collections.abc import Mapping

# 'object' and 'array' are omitted since this should really be read as
# "Swagger types that map to python primitives"
SWAGGER_PRIMITIVES = (
Expand Down Expand Up @@ -45,8 +50,7 @@ def is_dict_like(spec):
:param spec: swagger object specification in dict form
:rtype: boolean
"""
# TODO: check magic method instead
return type(spec) == dict
return isinstance(spec, Mapping)


def is_list_like(spec):
Expand Down
23 changes: 23 additions & 0 deletions tests/marshal/marshal_schema_object_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
from collections import defaultdict

import pytest

Expand Down Expand Up @@ -29,6 +30,28 @@ def test_dicts_can_be_used_instead_of_models(petstore_dict):
assert expected == result


def test_defaultdicts_can_be_used_instead_of_models(petstore_dict):
petstore_spec = Spec.from_dict(petstore_dict)
pet_spec = petstore_spec.spec_dict['definitions']['Pet']
pet = defaultdict(None, {
'id': 1,
'name': 'Fido',
'status': 'sold',
'photoUrls': ['wagtail.png', 'bark.png'],
'category': {
'id': 200,
'name': 'friendly',
},
'tags': [
{'id': 99, 'name': 'mini'},
{'id': 100, 'name': 'brown'},
],
})
expected = copy.deepcopy(pet)
result = marshal_schema_object(petstore_spec, pet_spec, pet)
assert expected == result


def test_unknown_type_raises_error(empty_swagger_spec):
invalid_spec = {'type': 'foo'}
with pytest.raises(SwaggerMappingError) as excinfo:
Expand Down

0 comments on commit 628b164

Please sign in to comment.