Skip to content

Commit

Permalink
Merge pull request #20 from taxjar/line-item-id-type-error
Browse files Browse the repository at this point in the history
Handle API line item numeric strings in json.Unmarshal
  • Loading branch information
sethobey committed Oct 20, 2022
2 parents 7e75fb4 + ee78058 commit 73cc5b2
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 11 deletions.
9 changes: 8 additions & 1 deletion CreateOrder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ func (client *Config) CreateOrder(params CreateOrderParams) (*CreateOrderRespons

order := new(CreateOrderResponse)
if err := json.Unmarshal(res.([]byte), &order); err != nil {
return nil, err
if typeError, ok := err.(*json.UnmarshalTypeError); ok {
// Ignores JSON line_item.id type errors due to API's conversion of numeric strings to integers
if !(typeError.Field == "order.line_items.id" && typeError.Value == "number") {
return nil, err
}
} else {
return nil, err
}
}

return order, nil
Expand Down
9 changes: 8 additions & 1 deletion CreateRefund.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ func (client *Config) CreateRefund(params CreateRefundParams) (*CreateRefundResp

refund := new(CreateRefundResponse)
if err := json.Unmarshal(res.([]byte), &refund); err != nil {
return nil, err
if typeError, ok := err.(*json.UnmarshalTypeError); ok {
// Ignores JSON line_item.id type errors due to API's conversion of numeric strings to integers
if !(typeError.Field == "refund.line_items.id" && typeError.Value == "number") {
return nil, err
}
} else {
return nil, err
}
}

return refund, nil
Expand Down
9 changes: 8 additions & 1 deletion DeleteOrder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ func (client *Config) DeleteOrder(transactionID string, params ...DeleteOrderPar

order := new(DeleteOrderResponse)
if err := json.Unmarshal(res.([]byte), &order); err != nil {
return nil, err
if typeError, ok := err.(*json.UnmarshalTypeError); ok {
// Ignores JSON line_item.id type errors due to API's conversion of numeric strings to integers
if !(typeError.Field == "order.line_items.id" && typeError.Value == "number") {
return nil, err
}
} else {
return nil, err
}
}

return order, nil
Expand Down
9 changes: 8 additions & 1 deletion DeleteRefund.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ func (client *Config) DeleteRefund(transactionID string, params ...DeleteRefundP

refund := new(DeleteRefundResponse)
if err := json.Unmarshal(res.([]byte), &refund); err != nil {
return nil, err
if typeError, ok := err.(*json.UnmarshalTypeError); ok {
// Ignores JSON line_item.id type errors due to API's conversion of numeric strings to integers
if !(typeError.Field == "refund.line_items.id" && typeError.Value == "number") {
return nil, err
}
} else {
return nil, err
}
}

return refund, nil
Expand Down
10 changes: 8 additions & 2 deletions ShowOrder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ func (client *Config) ShowOrder(transactionID string, params ...ShowOrderParams)

order := new(ShowOrderResponse)
if err := json.Unmarshal(res.([]byte), &order); err != nil {
return nil, err
if typeError, ok := err.(*json.UnmarshalTypeError); ok {
// Ignores JSON line_item.id type errors due to API's conversion of numeric strings to integers
if !(typeError.Field == "order.line_items.id" && typeError.Value == "number") {
return nil, err
}
} else {
return nil, err
}
}

return order, nil
}
9 changes: 8 additions & 1 deletion ShowRefund.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ func (client *Config) ShowRefund(transactionID string, params ...ShowRefundParam

refund := new(ShowRefundResponse)
if err := json.Unmarshal(res.([]byte), &refund); err != nil {
return nil, err
if typeError, ok := err.(*json.UnmarshalTypeError); ok {
// Ignores JSON line_item.id type errors due to API's conversion of numeric strings to integers
if !(typeError.Field == "refund.line_items.id" && typeError.Value == "number") {
return nil, err
}
} else {
return nil, err
}
}

return refund, nil
Expand Down
9 changes: 8 additions & 1 deletion UpdateOrder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ func (client *Config) UpdateOrder(params UpdateOrderParams) (*UpdateOrderRespons

order := new(UpdateOrderResponse)
if err := json.Unmarshal(res.([]byte), &order); err != nil {
return nil, err
if typeError, ok := err.(*json.UnmarshalTypeError); ok {
// Ignores JSON line_item.id type errors due to API's conversion of numeric strings to integers
if !(typeError.Field == "order.line_items.id" && typeError.Value == "number") {
return nil, err
}
} else {
return nil, err
}
}

return order, nil
Expand Down
9 changes: 8 additions & 1 deletion UpdateRefund.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ func (client *Config) UpdateRefund(params UpdateRefundParams) (*UpdateRefundResp

refund := new(UpdateRefundResponse)
if err := json.Unmarshal(res.([]byte), &refund); err != nil {
return nil, err
if typeError, ok := err.(*json.UnmarshalTypeError); ok {
// Ignores JSON line_item.id type errors due to API's conversion of numeric strings to integers
if !(typeError.Field == "refund.line_items.id" && typeError.Value == "number") {
return nil, err
}
} else {
return nil, err
}
}

return refund, nil
Expand Down
4 changes: 3 additions & 1 deletion test/LiveToken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,11 @@ var _ = Describe("using a live/sandbox token", func() {
})

Context("UpdateOrder", func() {
It("udpates an order", func() {
It("updates an order", func() {
res, err := client.UpdateOrder(taxjar.UpdateOrderParams{
TransactionID: "24",
Amount: 161,
Shipping: 5,
SalesTax: 10.3,
LineItems: []taxjar.OrderLineItem{
{
Expand Down Expand Up @@ -305,6 +306,7 @@ var _ = Describe("using a live/sandbox token", func() {
res, err := client.UpdateRefund(taxjar.UpdateRefundParams{
TransactionID: "24-refund",
TransactionReferenceID: "24",
Amount: -116,
Shipping: -5,
})
Expect(err).NotTo(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion test/Methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ var _ = Describe("Method:", func() {
})

Context("UpdateOrder", func() {
It("udpates an order", func() {
It("updates an order", func() {
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyRequest("PUT", "/v2/transactions/orders/24"),
ghttp.RespondWith(http.StatusOK, mocks.UpdateOrderJSON),
Expand Down

0 comments on commit 73cc5b2

Please sign in to comment.