Skip to content

Commit

Permalink
fix: fixed nil pointer exception after sdk update
Browse files Browse the repository at this point in the history
  • Loading branch information
demeyerthom committed Sep 3, 2024
1 parent 4f6f213 commit c5f1d90
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Fixed-20240903-091918.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: Fixed shipping_zone_rate freeAbove handling when empty
time: 2024-09-03T09:19:18.036808639+02:00
23 changes: 15 additions & 8 deletions commercetools/resource_shipping_zone_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func resourceShippingZoneRateDelete(ctx context.Context, d *schema.ResourceData,
func createShippingRateDraft(rate *platform.ShippingRate) *platform.ShippingRateDraft {
var freeAbove *platform.Money
if rate.FreeAbove != nil {
m := coerceTypedMoney(rate.FreeAbove)
m := coerceTypedMoney(*rate.FreeAbove)
freeAbove = &m
}

Expand Down Expand Up @@ -444,13 +444,20 @@ func setShippingZoneRateState(d *schema.ResourceData, shippingMethod *platform.S
return err
}

freeAbove := map[string]any{
"currency_code": shippingRate.FreeAbove.CurrencyCode,
"cent_amount": shippingRate.FreeAbove.CentAmount,
}
err = d.Set("free_above", []any{freeAbove})
if err != nil {
return err
if shippingRate.FreeAbove != nil {
freeAbove := map[string]any{
"currency_code": shippingRate.FreeAbove.CurrencyCode,
"cent_amount": shippingRate.FreeAbove.CentAmount,
}
err = d.Set("free_above", []any{freeAbove})
if err != nil {
return err
}
} else {
err = d.Set("free_above", nil)
if err != nil {
return err
}
}
return nil
}
Expand Down
11 changes: 2 additions & 9 deletions commercetools/resource_shipping_zone_rate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ func TestAccShippingZoneRate_createAndUpdate(t *testing.T) {
CheckDestroy: testAccCheckShippingZoneRateDestroy,
Steps: []resource.TestStep{
{
Config: testAccShippingZoneRateConfig(taxCategoryName, shippingMethodName, "EUR"),
Config: testAccShippingZoneRateCreate(taxCategoryName, shippingMethodName, "EUR"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "price.0.cent_amount", "5000"),
resource.TestCheckResourceAttr(resourceName, "price.0.currency_code", "EUR"),
resource.TestCheckResourceAttr(resourceName, "free_above.0.cent_amount", "50000"),
resource.TestCheckResourceAttr(resourceName, "free_above.0.currency_code", "EUR"),
resource.TestCheckResourceAttr(resourceName, "shipping_rate_price_tier.#", "2"),
resource.TestCheckResourceAttr(resourceName, "shipping_rate_price_tier.0.type", "CartValue"),
resource.TestCheckResourceAttr(resourceName, "shipping_rate_price_tier.0.minimum_cent_amount", "5000"),
Expand Down Expand Up @@ -60,7 +58,7 @@ func TestAccShippingZoneRate_createAndUpdate(t *testing.T) {
})
}

func testAccShippingZoneRateConfig(taxCategoryName string, shippingMethodName string, currencyCode string) string {
func testAccShippingZoneRateCreate(taxCategoryName string, shippingMethodName string, currencyCode string) string {
return hclTemplate(`
resource "commercetools_tax_category" "standard" {
name = "{{ .taxCategoryName }}"
Expand Down Expand Up @@ -93,11 +91,6 @@ func testAccShippingZoneRateConfig(taxCategoryName string, shippingMethodName st
currency_code = "{{ .currencyCode }}"
}
free_above {
cent_amount = 50000
currency_code = "{{ .currencyCode }}"
}
shipping_rate_price_tier {
type = "CartValue"
minimum_cent_amount = 5000
Expand Down

0 comments on commit c5f1d90

Please sign in to comment.