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

ModelSchema doesn't respect class Meta: ordered=True #52

Closed
jeffwidman opened this issue Dec 29, 2015 · 1 comment

Comments

@jeffwidman
Copy link

commented Dec 29, 2015

When I use ModelSchema, and set ordered=True in class Meta, the output isn't the same order as my schema fields:

Demo:

import marshmallow_sqlalchemy as mas
class GearReviewSchema(mas.ModelSchema): 
      class Meta:
          ordered = True

      # schema fields... 

gear_review_schema = GearReviewSchema()

review = db.session.query(GearReview).get(3)

gear_review_schema.dump(review) 
# BUG: generates an OrderedDict, but ordering is different than schema fields

However, when I use the normal Schema, it works as expected:

import marshmallow as ma
class GearReviewSchema(ma.Schema):
    class Meta:
        ordered = True

    # schema fields... 

gear_review_schema = GearReviewSchema()

review = db.session.query(GearReview).get(3)

gear_review_schema.dump(review)
# generates an OrderedDict, with ordering same as schema fields

I first noticed this issue using Flask-Marshmallow alongside Marshmallow-SQLAlchemy, but after playing around with it, it looks like the problem is located in Marshmallow-SQLAlchemy.

jmcarp added a commit to jmcarp/marshmallow-sqlalchemy that referenced this issue Dec 30, 2015

jmcarp added a commit to jmcarp/marshmallow-sqlalchemy that referenced this issue Dec 30, 2015

@jeffwidman

This comment has been minimized.

Copy link
Author

commented Dec 30, 2015

BTW, for anyone else who stumbles on this via Google, if you're hitting this with Flask-Marshmallow even after the fix has been merged, it's because by default Flask jsonify orders the keys. You'll need to set JSON_SORT_KEYS=False in your application config in order for the ordered keys to propagate through Flask.

jmcarp added a commit to jmcarp/marshmallow-sqlalchemy that referenced this issue Dec 31, 2015

@sloria sloria closed this in #53 Dec 31, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.