Skip to content

Commit

Permalink
fix duplicate search results for wildcard param
Browse files Browse the repository at this point in the history
  • Loading branch information
woutslakhorst committed Oct 23, 2024
1 parent 5b0dd1e commit 5cd2710
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions discovery/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/nuts-foundation/go-did/did"
"github.com/nuts-foundation/nuts-node/vcr/credential/store"
"slices"
"strings"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -271,6 +272,12 @@ func (s *sqlStore) search(serviceID string, query map[string]string, allowUnvali
if !allowUnvalidated {
stmt = stmt.Where("validated != 0")
}
// remove wildcards to prevent unneeded join on credential
for k, v := range query {
if strings.TrimSpace(v) == "*" {
delete(query, k)
}
}
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)
Expand Down
10 changes: 10 additions & 0 deletions discovery/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ func Test_sqlStore_search(t *testing.T) {
require.NoError(t, err)
require.Len(t, actualVPs, 2)

t.Run("wildcard", func(t *testing.T) {
actualVPs, err = c.search(testServiceID, map[string]string{"credentialSubject.person.givenName": "*"}, true)
require.NoError(t, err)
require.Len(t, actualVPs, 2)
})
t.Run("wildcard postfix", func(t *testing.T) {
actualVPs, err = c.search(testServiceID, map[string]string{"credentialSubject.person.givenName": "A*"}, true)
require.NoError(t, err)
require.Len(t, actualVPs, 1)
})
t.Run("validated", func(t *testing.T) {
actualVPs, err = c.search(testServiceID, map[string]string{}, false)
require.NoError(t, err)
Expand Down

0 comments on commit 5cd2710

Please sign in to comment.