Skip to content

adding meta ordered preserves order. #43

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

Open
wants to merge 3 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
7 changes: 5 additions & 2 deletions marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import uuid
import decimal

from collections import OrderedDict


import marshmallow
from marshmallow import fields, missing, Schema, validate
from marshmallow.class_registry import get_class
Expand Down Expand Up @@ -108,9 +111,9 @@ def _get_default_mapping(self, obj):

def get_properties(self, obj):
"""Fill out properties field."""
properties = {}
properties = OrderedDict()

for field_name, field in sorted(obj.fields.items()):
for field_name, field in obj.fields.items():
schema = self._get_schema_for_field(obj, field)
properties[field.name] = schema

Expand Down
17 changes: 17 additions & 0 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ def test_dump_schema():
for field_name, field in schema.fields.items():
assert field_name in props

def test_dump_ordered():
"""Order should be preserved."""

class AddressOrdered(Address):
"""Just set ordered meta field."""

class Meta: # noqa
ordered = True

schema = AddressOrdered()
json_schema = JSONSchema()
dumped = json_schema.dump(schema).data
assert (
list(dumped['definitions']['AddressOrdered']['properties'].keys()) ==
['id', 'street', 'number', 'city', 'floor']
)

def test_default():
schema = UserSchema()
json_schema = JSONSchema()
Expand Down