You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the DRF code, when deserializing a JSON object with a value for currency and None for the amount, there is an InvalidOperation exception instantiating the Money object.
def get_value(self, data):
amount = super(MoneyField, self).get_value(data)
currency = data.get(get_currency_field_name(self.field_name), None)
if currency: # Missing a check for amount is None.
return Money(amount, currency)
return amount
The fix is to also check that the amount is not None.
if currency and amount is not None:
return Money(amount, currency)
Here is a test to expose the error:
@pytest.mark.parametrize(
'body, expected', (
({'field': '10', 'field_currency': 'EUR'}, Money(10, 'EUR')),
({'field': '12.20', 'field_currency': 'GBP'}, Money(12.20, 'GBP')),
({'field': '15.15', 'field_currency': 'USD'}, Money(15.15, 'USD')),
({'field': None, 'field_currency': 'USD'}, None), # Add this line for amount is None and currency is not None.
),
)
def test_post_put_values(self, body, expected):
serializer = self.get_serializer(NullMoneyFieldModel, data=body)
serializer.is_valid()
assert serializer.validated_data['field'] == expected
I will submit a pull request with the test and fix.
Thanks
The text was updated successfully, but these errors were encountered:
carvincarl
changed the title
django rest framework to_internal_value with amount=None and currency is not None throws an InvalidOperation exception
django rest framework get_value with amount=None and currency is not None throws an InvalidOperation exception
Jan 9, 2019
carvincarl
added a commit
to carvincarl/django-money
that referenced
this issue
Jan 10, 2019
In the DRF code, when deserializing a JSON object with a value for currency and None for the amount, there is an InvalidOperation exception instantiating the Money object.
Example stack trace:
Here is the method with the problem:
The fix is to also check that the amount is not None.
Here is a test to expose the error:
I will submit a pull request with the test and fix.
Thanks
The text was updated successfully, but these errors were encountered: