Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: event payload #1537

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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

Check warning on line 103 in component/event/event.go

View check run for this annotation

Codecov / codecov/patch

component/event/event.go#L101-L103

Added lines #L101 - L103 were not covered by tests
}

if err := json.Unmarshal(jsonData, &payload); err != nil {

Check warning on line 106 in component/event/event.go

View check run for this annotation

Codecov / codecov/patch

component/event/event.go#L106

Added line #L106 was not covered by tests
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 @@
"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 @@
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 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())

Check warning on line 117 in pkg/event/spi/spi.go

View check run for this annotation

Codecov / codecov/patch

pkg/event/spi/spi.go#L117

Added line #L117 was not covered by tests
}

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 @@

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

Check warning on line 93 in pkg/service/credentialstatus/eventhandler/eventhandler_service.go

View check run for this annotation

Codecov / codecov/patch

pkg/service/credentialstatus/eventhandler/eventhandler_service.go#L93

Added line #L93 was not covered by tests
}

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
Loading