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

Field that is subclass of `fields.Int` gets `{type: string}` in the generated yaml. #250

Closed
TheGrandmother opened this issue Jul 17, 2018 · 2 comments

Comments

@TheGrandmother
Copy link

commented Jul 17, 2018

Rolling Python: 3.6.5, marshmallow: 3.0.0b11 and apispec: 0.39.0

I have created a custom field like this:

#custom_fields.py

class Id(fields.Integer):
    def __init__(self, **kw_args):
        if 'validate' not in kw_args or kw_args['validate'] is None:
            kw_args['validate'] = [lambda v: v > 0]
        else:
            kw_args['validate'] = kw_args['validate'].copy().append(
                lambda v: v > 0)
        super(Id, self).__init__(**kw_args)

And i then use it like this:

class Datapoint(Schema):
    uuid = custom_fields.Id(required=True)

And I then add the definition and generate the specs with to_yaml() but then it says:

uuid: {type: string}

This confuses me since the field works as expected otherwise and it will fail the validation will fail if i pass anything but an integer > 0 to the uuid field.

NOTE: I know that there is field.UUID but I, for some unknown reason, won't use it.

@TheGrandmother TheGrandmother changed the title Field that is subclass of `fields.Int` gets `type:string` in the generated swagger. Field that is subclass of `fields.Int` gets `type:string` in the generated yaml. Jul 17, 2018

@TheGrandmother TheGrandmother changed the title Field that is subclass of `fields.Int` gets `type:string` in the generated yaml. Field that is subclass of `fields.Int` gets `{type: string}` in the generated yaml. Jul 17, 2018

@lafrech

This comment has been minimized.

Copy link
Member

commented Jul 17, 2018

You should register your custom Field first.

https://apispec.readthedocs.io/en/stable/api_ext.html#apispec.ext.marshmallow.MarshmallowPlugin.map_to_openapi_type

@ma_plugin.map_to_openapi_type(Integer)  # will map to ('integer', 'int32')
class Id(fields.Integer):
    ...

@ma_plugin.map_to_openapi_type('integer', 'ID')
class Id(fields.Integer):
    ...
@TheGrandmother

This comment has been minimized.

Copy link
Author

commented Jul 17, 2018

Thanks!
I had no idea :3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.