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

Provide option to use data keys as property name and title #65

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
28 changes: 21 additions & 7 deletions marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

from .validation import handle_length, handle_one_of, handle_range


__all__ = (
'JSONSchema',
)


TYPE_MAP = {
dict: {
'type': 'object',
Expand Down Expand Up @@ -72,7 +70,6 @@
},
}


FIELD_VALIDATORS = {
validate.Length: handle_length,
validate.OneOf: handle_one_of,
Expand All @@ -91,6 +88,7 @@ def __init__(self, *args, **kwargs):
"""Setup internal cache of nested fields, to prevent recursion."""
self._nested_schema_classes = {}
self.nested = kwargs.pop('nested', False)
self.prefer_data_key = kwargs.pop('prefer_data_key', False)
super(JSONSchema, self).__init__(*args, **kwargs)

def _get_default_mapping(self, obj):
Expand All @@ -112,7 +110,7 @@ def get_properties(self, obj):

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

return properties

Expand All @@ -122,16 +120,18 @@ def get_required(self, obj):

for field_name, field in sorted(obj.fields.items()):
if field.required:
required.append(field.name)
required.append(
self._get_property_name_for_field(field)
)

return required or missing

def _from_python_type(self, obj, field, pytype):
"""Get schema definition from python type."""
json_schema = {
'title': field.attribute or field.name,
'title': field.attribute or self._get_property_name_for_field(
field),
}

for key, val in TYPE_MAP[pytype].items():
json_schema[key] = val

Expand Down Expand Up @@ -182,6 +182,20 @@ def _get_schema_for_field(self, obj, field):
)
return schema

def _get_property_name_for_field(self, field):
"""Get property name for field based on serialized object"""
name = field.name

if self.prefer_data_key:
# Handle change in load_from / dump_to between Marshmallow
# versions 2 and 3.
if marshmallow.__version__.split('.', 1)[0] >= '3':
name = field.data_key or name
else:
name = field.load_from or field.dump_to or name

return name

def _from_nested_schema(self, obj, field):
"""Support nested field."""
if isinstance(field.nested, basestring):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def read(fname):
package_dir={'marshmallow-jsonschema': 'marshmallow-jsonschema'},
include_package_data=True,
install_requires=['marshmallow>=2.9.0'],
tests_require=['pytest>=2.9.2', 'jsonschema', 'strict-rfc3339', 'coverage>=4.1'],
tests_require=['pytest>=2.9.2', 'jsonschema<=2.5.1', 'strict-rfc3339', 'coverage>=4.1'],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh boy - what's all this now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like 11 tests failed with jsonschema==2.6.0 - I can check again and validate.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this - tests work without the change.

license=read('LICENSE'),
zip_safe=False,
keywords=('marshmallow-jsonschema marshmallow schema serialization '
Expand Down
Loading