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

🔊 [#4927] Add system check for missing allow_blank on non-required charfields #4989

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

stevenbal
Copy link
Contributor

@stevenbal stevenbal commented Jan 6, 2025

Closes #4927

Changes

  • Add system check for missing allow_blank on non-required charfields

Checklist

Check off the items that are completed or not relevant.

  • Impact on features

    • Checked copying a form
    • Checked import/export of a form
    • Config checks in the configuration overview admin page
    • Problem detection in the admin email digest is handled
  • Release management

    • I have labelled the PR as "needs-backport" accordingly
  • I have updated the translations assets (you do NOT need to provide translations)

    • Ran ./bin/makemessages_js.sh
    • Ran ./bin/compilemessages_js.sh
  • Dockerfile/scripts

    • Updated the Dockerfile with the necessary scripts from the ./bin folder
  • Commit hygiene

    • Commit messages refer to the relevant Github issue
    • Commit messages explain the "why" of change, not the how

@stevenbal stevenbal marked this pull request as draft January 6, 2025 15:32
Copy link

codecov bot commented Jan 6, 2025

Codecov Report

Attention: Patch coverage is 83.33333% with 4 lines in your changes missing coverage. Please review.

Project coverage is 96.65%. Comparing base (7f8fd3b) to head (ff6cc90).
Report is 11 commits behind head on master.

Files with missing lines Patch % Lines
src/openforms/utils/checks.py 83.33% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4989      +/-   ##
==========================================
- Coverage   96.66%   96.65%   -0.02%     
==========================================
  Files         761      761              
  Lines       25957    25983      +26     
  Branches     3393     3402       +9     
==========================================
+ Hits        25092    25113      +21     
- Misses        601      603       +2     
- Partials      264      267       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@stevenbal stevenbal force-pushed the feature/4927-serializer-allow-blank-system-check branch from 8fa38e2 to ff6cc90 Compare January 7, 2025 08:21
@stevenbal stevenbal marked this pull request as ready for review January 7, 2025 10:19
@stevenbal
Copy link
Contributor Author

@sergei-maertens should I write a test for this check?

@sergei-maertens
Copy link
Member

@sergei-maertens should I write a test for this check?

No, I don't think so, So you can # pragma: no cover it :)

@register
def check_serializer_non_required_charfield_allow_blank_true(app_configs, **kwargs):
"""
https://github.com/open-formulieren/open-forms/issues/4927
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not make the summary line of a docstring a link to a github issue, always provide a description summary, and you can include the link in the body. But even better, the issue reference in the commit message should be sufficient.

"""
https://github.com/open-formulieren/open-forms/issues/4927

Check for serializers.CharFields that have required=False, but not `allow_blank=True`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Check for serializers.CharFields that have required=False, but not `allow_blank=True`
Check for serializers.CharFields that have ``required=False``, but not ``allow_blank=True``

https://github.com/open-formulieren/open-forms/issues/4927

Check for serializers.CharFields that have required=False, but not `allow_blank=True`
to avoid errors occurring when empty strings are passed from the frontend.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
to avoid errors occurring when empty strings are passed from the frontend.
to avoid bogus validation errors occurring when empty strings are provided by the frontend.

errors = []
serializers = get_subclasses(Serializer)
for serializer in serializers:
# TODO not sure if this issue can also occur for fields defined in Meta.fields?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it respects the model field and blank=False will result in required=True

serializers = get_subclasses(Serializer)
for serializer in serializers:
# TODO not sure if this issue can also occur for fields defined in Meta.fields?
for field_name, field in serializer._declared_fields.items():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could consider using drf_spectacular.plumbing.force_instance to get serializer instances, and then you can operate on serializer.fields instead?

Comment on lines +124 to +125
file_path = inspect.getfile(serializer)
line_number = get_field_line_number(serializer, field_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can use serializer_class.__module__ and serializer_class.__qualname__ to get the full python path instead to point to it, instead of trying to figure out line numbers etc.?

Comment on lines +127 to +129
# No line number indicates that the field is inherited from another class
if not file_path or not line_number:
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a problem?

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

Successfully merging this pull request may close these issues.

Add system check for missing allow_blank=True in specific serializer fields
2 participants