Skip to content

Do not create a default metadata["description"] #146

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

Open
mjpieters opened this issue May 27, 2021 · 1 comment
Open

Do not create a default metadata["description"] #146

mjpieters opened this issue May 27, 2021 · 1 comment

Comments

@mjpieters
Copy link

The metadata["description"] field is used when using the apispec project to generate OpenAPI specifications. Marshmallow_dataclass sets this field to the type name when using NewType and that clashes, as I don't need nor want to give every field a description in the spec.

Can we please decide for ourselves if description should be set?

@mjpieters
Copy link
Author

I currently use this workaround, a class decorator I place above @marshmallow_dataclasses.dataclass:

from typing import TypeVar

T = TypeVar("T")

# slight annoyance: marshmallow-dataclasses sets a default `metadata["description"]`
# value. This class decorator removes descriptions set to None, and I set
# description: None on NewType fields to prevent the default values being set.
def _no_default_description(cls: T, _sentinel=object()) -> T:
    for field in cls.Schema._declared_fields.values():
        if field.metadata.get("description", _sentinel) is None:
            del field.metadata["description"]
    return cls

e.g.

from marshmallow_dataclass import NewType, dataclass

CustomType = NewType(
    "CustomType", BaseType, field=CustomField, metadata={"description": None}
)

@_no_default_description
@dataclass
class SomeSchema:
    some_field: CustomType

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

No branches or pull requests

1 participant