-
Notifications
You must be signed in to change notification settings - Fork 34
Add support for Apispec / OpenApi #24
Comments
I'm +1 on this, I'd accept that PR too. One thing it'd need to keep in mind is that the field might expect the enum values to be either their names OR their values and it could change depending on if things are being dumped or loaded. I've not looked at Apispec so I'm not sure if it supports that particular situation. |
I think fix should looks like this: class ApispecEnumField(ma_enum.EnumField):
def __init__(self, enum_value, by_value=False, load_by=None, dump_by=None, error='', *args, **kwargs):
super().__init__(enum_value, by_value, load_by, dump_by, error, *args, **kwargs)
if self.load_by == ma_enum.LoadDumpOptions.name:
enum_meta = [e.name for e in enum_value]
elif self.load_by == ma_enum.LoadDumpOptions.value:
enum_meta = [e.value for e in enum_value]
else:
raise NotImplementedError
self.metadata['enum'] = enum_meta to show correct Enum values in Openapi/Swagger cause we depends on how data will be dumped or loaded PS: i rename |
I added a fork (based on #25) that fixes this issue (while removing the load_by/dump_by ambiguity) at https://github.com/h/marshmallow_enum This issue hasn't been fixed/merged since 2018, so if anyone else needs support for APISpec with marshmallow, simply run:
|
Just an FYI - as of version 3.0 of APISpec there is support for adding custom extensions for converting fields to OpenAPI properties. See the documentation for an example and the api. Instead of changing the internal structure of the |
Hi I also need this feature. Is there any plan to add this feature? @h would you consider adding your fork with new name to pip? |
Thanks @Bangertm, here is what your suggestion gives
@jitka consider this workaround. |
OpenApi spec allows defining enumerations, following the JSON Schema validation. Apispec allows generating such OpenApi spec from Marshmallow schemas.
Currently Apispec does not get enumeration information from MarshmallowEnum. It is easy to add support by doing something like the following in the
__init__
of theEnumField
class:metadata
is a property ofField
that apispec uses to retrieve OpenApi-related fields.Currently I am achieving the same by doing:
Finally I would like to thank you for your package :-)
The text was updated successfully, but these errors were encountered: