From 4832b2be25786c173bebc3369f5f1e2350e4add0 Mon Sep 17 00:00:00 2001 From: Johan Groth Date: Wed, 11 Dec 2019 13:38:21 +0100 Subject: [PATCH 1/3] fix: use data_key from field if available to set property name Marshmallow already has a way to customize the name of fields when serializing through the data_key property. The JSONSchema should use the same property when serializing the schema to json instead of inventing its own thing. --- marshmallow_jsonschema/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/marshmallow_jsonschema/base.py b/marshmallow_jsonschema/base.py index c7ffbbc..c8fe38c 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 @@ -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 From 0a0856b3df93132e5ff5ee666a7c4fd5657697bc Mon Sep 17 00:00:00 2001 From: Johan Groth Date: Thu, 12 Dec 2019 11:12:28 +0100 Subject: [PATCH 2/3] fix: handle required fields correctly when using data_key --- marshmallow_jsonschema/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/marshmallow_jsonschema/base.py b/marshmallow_jsonschema/base.py index c8fe38c..7a29b58 100644 --- a/marshmallow_jsonschema/base.py +++ b/marshmallow_jsonschema/base.py @@ -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 From 160c85aa688c8dae80a0260e96d9381f9e7c68d8 Mon Sep 17 00:00:00 2001 From: Johan Groth Date: Thu, 12 Dec 2019 11:30:05 +0100 Subject: [PATCH 3/3] test: update test with data_key --- tests/__init__.py | 2 +- tests/test_dump.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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():