Fix a bug in the way `default_renderer` is applied.

When an explicit `content_type` is specified to `pecan.expose()`, it should
always take precendece over the `default_renderer` specified at the application
level.

Fixes-bug: #1465688

Change-Id: I633777a3d682f17b7f61cbe691c6e93f2ce2a310
This commit is contained in:
Ryan Petrello 2015-06-25 11:48:49 -04:00
parent 0001a9d94d
commit 1fe3cec746
3 changed files with 22 additions and 5 deletions

View File

@ -585,9 +585,11 @@ class PecanBase(object):
template = content_types.get(pecan_state['content_type'])
# check if for controller override of template
template = pecan_state.get('override_template', template) or (
'json' if self.default_renderer == 'json' else None
)
template = pecan_state.get('override_template', template)
if template is None and cfg['explicit_content_type'] is False:
if self.default_renderer == 'json':
template = 'json'
pecan_state['content_type'] = pecan_state.get(
'override_content_type',
pecan_state['content_type']

View File

@ -23,9 +23,9 @@ def when_for(controller):
def expose(template=None,
content_type='text/html',
generic=False,
route=None):
route=None,
**kw):
'''
Decorator used to flag controller methods as being "exposed" for
@ -46,6 +46,8 @@ def expose(template=None,
wanted to route a function to `some-special-path'.
'''
content_type = kw.get('content_type', 'text/html')
if template == 'json':
content_type = 'application/json'
@ -54,6 +56,7 @@ def expose(template=None,
f.exposed = True
cfg = _cfg(f)
cfg['explicit_content_type'] = 'content_type' in kw
if route:
# This import is here to avoid a circular import issue

View File

@ -1957,6 +1957,18 @@ class TestEngines(PecanTestCase):
result = dict(json.loads(r.body.decode()))
assert result == {'name': 'Bill'}
def test_default_json_renderer_with_explicit_content_type(self):
class RootController(object):
@expose(content_type='text/plain')
def index(self, name='Bill'):
return name
app = TestApp(Pecan(RootController(), default_renderer='json'))
r = app.get('/')
assert r.status_int == 200
assert r.body == b_("Bill")
class TestDeprecatedRouteMethod(PecanTestCase):