diff --git a/agent/structs/structs.go b/agent/structs/structs.go index 48024b6ab..d6fb98958 100644 --- a/agent/structs/structs.go +++ b/agent/structs/structs.go @@ -1056,10 +1056,12 @@ func (t *ServiceConnect) UnmarshalJSON(data []byte) (err error) { }{ Alias: (*Alias)(t), } + if err = json.Unmarshal(data, &aux); err != nil { return err } - if t.SidecarService == nil { + + if t.SidecarService == nil && aux != nil { t.SidecarService = aux.SidecarServiceSnake } return nil diff --git a/agent/structs/structs_test.go b/agent/structs/structs_test.go index ae42a905a..2d3bc9b8f 100644 --- a/agent/structs/structs_test.go +++ b/agent/structs/structs_test.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/consul/agent/cache" "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/types" "github.com/stretchr/testify/assert" @@ -173,6 +174,46 @@ func testServiceNode(t *testing.T) *ServiceNode { } } +func TestRegisterRequest_UnmarshalJSON_WithConnectNilDoesNotPanic(t *testing.T) { + in := ` +{ + "ID": "", + "Node": "k8s-sync", + "Address": "127.0.0.1", + "TaggedAddresses": null, + "NodeMeta": { + "external-source": "kubernetes" + }, + "Datacenter": "", + "Service": { + "Kind": "", + "ID": "test-service-f8fd5f0f4e6c", + "Service": "test-service", + "Tags": [ + "k8s" + ], + "Meta": { + "external-k8s-ns": "", + "external-source": "kubernetes", + "port-stats": "18080" + }, + "Port": 8080, + "Address": "192.0.2.10", + "EnableTagOverride": false, + "CreateIndex": 0, + "ModifyIndex": 0, + "Connect": null + }, + "Check": null, + "SkipNodeUpdate": true +} +` + + var req RegisterRequest + err := lib.DecodeJSON(strings.NewReader(in), &req) + require.NoError(t, err) +} + func TestNode_IsSame(t *testing.T) { id := types.NodeID("e62f3b31-9284-4e26-ab14-2a59dea85b55") node := "mynode1"