From 5cd27102769843a7610114abc2dab86941d8ccee Mon Sep 17 00:00:00 2001 From: Wout Slakhorst Date: Wed, 23 Oct 2024 08:09:27 +0200 Subject: [PATCH 1/2] fix duplicate search results for wildcard param --- discovery/store.go | 7 +++++++ discovery/store_test.go | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/discovery/store.go b/discovery/store.go index 9f7ae7e79..47f6a7fcb 100644 --- a/discovery/store.go +++ b/discovery/store.go @@ -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" @@ -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) diff --git a/discovery/store_test.go b/discovery/store_test.go index 9daa0c9f6..7df338a6e 100644 --- a/discovery/store_test.go +++ b/discovery/store_test.go @@ -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) From e033909b91ac459cd8a5a4962eda8533a56bff88 Mon Sep 17 00:00:00 2001 From: Wout Slakhorst Date: Wed, 23 Oct 2024 13:05:12 +0200 Subject: [PATCH 2/2] stuff --- discovery/store.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/discovery/store.go b/discovery/store.go index 47f6a7fcb..2f6ddd907 100644 --- a/discovery/store.go +++ b/discovery/store.go @@ -273,14 +273,15 @@ func (s *sqlStore) search(serviceID string, query map[string]string, allowUnvali stmt = stmt.Where("validated != 0") } // remove wildcards to prevent unneeded join on credential + filteredQuery := make(map[string]string) for k, v := range query { - if strings.TrimSpace(v) == "*" { - delete(query, k) + if strings.TrimSpace(v) != "*" { + filteredQuery[k] = v } } - if len(query) > 0 { + if len(filteredQuery) > 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) + stmt = store.CredentialStore{}.BuildSearchStatement(stmt, "discovery_credential.credential_id", filteredQuery) } var matches []presentationRecord