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 duplicate search results for wildcard param #3512

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this modified the given query, which is error-prone. Better build a new query map

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
Loading