Skip to content

Commit

Permalink
Merge pull request #18 from appuio/fix-sending-json-null-to-odoo
Browse files Browse the repository at this point in the history
  • Loading branch information
bastjan authored Dec 7, 2023
2 parents 781e23c + 60e63cb commit 098d8bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
14 changes: 12 additions & 2 deletions pkg/odoo/odoo16.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type OdooAPIClient struct {
}

type apiObject struct {
Data []OdooMeteredBillingRecord `json:"data"`
Data ensureJSONArray[OdooMeteredBillingRecord] `json:"data"`
}

type OdooMeteredBillingRecord struct {
Expand Down Expand Up @@ -72,7 +72,7 @@ func NewOdooAPIWithClient(odooURL string, client *http.Client, logger logr.Logge

func (c OdooAPIClient) SendData(ctx context.Context, data []OdooMeteredBillingRecord) error {
apiObject := apiObject{
Data: data,
Data: ensureJSONArray[OdooMeteredBillingRecord](data),
}
str, err := json.Marshal(apiObject)
if err != nil {
Expand All @@ -92,3 +92,13 @@ func (c OdooAPIClient) SendData(ctx context.Context, data []OdooMeteredBillingRe

return nil
}

// ensureJSONArray is a wrapper around any slice that will marshal to an empty array instead of `null` if the array is nil.
type ensureJSONArray[T any] []T

func (a ensureJSONArray[T]) MarshalJSON() ([]byte, error) {
if a == nil {
return []byte("[]"), nil
}
return json.Marshal([]T(a))
}
7 changes: 4 additions & 3 deletions pkg/odoo/odoo16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ func TestOdooRecordsSent(t *testing.T) {
logger := logr.New(logr.Discard().GetSink())
uut := odoo.NewOdooAPIWithClient("https://foo.bar/odoo16/", &client, logger)

err := uut.SendData(context.Background(), []odoo.OdooMeteredBillingRecord{getOdooRecord()})

require.NoError(t, err)
require.NoError(t, uut.SendData(context.Background(), []odoo.OdooMeteredBillingRecord{getOdooRecord()}))
require.Equal(t, `{"data":[{"product_id":"my-product","instance_id":"my-instance","item_description":"my-description","item_group_description":"my-group","sales_order_id":"SO00000","unit_id":"my-unit","consumed_units":11.1,"timerange":"2022-02-22T22:22:22Z/2022-02-22T23:22:22Z"}]}`, mrt.receivedContent)

require.NoError(t, uut.SendData(context.Background(), nil))
require.Equal(t, `{"data":[]}`, mrt.receivedContent, "client should ensure that the data field is always represented as an array")
}

func TestErrorHandling(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func runQuery(ctx context.Context, odooClient OdooClient, prom PromQuerier, args
}

var errs error
var records []odoo.OdooMeteredBillingRecord
records := make([]odoo.OdooMeteredBillingRecord, 0, len(samples))
for _, sample := range samples {
record, err := processSample(ctx, odooClient, args, from, sample)
if err != nil {
Expand Down

0 comments on commit 098d8bc

Please sign in to comment.