diff --git a/tests/pecantest/test/controllers/ws.py b/tests/pecantest/test/controllers/ws.py index ebef4e9..a26f8df 100644 --- a/tests/pecantest/test/controllers/ws.py +++ b/tests/pecantest/test/controllers/ws.py @@ -71,6 +71,11 @@ class Criterion(Base): class AuthorsController(RestController): + _custom_actions = { + 'json_only': ['GET'], + 'xml_only': ['GET'] + } + books = BooksController() @wsmeext.pecan.wsexpose([Author], [six.text_type], [Criterion]) @@ -129,3 +134,11 @@ class AuthorsController(RestController): @wsmeext.pecan.wsexpose(Book, int, body=Author) def put(self, author_id, author=None): return author + + @wsmeext.pecan.wsexpose([Author], rest_content_types=('json',)) + def json_only(self): + return [Author(id=1, firstname=u"aname", books=[])] + + @wsmeext.pecan.wsexpose([Author], rest_content_types=('xml',)) + def xml_only(self): + return [Author(id=1, firstname=u"aname", books=[])] diff --git a/tests/pecantest/test/tests/test_ws.py b/tests/pecantest/test/tests/test_ws.py index 2bc484c..d4027b4 100644 --- a/tests/pecantest/test/tests/test_ws.py +++ b/tests/pecantest/test/tests/test_ws.py @@ -194,6 +194,24 @@ class TestWS(FunctionalTest): assert a['faultcode'] == 'Server' assert a['debuginfo'].startswith('Traceback (most recent call last):') + def test_json_only(self): + res = self.app.get('/authors/json_only.json') + assert res.status_int == 200 + body = json.loads(res.body.decode('utf-8')) + assert len(body) == 1 + assert body[0]['firstname'] == u"aname" + assert body[0]['books'] == [] + assert body[0]['id'] == 1 + res = self.app.get('/authors/json_only.xml', expect_errors=True) + + def test_xml_only(self): + res = self.app.get('/authors/xml_only.xml') + assert res.status_int == 200 + assert '1' in res.body.decode('utf-8') + assert 'aname' in res.body.decode('utf-8') + assert '' in res.body.decode('utf-8') + res = self.app.get('/authors/xml_only.json', expect_errors=True) + def test_body_parameter(self): res = self.app.put( '/authors/1/books/2.json', diff --git a/wsme/api.py b/wsme/api.py index c0ec72e..2321d97 100644 --- a/wsme/api.py +++ b/wsme/api.py @@ -112,10 +112,11 @@ class FunctionDefinition(object): arg.resolve_type(registry) def set_options(self, body=None, ignore_extra_args=False, status_code=200, - **extra_options): + rest_content_types=('json', 'xml'), **extra_options): self.body_type = body self.status_code = status_code self.ignore_extra_args = ignore_extra_args + self.rest_content_types = rest_content_types self.extra_options = extra_options def set_arg_types(self, argspec, arg_types): diff --git a/wsmeext/pecan.py b/wsmeext/pecan.py index 41c0334..78871d8 100644 --- a/wsmeext/pecan.py +++ b/wsmeext/pecan.py @@ -116,9 +116,11 @@ def wsexpose(*args, **kwargs): result=result ) - pecan_xml_decorate(callfunction) - pecan_text_xml_decorate(callfunction) - pecan_json_decorate(callfunction) + if 'xml' in funcdef.rest_content_types: + pecan_xml_decorate(callfunction) + pecan_text_xml_decorate(callfunction) + if 'json' in funcdef.rest_content_types: + pecan_json_decorate(callfunction) pecan.util._cfg(callfunction)['argspec'] = inspect.getargspec(f) callfunction._wsme_definition = funcdef return callfunction diff --git a/wsmeext/sphinxext.py b/wsmeext/sphinxext.py index 90bdb6c..0ffdeb1 100644 --- a/wsmeext/sphinxext.py +++ b/wsmeext/sphinxext.py @@ -449,7 +449,9 @@ def document_function(funcdef, docstrings=None, protocols=['restjson']): u'', ]) codesamples.extend(( - u' ' * 12 + line for line in sample.split('\n'))) + u' ' * 12 + line + for line in six.text_type(sample).split('\n') + )) if funcdef.return_type: codesamples.extend([ @@ -467,7 +469,9 @@ def document_function(funcdef, docstrings=None, protocols=['restjson']): u'', ]) codesamples.extend(( - u' ' * 12 + line for line in sample.split('\n'))) + u' ' * 12 + line + for line in six.text_type(sample).split('\n') + )) docstrings[0:0] = [codesamples] return docstrings