Skip to content

Commit 9ed4245

Browse files
fix(@cds.search): properly exclude an association from being searched (#1385)
it is a shame that I had no tests for this in place. :( --> With this change, excluding an association from being searched does not result in the association __being__ searched. The annotation value was just not considered.
1 parent a464ab0 commit 9ed4245

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

db-service/lib/search.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ const _getSearchableColumns = entity => {
7979
// always ignore virtual elements from search
8080
if(column?.virtual) continue
8181
if (column?.isAssociation || columnName.includes('.')) {
82+
if(!annotationValue)
83+
continue
84+
8285
const ref = columnName.split('.')
8386
if(ref.length > 1) skipDefaultSearchableElements = true
8487
deepSearchCandidates.push({ ref })

db-service/test/bookshop/db/search.cds

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ entity CalculatedAddressesWithoutAnno : Addresses {
113113
calculatedAddress : String = street || ' ' || zip || '' || city
114114
}
115115

116+
@cds.search: {calculatedAddress: false}
117+
entity CalculatedAddressesExclude : Addresses {}
118+
116119
@cds.search: {
117120
genre,
118121
books.doesNotExist
@@ -137,3 +140,10 @@ entity MultipleKeys {
137140
key ID3 : Integer;
138141
text: String;
139142
}
143+
144+
// the following should just lead to the defaults being searched
145+
@cds.search: {author: false}
146+
entity BooksDontSearchAuthor: Books {}
147+
148+
@cds.search: {author.name: false}
149+
entity BooksDontSearchAuthorName: Books {}

db-service/test/cqn4sql/search.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,4 +593,42 @@ describe('include / exclude logic', () => {
593593
} where search(Books.title, 'x')`
594594
expect(JSON.parse(JSON.stringify(transformed))).to.deep.equal(expected)
595595
})
596+
597+
it('excluding an association should not lead to the association being searched', () => {
598+
let query = cds.ql`SELECT from search.CalculatedAddressesExclude as Address { Address.ID }`
599+
query.SELECT.search = [{ val: 'x' }]
600+
601+
let res = cqn4sql(query, model)
602+
const expected = cds.ql`
603+
SELECT from search.CalculatedAddressesExclude as Address
604+
{
605+
Address.ID
606+
} where search(Address.city, 'x')`
607+
608+
expect(JSON.parse(JSON.stringify(res))).to.deep.equal(expected)
609+
})
610+
611+
it('excluding an association should not lead to the association being searched', () => {
612+
const excludeAuthor = cds.ql`SELECT from search.BooksDontSearchAuthor as Books { ID }`
613+
const excludeAuthorName = cds.ql`SELECT from search.BooksDontSearchAuthorName as Books { ID }`
614+
const defaultSearchableElements = cds.ql`SELECT from search.Books as Books { ID }`
615+
excludeAuthor.SELECT.search = [{ val: 'x' }]
616+
excludeAuthorName.SELECT.search = [{ val: 'x' }]
617+
defaultSearchableElements.SELECT.search = [{ val: 'x' }]
618+
619+
// excluding assocs (or paths) should lead to same result as default searchable elements
620+
const noAuthor = cqn4sql(excludeAuthor, model)
621+
const noAuthorName = cqn4sql(excludeAuthorName, model)
622+
const allDefaults = cqn4sql(defaultSearchableElements, model)
623+
expect(noAuthor.SELECT.where)
624+
.to.deep.equal(noAuthorName.SELECT.where)
625+
.to.deep.equal(allDefaults.SELECT.where)
626+
627+
const expected = cds.ql`
628+
SELECT from search.BooksDontSearchAuthor as Books
629+
{
630+
Books.ID
631+
} where search(Books.title, 'x')`
632+
expect(JSON.parse(JSON.stringify(noAuthor))).to.deep.equal(expected)
633+
})
596634
})

0 commit comments

Comments
 (0)