Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle datetime objects #206

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions bravado_core/param.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import datetime
import logging
from functools import partial

Expand All @@ -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):
Expand Down Expand Up @@ -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}".
Expand Down
41 changes: 41 additions & 0 deletions tests/param/marshal_param_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import copy
import datetime

import pytest
from mock import Mock
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions tests/param/stringify_body_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import datetime
import json

from bravado_core.param import stringify_body
Expand All @@ -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