Skip to content

Commit

Permalink
Change Schema.jsonify to take the same args as Schema.dump
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria committed Apr 29, 2015
1 parent 480f8e0 commit e1e0c02
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Changelog
0.6.0 (unreleased)
******************

Features:

- ``Schema.jsonify`` now takes the same arguments as ``marshmallow.Schema.dump``. Additional keywork arguments are passed to ``flask.jsonify``.


Deprecation/Removal:

- Remove support for ``MARSHMALLOW_DATEFORMAT`` and ``MARSHMALLOW_STRICT`` config options.
Expand Down
5 changes: 3 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ Output the data in your views.
all_users = User.all()
result = users_schema.dump(all_users)
return jsonify(result.data)
# OR
# return user_schema.jsonify(all_users)
@app.route('/api/users/<id>')
def user_detail(id):
user = User.get(id)
result = user_schema.dump(user)
return jsonify(result.data)
return user_schema.jsonify(user)
# {
# "email": "fred@queen.com",
# "date_created": "Fri, 25 Apr 2014 06:02:56 -0000",
Expand Down
21 changes: 12 additions & 9 deletions flask_marshmallow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,20 @@ class Schema(BaseSchema):
http://marshmallow.readthedocs.org/en/latest/api_reference.html#serializer
"""

def jsonify(self, *args, **kwargs):
"""Return a JSON response of the serialized data.
def jsonify(self, obj, many=False, *args, **kwargs):
"""Return a JSON response containing the serialized data.
.. deprecated:: 0.4.0
:param obj: Object to serialize.
:param bool many: Set to `True` if `obj` should be serialized as a collection.
:param kwargs: Additional keyword arguments passed to `flask.jsonify`.
.. versionchanged:: 0.6.0
Takes the same arguments as `marshmallow.Schema.dump`. Additional
keyword arguments are passed to `flask.jsonify`.
"""
warnings.warn(
'Schema.jsonify is deprecated. Call jsonify on the '
'output of Schema.dump instead.',
category=DeprecationWarning
)
return jsonify(self.data, *args, **kwargs)
data = self.dump(obj, many=many).data
return jsonify(data, *args, **kwargs)

class Marshmallow(object):
"""Wrapper class that integrates Marshmallow with a Flask application.
Expand Down
7 changes: 2 additions & 5 deletions test_flask_marshmallow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import mock
import pytest

from flask import Flask, url_for
Expand Down Expand Up @@ -205,11 +204,9 @@ def test_schema(app, mockauthor):
assert links['self'] == url_for('author', id=mockauthor.id)
assert links['collection'] == url_for('authors')

@pytest.mark.skipif(IS_MARSHMALLOW_2, reason='jsonify will not work with marshmallow 2 '
'because Schema.data was removed')
def test_jsonify(app, mockauthor):
s = AuthorSchema(mockauthor)
resp = s.jsonify()
s = AuthorSchema()
resp = s.jsonify(mockauthor)
assert isinstance(resp, BaseResponse)
assert resp.content_type == 'application/json'

Expand Down

0 comments on commit e1e0c02

Please sign in to comment.