diff --git a/v2/session/json.go b/v2/session/json.go index 36525d3e..2dea2d4b 100644 --- a/v2/session/json.go +++ b/v2/session/json.go @@ -164,3 +164,23 @@ func (r *ResponseMetaHeader) UnmarshalJSON(data []byte) error { return nil } + +func (r *ResponseVerificationHeader) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + ResponseVerificationHeaderToGRPCMessage(r), + ) +} + +func (r *ResponseVerificationHeader) UnmarshalJSON(data []byte) error { + msg := new(session.ResponseVerificationHeader) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *r = *ResponseVerificationHeaderFromGRPCMessage(msg) + + return nil +} diff --git a/v2/session/json_test.go b/v2/session/json_test.go index 442395c5..00bf382d 100644 --- a/v2/session/json_test.go +++ b/v2/session/json_test.go @@ -102,3 +102,15 @@ func TestResponseMetaHeaderJSON(t *testing.T) { require.Equal(t, r, r2) } + +func TestResponseVerificationHeaderJSON(t *testing.T) { + r := generateResponseVerificationHeader("key", "value") + + data, err := r.MarshalJSON() + require.NoError(t, err) + + r2 := new(session.ResponseVerificationHeader) + require.NoError(t, r2.UnmarshalJSON(data)) + + require.Equal(t, r, r2) +} diff --git a/v2/session/marshal.go b/v2/session/marshal.go index a10ee0ff..568dfed7 100644 --- a/v2/session/marshal.go +++ b/v2/session/marshal.go @@ -745,3 +745,14 @@ func (r *ResponseVerificationHeader) StableSize() (size int) { return size } + +func (r *ResponseVerificationHeader) Unmarshal(data []byte) error { + m := new(session.ResponseVerificationHeader) + if err := goproto.Unmarshal(data, m); err != nil { + return err + } + + *r = *ResponseVerificationHeaderFromGRPCMessage(m) + + return nil +} diff --git a/v2/session/marshal_test.go b/v2/session/marshal_test.go index 4a79ef68..a2b5d9d2 100644 --- a/v2/session/marshal_test.go +++ b/v2/session/marshal_test.go @@ -166,16 +166,14 @@ func TestResponseVerificationHeader_StableMarshal(t *testing.T) { verifHeaderOrigin := generateResponseVerificationHeader("Key", "Inside") verifHeaderFrom := generateResponseVerificationHeader("Value", "Outside") verifHeaderFrom.SetOrigin(verifHeaderOrigin) - transport := new(grpc.ResponseVerificationHeader) t.Run("non empty", func(t *testing.T) { wire, err := verifHeaderFrom.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, transport) - require.NoError(t, err) + verifHeaderTo := new(session.ResponseVerificationHeader) + require.NoError(t, verifHeaderTo.Unmarshal(wire)) - verifHeaderTo := session.ResponseVerificationHeaderFromGRPCMessage(transport) require.Equal(t, verifHeaderFrom, verifHeaderTo) }) }