Skip to content

Commit

Permalink
fix: event payload (#1537)
Browse files Browse the repository at this point in the history
Signed-off-by: Firas Qutishat <[email protected]>
  • Loading branch information
fqutishat authored Nov 23, 2023
1 parent d924715 commit db3e315
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
7 changes: 6 additions & 1 deletion component/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
12 changes: 10 additions & 2 deletions pkg/event/spi/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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"
Expand Down
12 changes: 10 additions & 2 deletions pkg/restapi/v1/issuer/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion pkg/restapi/v1/verifier/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion pkg/service/oidc4ci/oidc4ci_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit db3e315

Please sign in to comment.