apispec
apispec copied to clipboard
Allow user-defined field types to implement their own attribute function
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.
Hi. Sorry for the laaaaaaaaate reply.
This makes total sense but it is to easy to achieve in user code like you did that I'm lukewarm about adding this API to the lib.
If there was huge popular demand for this, I don't think I'd object. Leaving this open for now in case anyone would like to :+1: or comment.
I missed this before. I tend to think it would be more convenient overall for the people creating custom fields to also control their schema representation. Just not sure that they would want to take that on.