Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Fix string json unmarshal, add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed Feb 7, 2023
1 parent f91fe8c commit 759588a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
9 changes: 8 additions & 1 deletion router/types/forward.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -62,7 +63,13 @@ func (o *JSONObject) UnmarshalJSON(b []byte) error {
if err := o.orderedMap.UnmarshalJSON(b); err != nil {
// If ordered map unmarshal fails, this is a primitive value
o.obj = false
o.primitive = b
// Attempt to unmarshal as string, this removes extra JSON escaping
var primitiveStr string
if err := json.Unmarshal(b, &primitiveStr); err != nil {
o.primitive = b
return nil
}
o.primitive = []byte(primitiveStr)
return nil
}
// This is a JSON object, now stored as an ordered map to retain key order.
Expand Down
33 changes: 33 additions & 0 deletions router/types/forward_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package types_test

import (
"encoding/json"
"testing"

"github.com/strangelove-ventures/packet-forward-middleware/v4/router/types"
"github.com/stretchr/testify/require"
)

func TestForwardMetadataUnmarshalStringNext(t *testing.T) {
const memo = "{\"forward\":{\"receiver\":\"noble1f4cur2krsua2th9kkp7n0zje4stea4p9tu70u8\",\"port\":\"transfer\",\"channel\":\"channel-0\",\"timeout\":0,\"next\":\"{\\\"forward\\\":{\\\"receiver\\\":\\\"noble1l505zhahp24v5jsmps9vs5asah759fdce06sfp\\\",\\\"port\\\":\\\"transfer\\\",\\\"channel\\\":\\\"channel-0\\\",\\\"timeout\\\":0}}\"}}"
var packetMetadata types.PacketMetadata

err := json.Unmarshal([]byte(memo), &packetMetadata)
require.NoError(t, err)

nextBz, err := json.Marshal(packetMetadata.Forward.Next)
require.NoError(t, err)
require.Equal(t, `{"forward":{"receiver":"noble1l505zhahp24v5jsmps9vs5asah759fdce06sfp","port":"transfer","channel":"channel-0","timeout":0}}`, string(nextBz))
}

func TestForwardMetadataUnmarshalJSONNext(t *testing.T) {
const memo = "{\"forward\":{\"receiver\":\"noble1f4cur2krsua2th9kkp7n0zje4stea4p9tu70u8\",\"port\":\"transfer\",\"channel\":\"channel-0\",\"timeout\":0,\"next\":{\"forward\":{\"receiver\":\"noble1l505zhahp24v5jsmps9vs5asah759fdce06sfp\",\"port\":\"transfer\",\"channel\":\"channel-0\",\"timeout\":0}}}}"
var packetMetadata types.PacketMetadata

err := json.Unmarshal([]byte(memo), &packetMetadata)
require.NoError(t, err)

nextBz, err := json.Marshal(packetMetadata.Forward.Next)
require.NoError(t, err)
require.Equal(t, `{"forward":{"receiver":"noble1l505zhahp24v5jsmps9vs5asah759fdce06sfp","port":"transfer","channel":"channel-0","timeout":0}}`, string(nextBz))
}

0 comments on commit 759588a

Please sign in to comment.