Skip to content

Commit

Permalink
VCR: Use DID library JSON marshaller (revert API behavior to v5.1) (v…
Browse files Browse the repository at this point in the history
…5.2 backport) (#2166)
  • Loading branch information
reinkrul authored May 16, 2023
1 parent 2707330 commit 3c83b39
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/pages/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Hazelnut update (v5.2.2)
Release date: 2023-05-16

- Fixed issue where VDR could no longer update broken DID Documents.
- Reverted VCR API change in which `credentialSubject` was returned as an array instead of an object.

**Full Changelog**: https://github.com/nuts-foundation/nuts-node/compare/v5.2.1...v5.2.2

Expand Down
20 changes: 20 additions & 0 deletions vcr/api/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package v2

import (
"encoding/json"
"github.com/nuts-foundation/go-did/vc"
"github.com/nuts-foundation/nuts-node/vcr/credential"
)
Expand All @@ -35,3 +36,22 @@ type Revocation = credential.Revocation

// VerifiablePresentation is an alias to use from within the API
type VerifiablePresentation = vc.VerifiablePresentation

var _ json.Marshaler = (*IssueVC200JSONResponse)(nil)
var _ json.Marshaler = (*ResolveVC200JSONResponse)(nil)
var _ json.Marshaler = (*CreateVP200JSONResponse)(nil)

// MarshalJSON forwards the call to the underlying VerifiableCredential to make sure the expected JSON-LD is returned.
func (r IssueVC200JSONResponse) MarshalJSON() ([]byte, error) {
return vc.VerifiableCredential(r).MarshalJSON()
}

// MarshalJSON forwards the call to the underlying VerifiableCredential to make sure the expected JSON-LD is returned.
func (r ResolveVC200JSONResponse) MarshalJSON() ([]byte, error) {
return vc.VerifiableCredential(r).MarshalJSON()
}

// MarshalJSON forwards the call to the underlying VerifiableCredential to make sure the expected JSON-LD is returned.
func (r CreateVP200JSONResponse) MarshalJSON() ([]byte, error) {
return vc.VerifiablePresentation(r).MarshalJSON()
}
55 changes: 55 additions & 0 deletions vcr/api/v2/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package v2

import (
"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)

func Test_Marshalling(t *testing.T) {
t.Run("IssueVC200JSONResponse", func(t *testing.T) {
r := IssueVC200JSONResponse{
CredentialSubject: []interface{}{
map[string]interface{}{
"id": "did:nuts:123",
}},
}
data, _ := json.Marshal(r)
result := make(map[string]interface{}, 0)
err := json.Unmarshal(data, &result)
require.NoError(t, err)

assert.IsType(t, make(map[string]interface{}, 0), result["credentialSubject"]) // single entry should not end up as slice
})
t.Run("ResolveVC200JSONResponse", func(t *testing.T) {
r := ResolveVC200JSONResponse{
CredentialSubject: []interface{}{
map[string]interface{}{
"id": "did:nuts:123",
}},
}
data, _ := json.Marshal(r)
result := make(map[string]interface{}, 0)
err := json.Unmarshal(data, &result)
require.NoError(t, err)

assert.IsType(t, make(map[string]interface{}, 0), result["credentialSubject"]) // single entry should not end up as slice
})
t.Run("CreateVP200JSONResponse", func(t *testing.T) {
r := CreateVP200JSONResponse{
VerifiableCredential: []VerifiableCredential{
{
IssuanceDate: time.Now(),
},
},
}
data, _ := json.Marshal(r)
result := make(map[string]interface{}, 0)
err := json.Unmarshal(data, &result)
require.NoError(t, err)

assert.IsType(t, make(map[string]interface{}, 0), result["verifiableCredential"]) // single entry should not end up as slice
})
}

0 comments on commit 3c83b39

Please sign in to comment.