A continuation of HomeBox the inventory and organization system built for the Home User
fix: handle numeric list_price in upcitemdb response (#1432)
adriane5b0fe22bb76
diff --git a/backend/app/api/handlers/v1/v1_ctrl_product_search.go b/backend/app/api/handlers/v1/v1_ctrl_product_search.goindex 861e0815..5d86d20a 100644--- a/backend/app/api/handlers/v1/v1_ctrl_product_search.go+++ b/backend/app/api/handlers/v1/v1_ctrl_product_search.go@@ -1,6 +1,7 @@package v1import (+ "bytes""encoding/base64""encoding/json""errors"@@ -24,6 +25,50 @@ const (schemeHTTPS = "https")+// flexibleString is a string that can be unmarshaled from either a JSON+// string or a JSON number. upcitemdb.com sometimes returns price fields+// (e.g. list_price, shipping) as numbers rather than strings, which would+// otherwise cause json.Unmarshal to fail on a plain string field.+type flexibleString string++// UnmarshalJSON accepts a JSON string, number, or null and stores the result+// as a Go string. Composite tokens (objects and arrays) are rejected so that+// unexpected shapes surface as clear errors instead of being silently stored+// as their raw representation.+func (f *flexibleString) UnmarshalJSON(data []byte) error {+ trimmed := bytes.TrimSpace(data)+ if len(trimmed) == 0 {+ *f = ""+ return nil+ }++ switch c := trimmed[0]; {+ case c == '"':+ var s string+ if err := json.Unmarshal(trimmed, &s); err != nil {+ return err+ }+ *f = flexibleString(s)+ return nil+ case c == 'n':+ if string(trimmed) != "null" {+ return fmt.Errorf("flexibleString: unexpected token %q", string(trimmed))+ }+ *f = ""+ return nil+ case c == '-' || (c >= '0' && c <= '9'):+ // Validate the token is a well-formed JSON number, then store its literal form.+ var n json.Number+ if err := json.Unmarshal(trimmed, &n); err != nil {+ return err+ }+ *f = flexibleString(n.String())+ return nil+ default:+ return fmt.Errorf("flexibleString: cannot unmarshal %s into string", string(trimmed))+ }+}+type UPCITEMDBResponse struct {Code string `json:"code"`Total int `json:"total"`@@ -44,17 +89,17 @@ type UPCITEMDBResponse struct {HighestRecordedPrice float64 `json:"highest_recorded_price"`Images []string `json:"images"`Offers []struct {- Merchant string `json:"merchant"`- Domain string `json:"domain"`- Title string `json:"title"`- Currency string `json:"currency"`- ListPrice string `json:"list_price"`- Price float64 `json:"price"`- Shipping string `json:"shipping"`- Condition string `json:"condition"`- Availability string `json:"availability"`- Link string `json:"link"`- UpdatedT int `json:"updated_t"`+ Merchant string `json:"merchant"`+ Domain string `json:"domain"`+ Title string `json:"title"`+ Currency string `json:"currency"`+ ListPrice flexibleString `json:"list_price"`+ Price float64 `json:"price"`+ Shipping flexibleString `json:"shipping"`+ Condition string `json:"condition"`+ Availability string `json:"availability"`+ Link string `json:"link"`+ UpdatedT int `json:"updated_t"`} `json:"offers"`Asin string `json:"asin"`Elid string `json:"elid"`diff --git a/backend/app/api/handlers/v1/v1_ctrl_product_search_test.go b/backend/app/api/handlers/v1/v1_ctrl_product_search_test.goindex f383a9de..d5c4263d 100644--- a/backend/app/api/handlers/v1/v1_ctrl_product_search_test.go+++ b/backend/app/api/handlers/v1/v1_ctrl_product_search_test.go@@ -1,6 +1,126 @@package v1-import "testing"+import (+ "encoding/json"+ "testing"+)++func TestUPCITEMDBResponseUnmarshalNumericListPrice(t *testing.T) {+ body := []byte(`{+ "code": "OK",+ "total": 1,+ "offset": 0,+ "items": [{+ "title": "Example",+ "offers": [{+ "merchant": "ExampleStore",+ "list_price": 19.99,+ "price": 14.5,+ "shipping": 4.25+ }]+ }]+ }`)++ var result UPCITEMDBResponse+ if err := json.Unmarshal(body, &result); err != nil {+ t.Fatalf("unexpected unmarshal error: %v", err)+ }++ if len(result.Items) != 1 || len(result.Items[0].Offers) != 1 {+ t.Fatalf("expected one item with one offer, got items=%d", len(result.Items))+ }++ offer := result.Items[0].Offers[0]+ if offer.ListPrice != "19.99" {+ t.Fatalf("expected list_price %q, got %q", "19.99", offer.ListPrice)+ }+ if offer.Shipping != "4.25" {+ t.Fatalf("expected shipping %q, got %q", "4.25", offer.Shipping)+ }+}++func TestUPCITEMDBResponseUnmarshalStringListPrice(t *testing.T) {+ body := []byte(`{+ "items": [{+ "offers": [{+ "list_price": "19.99",+ "shipping": "Free"+ }]+ }]+ }`)++ var result UPCITEMDBResponse+ if err := json.Unmarshal(body, &result); err != nil {+ t.Fatalf("unexpected unmarshal error: %v", err)+ }++ if len(result.Items) == 0 || len(result.Items[0].Offers) == 0 {+ t.Fatalf("expected at least one item with one offer, got items=%d", len(result.Items))+ }++ offer := result.Items[0].Offers[0]+ if offer.ListPrice != "19.99" {+ t.Fatalf("expected list_price %q, got %q", "19.99", offer.ListPrice)+ }+ if offer.Shipping != "Free" {+ t.Fatalf("expected shipping %q, got %q", "Free", offer.Shipping)+ }+}++func TestUPCITEMDBResponseUnmarshalNullListPrice(t *testing.T) {+ body := []byte(`{+ "items": [{+ "offers": [{+ "list_price": null,+ "shipping": null+ }]+ }]+ }`)++ var result UPCITEMDBResponse+ if err := json.Unmarshal(body, &result); err != nil {+ t.Fatalf("unexpected unmarshal error: %v", err)+ }++ if len(result.Items) == 0 || len(result.Items[0].Offers) == 0 {+ t.Fatalf("expected at least one item with one offer, got items=%d", len(result.Items))+ }++ offer := result.Items[0].Offers[0]+ if offer.ListPrice != "" {+ t.Fatalf("expected empty list_price, got %q", offer.ListPrice)+ }+ if offer.Shipping != "" {+ t.Fatalf("expected empty shipping, got %q", offer.Shipping)+ }+}++func TestFlexibleStringRejectsCompositeTypes(t *testing.T) {+ cases := map[string]string{+ "object": `{"foo":"bar"}`,+ "array": `[1,2,3]`,+ "bool": `true`,+ }++ for name, payload := range cases {+ t.Run(name, func(t *testing.T) {+ var f flexibleString+ if err := f.UnmarshalJSON([]byte(payload)); err == nil {+ t.Fatalf("expected error for %s payload, got nil (value=%q)", name, f)+ }+ })+ }+}++func TestFlexibleStringHandlesLeadingWhitespace(t *testing.T) {+ var f flexibleString+ if err := f.UnmarshalJSON([]byte(" 12.50")); err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if f != "12.50" {+ t.Fatalf("expected %q, got %q", "12.50", f)+ }+}func TestBuildOpenFactsBarcodeProduct(t *testing.T) {product, ok := buildOpenFactsBarcodeProduct("openbeautyfacts.org", "3600522058124", openFactsProduct{