Skip to content

Commit

Permalink
prevent duplicate results in discovery search (#3459)
Browse files Browse the repository at this point in the history
  • Loading branch information
woutslakhorst authored Oct 8, 2024
1 parent 48d37c1 commit 7854e88
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions discovery/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@ func (s *sqlStore) get(serviceID string, startAfter int) (map[string]vc.Verifiab
// Wildcard matching is supported by prefixing or suffixing the value with an asterisk (*).
// It returns the presentations which contain credentials that match the given query.
func (s *sqlStore) search(serviceID string, query map[string]string) ([]vc.VerifiablePresentation, error) {
// first only select columns also used in group by clause
// if the query is empty, there's no need to do a join
stmt := s.db.Model(&presentationRecord{}).
Where("service_id = ?", serviceID).
Joins("inner join discovery_credential ON discovery_credential.presentation_id = discovery_presentation.id")
stmt = store.CredentialStore{}.BuildSearchStatement(stmt, "discovery_credential.credential_id", query)
Where("service_id = ?", serviceID)
if len(query) > 0 {
stmt = stmt.Joins("inner join discovery_credential ON discovery_credential.presentation_id = discovery_presentation.id")
stmt = store.CredentialStore{}.BuildSearchStatement(stmt, "discovery_credential.credential_id", query)
}

var matches []presentationRecord
if err := stmt.Preload("Credentials").Preload("Credentials.Credential").Find(&matches).Error; err != nil {
Expand Down
12 changes: 12 additions & 0 deletions discovery/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ func Test_sqlStore_search(t *testing.T) {
require.Len(t, actualVPs, 1)
assert.Equal(t, vpAlice.ID.String(), actualVPs[0].ID.String())
})
t.Run("find all", func(t *testing.T) {
vps := []vc.VerifiablePresentation{vpAlice, vpBob}
c := setupStore(t, storageEngine.GetSQLDatabase())
for _, vp := range vps {
err := c.add(testServiceID, vp, 0)
require.NoError(t, err)
}

actualVPs, err := c.search(testServiceID, map[string]string{})
require.NoError(t, err)
require.Len(t, actualVPs, 2)
})
t.Run("not found", func(t *testing.T) {
vps := []vc.VerifiablePresentation{vpAlice, vpBob}
c := setupStore(t, storageEngine.GetSQLDatabase())
Expand Down

0 comments on commit 7854e88

Please sign in to comment.