flask_accepts
flask_accepts copied to clipboard
TypeError: Unknown type for marshmallow model field was used.
Hi,
I wanted to use custom marshmallow field related to mongo data serialisation
class ObjectIdField(fields.Field):
def _deserialize(self, value, attr, data, **kwargs):
try:
return bson.ObjectId(value)
except Exception:
raise ValidationError('invalid ObjectId `%s`' % value)
def _serialize(self, value, attr, obj, **kwargs):
if value is None:
return missing
return str(value)```
and I'm getting this error: TypeError: Unknown type for marshmallow model field was used.
Could you give me some directions how to deal with this?
Thanks
Custom fields are problematic because flask_accepts wants to convert various marshmallow fields into an equivalent data type in flask_restx for Swagger documentation. The error you are receiving is indicating that there isn’t a known equivalent for Swagger, so your field would not show up in the resulting documentation, unfortunately.
it would be fine if it's not in documentation but it fails so I cannot do validation and marshaling what has higher priority than documentation I think. Could we leave this field undocumented or something what could not break basic functionality?
Sent with GitHawk
A possibility to update the type_map
used in utils.py or provide a custom one would be sufficient.
For Example:
from flask_restx.fields import String
from flask_accepts.utils import type_map_base
type_map = type_map_base.update({ObjectIdField: String})
@api.route("/user")
class CurrentUser(Resource):
@responds(schema=UserSchema, swagger_type_map=type_map)
def get(self):
return request.parsed_obj