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

fix: use data_key from field if available to set property name #100

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
8 changes: 4 additions & 4 deletions marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def get_properties(self, obj) -> typing.Dict[str, typing.Dict[str, typing.Any]]:

for field_name, field in fields_items_sequence:
schema = self._get_schema_for_field(obj, field)
properties[field.metadata.get("name") or field.name] = schema
properties[field.data_key or field.name] = schema

return properties

Expand All @@ -170,7 +170,7 @@ def get_required(self, obj) -> typing.Union[typing.List[str], _Missing]:

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

return required or missing

Expand Down Expand Up @@ -199,7 +199,7 @@ def _from_python_type(self, obj, field, pytype) -> typing.Dict[str, typing.Any]:
metadata.update(field.metadata)

for md_key, md_val in metadata.items():
if md_key in ("metadata", "name"):
if md_key == "metadata":
continue
json_schema[md_key] = md_val

Expand Down Expand Up @@ -317,7 +317,7 @@ def _from_nested_schema(self, obj, field):
metadata.update(field.metadata)

for md_key, md_val in metadata.items():
if md_key in ("metadata", "name"):
if md_key == "metadata":
continue
schema[md_key] = md_val

Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class UserSchema(Schema):
email = fields.Email()
balance = fields.Decimal()
registered = fields.Boolean()
hair_colors = fields.List(fields.Raw)
hair_colors = fields.List(fields.Raw, data_key="hairColors", required=True)
sex_choices = fields.List(fields.Raw)
finger_count = fields.Integer()
uid = fields.UUID()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_dump_schema():

props = dumped["definitions"]["UserSchema"]["properties"]
for field_name, field in schema.fields.items():
assert field_name in props
assert field.data_key or field_name in props


def test_default():
Expand Down