diff --git a/marshmallow_jsonschema/base.py b/marshmallow_jsonschema/base.py index c7ffbbc..7a29b58 100644 --- a/marshmallow_jsonschema/base.py +++ b/marshmallow_jsonschema/base.py @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/tests/__init__.py b/tests/__init__.py index 829cead..961432b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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() diff --git a/tests/test_dump.py b/tests/test_dump.py index 5a8d1d8..2a77177 100644 --- a/tests/test_dump.py +++ b/tests/test_dump.py @@ -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():