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

Fix DisplayOptions Serializer to validate display options when updating a column #1297

Merged
merged 6 commits into from
Apr 27, 2022

Conversation

silentninja
Copy link
Contributor

@silentninja silentninja commented Apr 19, 2022

Related to #1228

When a column is partially updated with an invalid display option, it does not get validated and the request succeeds due to weird behaviour with the DRF serializer which treats nested data as a partial update too, but we completely replace the display options object when saving it, so we end up with a scenario where a required field could be missing. So when the same object is requested by the API, the serializer throws up an error when looking up for the required field in the display options JSON object. This PR fixes it by validating display options during a partial update

Technical Details

During a partial update of a parent serializer, drf calls the nested serializer with partial flag even if we haven't explicitly specified it. We consider update to display options as a full update but since DRF takes a partial flag from the parent serializer, the nested display options serializer gets treated as a partial update, so the serializer becomes valid even if a required field is missing. A mixin was used to fix the behaviour for the individual display options serializer but it wasn't applied to the mapping serializer. This PR adds the mixin that fixes the behaviour of the DRF serializer to the mapping serializer.

Screenshots

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the master branch of the repository
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no
    visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@silentninja silentninja requested review from a team and mathemancer and removed request for a team April 19, 2022 13:46
@silentninja
Copy link
Contributor Author

silentninja commented Apr 19, 2022

@pavish This PR adds validation for display options when partially updating a column serializer. Based on the sample requests you had mentioned in issue #1228, I am assuming you would be sending empty object even if the column type has the required display options. Can you open a separate PR to address the issue?

@codecov-commenter
Copy link

codecov-commenter commented Apr 19, 2022

Codecov Report

Merging #1297 (4336a9b) into master (b68e6de) will increase coverage by 0.15%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #1297      +/-   ##
==========================================
+ Coverage   93.49%   93.65%   +0.15%     
==========================================
  Files         114      114              
  Lines        4426     4426              
==========================================
+ Hits         4138     4145       +7     
+ Misses        288      281       -7     
Flag Coverage Δ
pytest-backend 93.65% <ø> (+0.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
mathesar/api/serializers/shared_serializers.py 87.67% <ø> (+9.58%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b68e6de...4336a9b. Read the comment docs.

@pavish
Copy link
Member

pavish commented Apr 19, 2022

@silentninja

I am assuming you would be sending empty object even if the column type has the required display options

I do not understand this. While patching a column, I'd send an empty object if the user has not set any display options even if the column allows display options and has required ones. See Kriti's comment here.

Based on the sample requests you had mentioned in issue #1228

Also the sample requests mentioned in #1228 contain values for all the required fields.

@silentninja
Copy link
Contributor Author

silentninja commented Apr 19, 2022

Also the sample requests mentioned in #1228 contain values for all the required fields.

Sorry got confused with issue #1276 scenario 2, please ignore that message. But this PR should solve the invalid state, so now it has to be None or a valid object at the time of validation. I have added a comment to #1276 on how to deal with an empty object.

@kgodey kgodey added the pr-status: review A PR awaiting review label Apr 19, 2022
Copy link
Contributor

@mathemancer mathemancer left a comment

Choose a reason for hiding this comment

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

While I do think this change is needed, and the test looks good, I don't understand how this PR fixes #1228 . Can you add some explanation about that to the description for posterity?

@silentninja
Copy link
Contributor Author

@mathemancer #1228 happens due to a bugs that can lead to invalid data being saved, this PR fixes one of the bugs that can cause the error. I updated the description to mention it is related to #1228 rather being a complete fix. I have also updated the description to mention the cause of the issue and how this PR aims to solve it.

@silentninja silentninja requested a review from mathemancer April 26, 2022 18:38
Copy link
Contributor

@mathemancer mathemancer left a comment

Choose a reason for hiding this comment

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

Okay, thanks for the extra explanation.

@mathemancer mathemancer enabled auto-merge April 27, 2022 04:53
@mathemancer mathemancer merged commit 07ef3fd into master Apr 27, 2022
@mathemancer mathemancer deleted the partial-display-options-fix branch April 27, 2022 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-status: review A PR awaiting review
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

5 participants