diff --git a/component/credentialstatus/credentialstatus_service_test.go b/component/credentialstatus/credentialstatus_service_test.go index c4cf609d1..2f325aa06 100644 --- a/component/credentialstatus/credentialstatus_service_test.go +++ b/component/credentialstatus/credentialstatus_service_test.go @@ -854,7 +854,7 @@ func validateEvent(e *spi.Event) error { return fmt.Errorf(unexpectedFieldFmt, "DataContentType") } - if len(e.Data) == 0 { + if len(e.Data.(map[string]interface{})) == 0 { return fmt.Errorf(unexpectedFieldFmt, "Data") } diff --git a/component/event/event.go b/component/event/event.go index 2c5d2d9c4..cf73bee0a 100644 --- a/component/event/event.go +++ b/component/event/event.go @@ -98,7 +98,12 @@ func (b *Bus) handleEvent(e *spi.Event) error { //nolint:gocognit payload := eventPayload{} - if err := json.Unmarshal(e.Data, &payload); err != nil { + jsonData, err := json.Marshal(e.Data.(map[string]interface{})) + if err != nil { + return err + } + + if err := json.Unmarshal(jsonData, &payload); err != nil { return err } diff --git a/pkg/event/spi/spi.go b/pkg/event/spi/spi.go index ec43c980b..f7d2e400e 100644 --- a/pkg/event/spi/spi.go +++ b/pkg/event/spi/spi.go @@ -11,8 +11,11 @@ import ( "time" utiltime "github.com/trustbloc/did-go/doc/util/time" + "github.com/trustbloc/logutil-go/pkg/log" ) +var logger = log.New("event") + const ( // VerifierEventTopic verifier topic name. VerifierEventTopic = "vcs-verifier" @@ -74,7 +77,7 @@ type Event struct { DataContentType string `json:"datacontenttype,omitempty"` // Data defines message(optional). - Data json.RawMessage `json:"data,omitempty"` + Data interface{} `json:"data,omitempty"` // TransactionID defines transaction ID(optional). TransactionID string `json:"txnid,omitempty"` @@ -109,7 +112,12 @@ func (m *Event) Copy() *Event { func NewEventWithPayload(uuid string, source string, eventType EventType, payload Payload) *Event { event := NewEvent(uuid, source, eventType) - event.Data = json.RawMessage(payload) + var data map[string]interface{} + if err := json.Unmarshal(payload, &data); err != nil { + logger.Error(err.Error()) + } + + event.Data = data // vcs components always use json event.DataContentType = "application/json" diff --git a/pkg/restapi/v1/issuer/controller_test.go b/pkg/restapi/v1/issuer/controller_test.go index e0df37b7a..87fec10ec 100644 --- a/pkg/restapi/v1/issuer/controller_test.go +++ b/pkg/restapi/v1/issuer/controller_test.go @@ -840,7 +840,11 @@ func TestController_InitiateCredentialIssuance(t *testing.T) { assert.Equal(t, msg.Type, spi.IssuerOIDCInteractionFailed) ep := &oidc4ci.EventPayload{} - assert.NoError(t, json.Unmarshal(msg.Data, ep)) + + jsonData, errMarshal := json.Marshal(msg.Data.(map[string]interface{})) + require.NoError(t, errMarshal) + + assert.NoError(t, json.Unmarshal(jsonData, ep)) assert.Equal(t, string(resterr.SystemError), ep.ErrorCode) assert.Equal(t, resterr.IssuerProfileSvcComponent, ep.ErrorComponent) @@ -869,7 +873,11 @@ func TestController_InitiateCredentialIssuance(t *testing.T) { assert.Equal(t, msg.Type, spi.IssuerOIDCInteractionFailed) ep := &oidc4ci.EventPayload{} - assert.NoError(t, json.Unmarshal(msg.Data, ep)) + + jsonData, errMarshal := json.Marshal(msg.Data.(map[string]interface{})) + require.NoError(t, errMarshal) + + assert.NoError(t, json.Unmarshal(jsonData, ep)) assert.Equal(t, string(resterr.ProfileNotFound), ep.ErrorCode) assert.Empty(t, ep.ErrorComponent) diff --git a/pkg/restapi/v1/verifier/controller_test.go b/pkg/restapi/v1/verifier/controller_test.go index 2bd50926b..882ae8f45 100644 --- a/pkg/restapi/v1/verifier/controller_test.go +++ b/pkg/restapi/v1/verifier/controller_test.go @@ -679,7 +679,11 @@ func TestController_CheckAuthorizationResponse(t *testing.T) { assert.Equal(t, msg.Type, spi.VerifierOIDCInteractionFailed) ep := &oidc4ci.EventPayload{} - assert.NoError(t, json.Unmarshal(msg.Data, ep)) + + jsonData, err := json.Marshal(msg.Data.(map[string]interface{})) + require.NoError(t, err) + + assert.NoError(t, json.Unmarshal(jsonData, ep)) assert.Equal(t, string(resterr.InvalidValue), ep.ErrorCode) assert.Empty(t, ep.ErrorComponent) diff --git a/pkg/service/credentialstatus/eventhandler/eventhandler_service.go b/pkg/service/credentialstatus/eventhandler/eventhandler_service.go index 519d59453..709523d0e 100644 --- a/pkg/service/credentialstatus/eventhandler/eventhandler_service.go +++ b/pkg/service/credentialstatus/eventhandler/eventhandler_service.go @@ -88,7 +88,12 @@ func (s *Service) HandleEvent(ctx context.Context, event *spi.Event) error { //n payload := credentialstatus.UpdateCredentialStatusEventPayload{} - if err := json.Unmarshal(event.Data, &payload); err != nil { + jsonData, err := json.Marshal(event.Data.(map[string]interface{})) + if err != nil { + return err + } + + if err := json.Unmarshal(jsonData, &payload); err != nil { return err } diff --git a/pkg/service/credentialstatus/eventhandler/eventhandler_service_test.go b/pkg/service/credentialstatus/eventhandler/eventhandler_service_test.go index f4c5164cb..0bf7bee9d 100644 --- a/pkg/service/credentialstatus/eventhandler/eventhandler_service_test.go +++ b/pkg/service/credentialstatus/eventhandler/eventhandler_service_test.go @@ -197,7 +197,7 @@ func TestService_HandleEvent(t *testing.T) { event := createStatusUpdatedEvent( t, cslURL, profileID, profileVersion, statusBytePositionIndex, true) - event.Data = []byte(` 123`) + event.Data = map[string]interface{}{} s := New(&Config{ DocumentLoader: loader, diff --git a/pkg/service/oidc4ci/oidc4ci_service_test.go b/pkg/service/oidc4ci/oidc4ci_service_test.go index f88879bde..610d97d8e 100644 --- a/pkg/service/oidc4ci/oidc4ci_service_test.go +++ b/pkg/service/oidc4ci/oidc4ci_service_test.go @@ -1956,7 +1956,11 @@ func expectedPublishErrorEventFunc( require.Equal(t, spi.IssuerOIDCInteractionFailed, messages[0].Type) var ep oidc4ci.EventPayload - require.NoError(t, json.Unmarshal(messages[0].Data, &ep)) + + jsonData, err := json.Marshal(messages[0].Data.(map[string]interface{})) + require.NoError(t, err) + + require.NoError(t, json.Unmarshal(jsonData, &ep)) assert.Equalf(t, string(errCode), ep.ErrorCode, "unexpected error code") assert.Equalf(t, errComponent, ep.ErrorComponent, "unexpected error component")