Skip to content

Commit

Permalink
Allow this to work with nested schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
s0undt3ch committed Nov 25, 2017
1 parent 2b82154 commit 1f7c1de
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _from_nested_schema(self, obj, field):
# If this is not a schema we've seen, and it's not this schema,
# put it in our list of schema defs
if name not in self._nested_schema_classes and name != outer_name:
wrapped_nested = JSONSchema(nested=True)
wrapped_nested = self.__class__(nested=True)
wrapped_dumped = wrapped_nested.dump(
nested()
)
Expand Down
34 changes: 34 additions & 0 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,37 @@ def get_custom_mappings(self):
'default': 'red',
'title': 'favourite_colour',
'type': 'string'}


def test_nested_custom_field_by_subclassing():

class Colour(fields.String):
def __init__(self, default='red', **kwargs):
super(Colour, self).__init__(default=default, **kwargs)

class ColoursSchema(Schema):
favourite_colour = Colour()

class UserSchema(Schema):
name = fields.String(required=True)
colours = fields.Nested(ColoursSchema)

schema = UserSchema()
json_schema = JSONSchema()

with pytest.raises(ValueError):
# The custom Color field is not registered
dumped = json_schema.dump(schema)

# Provide the custom field to the default mappings
class CustomJSONSchema(JSONSchema):
def get_custom_mappings(self):
return {Colour: text_type}

# Register the field
json_schema = CustomJSONSchema()
dumped = json_schema.dump(schema).data
assert dumped['definitions']['ColoursSchema']['properties']['favourite_colour'] == {
'default': 'red',
'title': 'favourite_colour',
'type': 'string'}

0 comments on commit 1f7c1de

Please sign in to comment.