apispec
apispec copied to clipboard
Allow map_to_openapi_type to be called like a function
Hi,
so I have a schema which uses a custom field. Currently you expect that we do the following:
ma = MarshmallowPlugin()
@ma.map_to_openapi_type(fields.Dict)
class Custom(field.Field): pass
However, it's not very nice if the fields are in separate files since you always need the MarshmallowPlugin
instance. As a workaround, I figured out, that the decorator doesn't really change something in the class, therefore I can also write:
ma.map_to_openapi_type(fields.Dict)(Custom)
. However, still not very nice syntax.
Therefore I would like to propose a new function which just takes the two args: ma.map_to_openapi_type2(Custom, fileds,Dict)
(name can be improved 😄 ). This can then be called whereever the MarshmallowPlugin
is created.
What do you think?
I see what you mean.
This is more or less what I do in flask-smorest: https://github.com/marshmallow-code/flask-smorest/blob/ec4fbecc71686535a38f8642760bab390f0fdc44/flask_smorest/spec/init.py#L269-L302.
The form
ma.map_to_openapi_type(fields.Dict)(Custom)
is not that terrible. Agreed it looks strange.
If we came up with a correct name, I wouldn't object to adding another function.
Or in the next major release we just drop the decorator form. It is not that useful since the class is not modified.
What do you think about create_type_mapping
The more I think of it, the more I lean towards just removing the decorator form.
Let's do that in 6.0.
@kasium, v6 is on its way so let's drop the decorator form and change it to function form.
You may send a PR if you want or I'll do it when I get the time.
@lafrech sounds great. I can create a PR in about two weeks. Does that work for you?
@kasium, see #804.
Thanks, looks fine to me