Skip to content

Allow user-defined field types to implement their own attribute function #715

@Meallia

Description

@Meallia

When using user-defined fields ( such as EnumField as requested in #506 or Union from marshmallow-union) it would be nice to be able to hook directly into apispec.ext.marshmallow.field_converter.FieldConverterMixin.field2property by defining a specific method.

I'm currently using this simple code for that purpose:

def customfield2properties(self: OpenAPIConverter, field: fields.Field, ret: Dict[str, Any]) -> Dict[str, Any]:
    """
    A field attribute function to allow field classes to declare
    how to generate/add parameters to their jsonschema representation
    """
    if hasattr(field, "apispec_field2properties"):
        field.apispec_field2properties(self, ret)
    return ret

Once added to the converter with any field defining the apispec_field2properties method can describe itself without having to add a specific attribute_function to the Marshmallow plugin.

Is this something you would be interested in having in apispec? I can work on a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions