Skip to content

Commit

Permalink
fix(search): ignore invalid path expressions inside @cds.search (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
patricebender authored Nov 11, 2024
1 parent 8bb8206 commit 250edd5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion db-service/lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const _getSearchableColumns = entity => {
deepSearchCandidates.forEach(c => {
const element = c.ref.reduce((resolveIn, curr, i) => {
const next = resolveIn.elements?.[curr] || resolveIn._target.elements[curr]
if (next.isAssociation && !c.ref[i + 1]) {
if (next?.isAssociation && !c.ref[i + 1]) {
const searchInTarget = _getSearchableColumns(next._target)
searchInTarget.forEach(elementRefInTarget => {
searchableColumns.push({ ref: c.ref.concat(...elementRefInTarget.ref) })
Expand Down
12 changes: 12 additions & 0 deletions db-service/test/bookshop/db/search.cds
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ entity Books {
coAuthor_ID_unmanaged : Integer;
coAuthorUnmanaged : Association to Authors
on coAuthorUnmanaged.ID = coAuthor_ID_unmanaged;
shelf : Association to BookShelf;
}

@cds.search: {author.lastName}
Expand Down Expand Up @@ -70,3 +71,14 @@ entity CalculatedAddresses : Addresses {
entity CalculatedAddressesWithoutAnno : Addresses {
calculatedAddress : String = street || ' ' || zip || '' || city
}

@cds.search: {
genre,
books.doesNotExist
}
entity BookShelf {
key ID : Integer;
genre : String;
books : Composition of many Books
on books.shelf = $self;
}
14 changes: 14 additions & 0 deletions db-service/test/cqn4sql/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,20 @@ describe('search w/ path expressions', () => {
} where search((authorWithAddress.note, address.city), 'x')`
expect(JSON.parse(JSON.stringify(res))).to.deep.equal(expected)
})

it('dont dump for non existing search paths, but ignore the path', () => {
let query = CQL`SELECT from search.BookShelf { ID, genre }`
query.SELECT.search = [{ val: 'Harry Plotter' }]

let res = cqn4sql(query, model)
const expected = CQL`
SELECT from search.BookShelf as BookShelf
{
BookShelf.ID,
BookShelf.genre
} where search((BookShelf.genre), 'Harry Plotter')`
expect(JSON.parse(JSON.stringify(res))).to.deep.equal(expected)
})
})

describe('calculated elements', () => {
Expand Down

0 comments on commit 250edd5

Please sign in to comment.