+17
-2
@@ -87,7 +87,7 @@ class _OpenAPICodec(object):
|
||||
:rtype: OrderedDict
|
||||
"""
|
||||
swagger.security_definitions = swagger_settings.SECURITY_DEFINITIONS
|
||||
return swagger
|
||||
return swagger.as_odict()
|
||||
|
||||
|
||||
class OpenAPICodecJson(_OpenAPICodec):
|
||||
@@ -146,9 +146,24 @@ SaneYamlDumper.add_representer(OrderedDict, SaneYamlDumper.represent_odict)
|
||||
SaneYamlDumper.add_multi_representer(OrderedDict, SaneYamlDumper.represent_odict)
|
||||
|
||||
|
||||
def yaml_sane_dump(data, binary):
|
||||
"""Dump the given data dictionary into a sane format:
|
||||
|
||||
* OrderedDicts are dumped as regular mappings instead of non-standard !!odict
|
||||
* multi-line mapping style instead of json-like inline style
|
||||
* list elements are indented into their parents
|
||||
|
||||
:param dict data: the data to be serializers
|
||||
:param bool binary: True to return a utf-8 encoded binary object, False to return a string
|
||||
:return: the serialized YAML
|
||||
:rtype: str,bytes
|
||||
"""
|
||||
return yaml.dump(data, Dumper=SaneYamlDumper, default_flow_style=False, encoding='utf-8' if binary else None)
|
||||
|
||||
|
||||
class OpenAPICodecYaml(_OpenAPICodec):
|
||||
media_type = 'application/yaml'
|
||||
|
||||
def _dump_dict(self, spec):
|
||||
"""Dump ``spec`` into YAML."""
|
||||
return yaml.dump(spec, Dumper=SaneYamlDumper, default_flow_style=False, encoding='utf-8')
|
||||
return yaml_sane_dump(spec, binary=True)
|
||||
|
||||
+26
-5
@@ -62,6 +62,13 @@ def make_swagger_name(attribute_name):
|
||||
return camelize(attribute_name.rstrip('_'), uppercase_first_letter=False)
|
||||
|
||||
|
||||
def _bare_SwaggerDict(cls):
|
||||
assert issubclass(cls, SwaggerDict)
|
||||
result = cls.__new__(cls)
|
||||
OrderedDict.__init__(result) # no __init__ called for SwaggerDict subclasses!
|
||||
return result
|
||||
|
||||
|
||||
class SwaggerDict(OrderedDict):
|
||||
"""A particular type of OrderedDict, which maps all attribute accesses to dict lookups using
|
||||
:func:`.make_swagger_name`. Attribute names starting with ``_`` are set on the object as-is and are not included
|
||||
@@ -108,11 +115,25 @@ class SwaggerDict(OrderedDict):
|
||||
for attr, val in self._extras__.items():
|
||||
setattr(self, attr, val)
|
||||
|
||||
# noinspection PyArgumentList,PyDefaultArgument
|
||||
def __deepcopy__(self, memodict={}):
|
||||
result = OrderedDict(list(self.items()))
|
||||
result.update(copy.deepcopy(result, memodict))
|
||||
return result
|
||||
@staticmethod
|
||||
def _as_odict(obj):
|
||||
if isinstance(obj, dict):
|
||||
result = OrderedDict()
|
||||
for attr, val in obj.items():
|
||||
result[attr] = SwaggerDict._as_odict(val)
|
||||
return result
|
||||
elif isinstance(obj, (list, tuple)):
|
||||
return type(obj)(SwaggerDict._as_odict(elem) for elem in obj)
|
||||
|
||||
return obj
|
||||
|
||||
def as_odict(self):
|
||||
return SwaggerDict._as_odict(self)
|
||||
|
||||
def __reduce__(self):
|
||||
# for pickle supprt; this skips calls to all SwaggerDict __init__ methods and relies
|
||||
# on the already set attributes instead
|
||||
return _bare_SwaggerDict, (type(self),), vars(self), None, iter(self.items())
|
||||
|
||||
|
||||
class Contact(SwaggerDict):
|
||||
|
||||
Reference in New Issue
Block a user