Add pretty and media_type options to codecs
parent
d2cc0a348c
commit
8b0da2607f
|
|
@ -88,7 +88,7 @@ class _OpenAPICodec(object):
|
||||||
|
|
||||||
:param dict spec: a python dict
|
:param dict spec: a python dict
|
||||||
:return: string representation of ``spec``
|
:return: string representation of ``spec``
|
||||||
:rtype: str
|
:rtype: str or bytes
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError("override this method")
|
raise NotImplementedError("override this method")
|
||||||
|
|
||||||
|
|
@ -105,9 +105,22 @@ class _OpenAPICodec(object):
|
||||||
class OpenAPICodecJson(_OpenAPICodec):
|
class OpenAPICodecJson(_OpenAPICodec):
|
||||||
media_type = 'application/json'
|
media_type = 'application/json'
|
||||||
|
|
||||||
|
def __init__(self, validators, pretty=False, media_type='application/json'):
|
||||||
|
super(OpenAPICodecJson, self).__init__(validators)
|
||||||
|
self.pretty = pretty
|
||||||
|
self.media_type = media_type
|
||||||
|
|
||||||
def _dump_dict(self, spec):
|
def _dump_dict(self, spec):
|
||||||
"""Dump ``spec`` into JSON."""
|
"""Dump ``spec`` into JSON.
|
||||||
return json.dumps(spec)
|
|
||||||
|
:rtype: str"""
|
||||||
|
if self.pretty:
|
||||||
|
out = json.dumps(spec, indent=4, separators=(',', ': '))
|
||||||
|
if out[-1] != '\n':
|
||||||
|
out += '\n'
|
||||||
|
return out
|
||||||
|
else:
|
||||||
|
return json.dumps(spec)
|
||||||
|
|
||||||
|
|
||||||
YAML_MAP_TAG = u'tag:yaml.org,2002:map'
|
YAML_MAP_TAG = u'tag:yaml.org,2002:map'
|
||||||
|
|
@ -201,6 +214,12 @@ def yaml_sane_load(stream):
|
||||||
class OpenAPICodecYaml(_OpenAPICodec):
|
class OpenAPICodecYaml(_OpenAPICodec):
|
||||||
media_type = 'application/yaml'
|
media_type = 'application/yaml'
|
||||||
|
|
||||||
|
def __init__(self, validators, media_type='application/yaml'):
|
||||||
|
super(OpenAPICodecYaml, self).__init__(validators)
|
||||||
|
self.media_type = media_type
|
||||||
|
|
||||||
def _dump_dict(self, spec):
|
def _dump_dict(self, spec):
|
||||||
"""Dump ``spec`` into YAML."""
|
"""Dump ``spec`` into YAML.
|
||||||
|
|
||||||
|
:rtype: bytes"""
|
||||||
return yaml_sane_dump(spec, binary=True)
|
return yaml_sane_dump(spec, binary=True)
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ class Command(BaseCommand):
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-m', '--mock-request', dest='mock',
|
'-m', '--mock-request', dest='mock',
|
||||||
default=False, action='store_true',
|
default=False, action='store_true',
|
||||||
help='Use a mock request when generating the swagger schema. This is useful if your views or serializers'
|
help='Use a mock request when generating the swagger schema. This is useful if your views or serializers '
|
||||||
'depend on context from a request in order to function.'
|
'depend on context from a request in order to function.'
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|
@ -85,11 +85,9 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
def write_schema(self, schema, stream, format):
|
def write_schema(self, schema, stream, format):
|
||||||
if format == 'json':
|
if format == 'json':
|
||||||
codec = OpenAPICodecJson(validators=[])
|
codec = OpenAPICodecJson(validators=[], pretty=True)
|
||||||
swagger_json = codec.encode(schema)
|
swagger_json = codec.encode(schema).decode('utf-8')
|
||||||
swagger_json = json.loads(swagger_json.decode('utf-8'), object_pairs_hook=OrderedDict)
|
stream.write(swagger_json)
|
||||||
pretty_json = json.dumps(swagger_json, indent=4, ensure_ascii=True)
|
|
||||||
stream.write(pretty_json)
|
|
||||||
elif format == 'yaml':
|
elif format == 'yaml':
|
||||||
codec = OpenAPICodecYaml(validators=[])
|
codec = OpenAPICodecYaml(validators=[])
|
||||||
swagger_yaml = codec.encode(schema).decode('utf-8')
|
swagger_yaml = codec.encode(schema).decode('utf-8')
|
||||||
|
|
@ -142,6 +140,7 @@ class Command(BaseCommand):
|
||||||
if mock:
|
if mock:
|
||||||
request = self.get_mock_request(api_url, format, user)
|
request = self.get_mock_request(api_url, format, user)
|
||||||
|
|
||||||
|
|
||||||
if request and api_version:
|
if request and api_version:
|
||||||
request.version = api_version
|
request.version = api_version
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue