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

TypeError: Unknown type for marshmallow model field was used. #72

Open
pipozzz opened this issue Apr 18, 2020 · 3 comments
Open

TypeError: Unknown type for marshmallow model field was used. #72

pipozzz opened this issue Apr 18, 2020 · 3 comments

Comments

@pipozzz
Copy link
Contributor

pipozzz commented Apr 18, 2020

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
@apryor6
Copy link
Owner

apryor6 commented Apr 18, 2020

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.

@pipozzz
Copy link
Contributor Author

pipozzz commented Apr 19, 2020

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

@der-joel
Copy link

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

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

3 participants