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

Prevent hard-errors when VatPriceGenerator is invoked without a defau… #5324

Open
wants to merge 1 commit into
base: v3.4
Choose a base branch
from

Conversation

firemind
Copy link

@firemind firemind commented Aug 9, 2023

Guard against exceptions when calling Price#net_amount without a price.

Skip trying to calculate foreign prices in VatPriceGenerator when the default_price does not have an amount set.

Because the VatPriceGenerator is invoked in a before_validate hook when creating a product, this can lead to exceptions when an admin tries to create a product without specifying a price (and there are non-default countries for which a price needs to be calculated).

This PR allows the validations to run without raising exceptions so the admin can see the validation errors.

@firemind firemind requested a review from a team as a code owner August 9, 2023 13:47
@github-actions github-actions bot added the changelog:solidus_core Changes to the solidus_core gem label Aug 9, 2023
@codecov
Copy link

codecov bot commented Aug 9, 2023

Codecov Report

Merging #5324 (94820f8) into v3.4 (3ef8231) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             v3.4    #5324   +/-   ##
=======================================
  Coverage   86.64%   86.64%           
=======================================
  Files         580      580           
  Lines       14741    14743    +2     
=======================================
+ Hits        12772    12774    +2     
  Misses       1969     1969           
Files Changed Coverage Δ
core/app/models/spree/price.rb 97.72% <100.00%> (+0.05%) ⬆️
...re/app/models/spree/variant/vat_price_generator.rb 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@waiting-for-dev waiting-for-dev added the release:major Breaking change on hold until next major release label Aug 10, 2023
@waiting-for-dev
Copy link
Contributor

I labeled the PR as a breaking change, as it's changing a behavior some people could be relying on. It'll be merged in the next major.

@@ -46,6 +46,8 @@ def price=(price)
end

def net_amount
return nil unless amount
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe it's possible for amount to be nil as of v3.1. (See the "Other important changes" section in the CHANGELOG.md entry for v3.1, or the PR that removes nil as a valid value: #3987)

Which means this shouldn't be possible on recent versions of Solidus. (Assuming the task to remove any records with a nil amount has been run and there isn't any invalid data left in the database from before that change.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, nevermind. I missed that we were trying to fix a before_validation callback on Spree::Variant.

In which case I think the preferred fix should be skipping that callback instead of modifying this public method. The callback already has a conditional associated to it, so I believe it would be a less invasive change to prevent it from running in the case when a price is invalid. (And we avoid any future confusion from having a nil check in a method where a nil value is considered invalid.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem release:major Breaking change on hold until next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants