apispec icon indicating copy to clipboard operation
apispec copied to clipboard

Allow map_to_openapi_type to be called like a function

Open kasium opened this issue 3 years ago • 3 comments

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?

kasium avatar Feb 04 '22 07:02 kasium

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.

lafrech avatar Feb 10 '22 22:02 lafrech

What do you think about create_type_mapping

kasium avatar Feb 11 '22 06:02 kasium

The more I think of it, the more I lean towards just removing the decorator form.

Let's do that in 6.0.

lafrech avatar Feb 11 '22 09:02 lafrech

@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 avatar Oct 04 '22 13:10 lafrech

@lafrech sounds great. I can create a PR in about two weeks. Does that work for you?

kasium avatar Oct 04 '22 14:10 kasium

@kasium, see #804.

lafrech avatar Oct 11 '22 22:10 lafrech

Thanks, looks fine to me

kasium avatar Oct 12 '22 12:10 kasium