diff --git a/bravado_core/param.py b/bravado_core/param.py index d6100e60..108fbc04 100644 --- a/bravado_core/param.py +++ b/bravado_core/param.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import datetime import logging from functools import partial @@ -23,12 +24,17 @@ } +def default_handler(x): + if isinstance(x, datetime.datetime) or isinstance(x, datetime.date): + return x.isoformat() + + def stringify_body(value): """Json dump the value to string if not already in string """ if not value or isinstance(value, six.string_types): return value - return json.dumps(value) + return json.dumps(value, default=default_handler) class Param(object): @@ -141,7 +147,7 @@ def marshal_param(param, value, request): request.setdefault('data', {})[param.name] = value elif location == 'body': request['headers']['Content-Type'] = APP_JSON - request['data'] = json.dumps(value) + request['data'] = json.dumps(value, default=default_handler) else: raise SwaggerMappingError( "Don't know how to marshal_param with location {0}". diff --git a/tests/param/marshal_param_test.py b/tests/param/marshal_param_test.py index 7f0ba250..0c1fb105 100644 --- a/tests/param/marshal_param_test.py +++ b/tests/param/marshal_param_test.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import copy +import datetime import pytest from mock import Mock @@ -159,6 +160,46 @@ def test_body(empty_swagger_spec, param_spec): assert APP_JSON == request['headers']['Content-Type'] +def test_body_nested_date(empty_swagger_spec, param_spec): + now = datetime.date.today() + now_param = {'updated_at': now} + now_str = '{"updated_at": "' + now.isoformat() + '"}' + param_spec['in'] = 'body' + param_spec['schema'] = { + 'type': 'object' + } + del param_spec['type'] + del param_spec['format'] + param = Param(empty_swagger_spec, Mock(spec=Operation), param_spec) + request = { + 'headers': { + } + } + marshal_param(param, now_param, request) + assert now_str == request['data'] + assert APP_JSON == request['headers']['Content-Type'] + + +def test_body_nested_datetime(empty_swagger_spec, param_spec): + now = datetime.datetime.utcnow() + now_param = {"updated_at": now} + now_str = '{"updated_at": "' + now.isoformat() + '"}' + param_spec['in'] = 'body' + param_spec['schema'] = { + 'type': 'object' + } + del param_spec['type'] + del param_spec['format'] + param = Param(empty_swagger_spec, Mock(spec=Operation), param_spec) + request = { + 'headers': { + } + } + marshal_param(param, now_param, request) + assert now_str == request['data'] + assert APP_JSON == request['headers']['Content-Type'] + + def test_formData_integer(empty_swagger_spec, param_spec): param_spec['in'] = 'formData' param = Param(empty_swagger_spec, Mock(spec=Operation), param_spec) diff --git a/tests/param/stringify_body_test.py b/tests/param/stringify_body_test.py index ce8922ee..261387cc 100644 --- a/tests/param/stringify_body_test.py +++ b/tests/param/stringify_body_test.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import datetime import json from bravado_core.param import stringify_body @@ -12,3 +13,19 @@ def test_stringify_body_converts_dict_to_str(): def test_stringify_body_ignores_data_if_already_str(): assert 'foo' == stringify_body('foo') + + +def test_stringify_body_handles_date(): + now = datetime.date.today() + now_str = '{"now": "' + now.isoformat() + '"}' + body = {'now': now} + body_str = stringify_body(body) + assert body_str == now_str + + +def test_stringify_body_handles_datetime(): + now = datetime.datetime.utcnow() + now_str = '{"now": "' + now.isoformat() + '"}' + body = {'now': now} + body_str = stringify_body(body) + assert body_str == now_str