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

UnsupportedValueError on custom marshmallow.fields inside schema #157

Open
pingsutw opened this issue Oct 13, 2021 · 0 comments
Open

UnsupportedValueError on custom marshmallow.fields inside schema #157

pingsutw opened this issue Oct 13, 2021 · 0 comments

Comments

@pingsutw
Copy link

Fail to dump JSON SCHEMA when there is a custom field inside the scheme.
To reproduce:

class PinCode(fields.Field):
    """Field that serializes to a string of numbers and deserializes
    to a list of numbers.
    """
    def _serialize(self, value, attr, obj, **kwargs):
        if value is None:
            return ""
        return "".join(str(d) for d in value)

    def _deserialize(self, value, attr, data, **kwargs):
        return [int(c) for c in value]

class UserSchema(Schema):
    name = fields.String()
    email = fields.String()
    pin_code = PinCode()

bowie = dict(name="David Bowie", email="[email protected]", pin_code="1234")
schema = UserSchema()
pprint(schema.fields)
data = schema.dump(bowie)
result = schema.load(data)
print(JSONSchema().dump(schema)) # <- Error occurs here.

Error message:

    return self._serialize(value, attr, obj, **kwargs)
  File "/Users/kevin/opt/anaconda3/envs/flyte/lib/python3.8/site-packages/marshmallow/fields.py", line 1864, in _serialize
    return self._serialize_method(obj)
  File "/Users/kevin/opt/anaconda3/envs/flyte/lib/python3.8/site-packages/marshmallow_jsonschema/base.py", line 163, in get_properties
    schema = self._get_schema_for_field(obj, field)
  File "/Users/kevin/opt/anaconda3/envs/flyte/lib/python3.8/site-packages/marshmallow_jsonschema/base.py", line 266, in _get_schema_for_field
    pytype = self._get_python_type(field)
  File "/Users/kevin/opt/anaconda3/envs/flyte/lib/python3.8/site-packages/marshmallow_jsonschema/base.py", line 251, in _get_python_type
    raise UnsupportedValueError("unsupported field type %s" % field)
marshmallow_jsonschema.exceptions.UnsupportedValueError: unsupported field type <fields.PinCode(dump_default=<marshmallow.missing>, attribute=None, validate=None, required=False, load_only=False, dump_only=False, load_default=<marshmallow.missing>, allow_none=False, error_messages={'required': 'Missing data for required field.', 'null': 'Field may not be null.', 'validator_failed': 'Invalid value.'})>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant