flask_accepts icon indicating copy to clipboard operation
flask_accepts copied to clipboard

TypeError: Unknown type for marshmallow model field was used.

Open pipozzz opened this issue 4 years ago • 3 comments

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

pipozzz avatar Apr 18 '20 21:04 pipozzz

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.

apryor6 avatar Apr 18 '20 21:04 apryor6

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

pipozzz avatar Apr 19 '20 05:04 pipozzz

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

der-joel avatar Sep 16 '20 13:09 der-joel