apispec icon indicating copy to clipboard operation
apispec copied to clipboard

Incorrect handling of nullable enums in marshmallow converter

Open pmdarrow opened this issue 3 years ago • 1 comments

According to the OpenAPI specification, nullable enums must list null in the allowed options. apispec doesn't do this by default, so in my projects I've had to patch this behaviour in with the following attribute function:

def field_to_properties(self, field, ret):
    enum = ret.get('enum', [])
    nullable = ret.get('nullable', False)
    if enum and nullable and None not in enum:
        return {'enum': [*enum, None]}
    return {}

It would be nice if apispec implemented this.

pmdarrow avatar Nov 22 '22 19:11 pmdarrow

Hi Peter. Thanks for reporting.

This also applies to the new Enum field (enum2properties).

This should be a relatively easy fix. Anybody willing to look into it, please go ahead.

lafrech avatar Nov 23 '22 22:11 lafrech

fixed by #888

sloria avatar Feb 26 '24 19:02 sloria