diff --git a/marshmallow_jsonschema/base.py b/marshmallow_jsonschema/base.py index fbda2aa..507785d 100644 --- a/marshmallow_jsonschema/base.py +++ b/marshmallow_jsonschema/base.py @@ -92,7 +92,7 @@ def get_properties(self, obj): for field_name, field in sorted(obj.fields.items()): if hasattr(field, '_jsonschema_type_mapping'): - schema = field._jsonschema_type_mapping() + schema = self.__class__._from_custom_field_type(field) elif field.__class__ in mapping: pytype = mapping[field.__class__] schema = self.__class__._from_python_type(field, pytype) @@ -166,3 +166,20 @@ def _from_nested_schema(cls, field): } return schema + + @classmethod + def _from_custom_field_type(cls, field): + json_schema = field._jsonschema_type_mapping() + + if field.default is not missing: + json_schema['default'] = field.default + + if field.metadata.get('metadata', {}).get('description'): + json_schema['description'] = ( + field.metadata['metadata'].get('description') + ) + + if field.metadata.get('metadata', {}).get('title'): + json_schema['title'] = field.metadata['metadata'].get('title') + + return json_schema diff --git a/tests/test_dump.py b/tests/test_dump.py index 039c72f..c09a3de 100644 --- a/tests/test_dump.py +++ b/tests/test_dump.py @@ -186,9 +186,18 @@ def _serialize(self, value, attr, obj): class UserSchema(Schema): name = fields.String(required=True) - favourite_colour = Colour() + favourite_colour = Colour(default='#ffffff', + metadata=dict( + description="User's favorite colour", + title="Colour") + ) schema = UserSchema() json_schema = JSONSchema() dumped = json_schema.dump(schema).data - assert dumped['properties']['favourite_colour'] == {'type': 'string'} + assert dumped['properties']['favourite_colour'] == { + 'type': 'string', + 'default': '#ffffff', + 'description': "User's favorite colour", + 'title': "Colour", + }