Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Meallia opened this issue Sep 24, 2021 · 2 comments
Open

Comments

@Meallia
Copy link

Meallia commented Sep 24, 2021

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.

@lafrech
Copy link
Member

lafrech commented Oct 12, 2022

Hi. Sorry for the laaaaaaaaate reply.

This makes total sense but it is so 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 👍 or comment.

@Bangertm
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants