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: make nullable django fields also Noneable in event data #394

Merged
merged 1 commit into from
Sep 17, 2024

Conversation

pwnage101
Copy link
Contributor

This should hopefully fix the error we are currently seeing when attempting to serialize real-life LC fulfillments outside of unit test environments. The error is exactly described in this ticket for a different event experiencing the same issue:

edx/edx-arch-experiments#788

Basically, certain fields are passed into the serializer as None because the actual value in the database is NULL, but the serializer field default is not explicitly set to None which triggers a code path that doesn't tolerate being passed None:

if field.default is None:
return None
else:
# Bail out early with an informative message rather
# than ending up with an inscrutable error from inside
# a custom serializer.
#
# If we ever make a custom serializer that can handle
# None as an input, we can remove this check.
# pylint: disable-next=broad-exception-raised
raise Exception("None cannot be handled by custom serializers (and default=None was not set)")

ENT-9213

@pwnage101 pwnage101 requested a review from a team as a code owner September 11, 2024 05:35
@pwnage101 pwnage101 force-pushed the pwnage101/ENT-9213-2 branch 2 times, most recently from 99ba749 to ace9540 Compare September 11, 2024 05:53
@pwnage101
Copy link
Contributor Author

pwnage101 commented Sep 11, 2024

@mariajgrimaldi Please take a look when you have a chance!

Copy link
Member

@mariajgrimaldi mariajgrimaldi left a comment

Choose a reason for hiding this comment

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

This looks good. Let's hope no other events raise the same errors. I'll make a mental note to look for these edge cases when reviewing. Thank you so much!

Is this enough to fix the issue described above?

@pwnage101
Copy link
Contributor Author

Thanks for the prompt review! I do want to get this right so now that I think about it, I want to perform additional testing. I was just eager to get this out for review in case of delay, but that was not a concern.

Maybe you can also help answer a question about versioning: Since this is one of the first instances where we're "evolving" an existing event without creating a v2 version of it, do you forsee any possible issues of doing that? Technical issues maybe? This change is theoretically backwards-compatible (only making existing fields more permissive), and I also doubt anybody in the open source community has adopted this event given it's limited use for enterprise purposes and age.

@mariajgrimaldi
Copy link
Member

mariajgrimaldi commented Sep 11, 2024

@pwnage101: That's a good question. As you said, I see these changes as backward compatible too. If these events were adopted elsewhere, defaults must've been handled in some way for them to work, so these changes won't affect current implementations. In this case, documentation will be enough to let adopters know. However, if there was already a default for the attributes, we'll have to discuss the implications of the default change further.

@pwnage101
Copy link
Contributor Author

I just finished testing locally, and indeed it works the way I expected. First here's a reproduction of the error before any changes (data is prepared in a unit test to simulate what we'd pass to send_event()):

(Pdb) from edx_event_bus_kafka.internal.producer import get_signal_serializer
(Pdb) from openedx_events.enterprise.signals import LEARNER_CREDIT_COURSE_ENROLLMENT_REVOKED
(Pdb) serializer = get_signal_serializer(LEARNER_CREDIT_COURSE_ENROLLMENT_REVOKED)
(Pdb) serializer.to_dict({'learner_credit_course_enrollment': data})
*** Exception: None cannot be handled by custom serializers (and default=None was not set)

After changes to make fields allow None:

(Pdb) from edx_event_bus_kafka.internal.producer import get_signal_serializer
(Pdb) from openedx_events.enterprise.signals import LEARNER_CREDIT_COURSE_ENROLLMENT_REVOKED
(Pdb) serializer = get_signal_serializer(LEARNER_CREDIT_COURSE_ENROLLMENT_REVOKED)
(Pdb) serializer.to_dict({'learner_credit_course_enrollment': data})
{'learner_credit_course_enrollment': {'created': '2024-09-11T21:45:51.594917+00:00', 'enterprise_course_enrollment': {'course_id': 'week-beat-through', 'created': '2024-09-11T21:45:51.593861+00:00', 'enterprise_customer_user': {'active': True, 'created': '2024-09-11T21:45:51.586227+00:00', 'enterprise_customer_uuid': '9fb43fcb-a4de-4c60-a578-29b37a01b85b', 'id': 1, 'invite_key': None, 'is_relinkable': True, 'linked': True, 'modified': '2024-09-11T21:45:51.586227+00:00', 'should_inactivate_other_customers': True, 'user_id': 1}, 'id': 1, 'modified': '2024-09-11T21:45:51.593861+00:00', 'saved_for_later': False, 'source_slug': 'enterprise_api', 'unenrolled': None, 'unenrolled_at': None}, 'enterprise_course_entitlement_uuid': None, 'fulfillment_type': 'learner_credit', 'is_revoked': False, 'modified': '2024-09-11T21:45:51.594917+00:00', 'transaction_id': '9cffe896-f52c-4d31-89cb-d5d218261ca8', 'uuid': '38a66051-fcfe-4387-96d5-743c281670cf'}}

@pwnage101
Copy link
Contributor Author

I'm happy with this PR and feel it's ready to merge if you feel the same.

CHANGELOG.rst Outdated Show resolved Hide resolved
@pwnage101 pwnage101 force-pushed the pwnage101/ENT-9213-2 branch 2 times, most recently from ff45dd6 to 11df135 Compare September 12, 2024 16:56
@pwnage101
Copy link
Contributor Author

@mariajgrimaldi please merge at your convenience

@pwnage101
Copy link
Contributor Author

@mariajgrimaldi could you take a look?

CHANGELOG.rst Outdated
@@ -18,6 +18,14 @@ __________



[9.14.1] - 2024-09-12
Copy link
Member

Choose a reason for hiding this comment

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

Could you please update the date?

Copy link
Member

@mariajgrimaldi mariajgrimaldi left a comment

Choose a reason for hiding this comment

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

I'll be merging this today. Thank you!

This should hopefully fix the error we are currently seeing when
attempting to serialize real-life LC fulfillments outside of unit test
environments.  The error is exactly described in this ticket for a
different event experiencing the same issue:

edx/edx-arch-experiments#788

ENT-9213
@mariajgrimaldi mariajgrimaldi merged commit afa83d9 into openedx:main Sep 17, 2024
6 checks passed
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.

3 participants