[v12.0/forgejo] fix: skip empty tokens in SearchOptions.Tokens() (#8398)

**Backport:** https://codeberg.org/forgejo/forgejo/pulls/8261

Query string tokenizer could return a list containing empty tokens when the query string was `\` or `"` (probably in other scenarios as well).

This seems undesirable and is what triggered #8260, but I'm posting this separately from that fix in case I'm wrong. Feel free to reject if so.

The actual change in behavior is that now searching for `\` or `"` behaves the same as if the query were empty (the bleve/elastic code checks that the tokenizer actually returned, anything rather than just query being non-empty).

### Tests

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [x] I do not want this change to show in the release notes.
- [ ] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Co-authored-by: Danko Aleksejevs <danko@very.lv>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8398
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
This commit is contained in:
forgejo-backport-action 2025-07-03 15:56:58 +02:00 committed by Earl Warren
commit 7781c8bae4
5 changed files with 146 additions and 19 deletions

View file

@ -87,14 +87,44 @@ func TestIndexer(t *testing.T, indexer internal.Indexer) {
}
}
func allResults(t *testing.T, data map[int64]*internal.IndexerData, result *internal.SearchResult) {
assert.Len(t, result.Hits, len(data))
assert.Equal(t, len(data), int(result.Total))
}
var cases = []*testIndexerCase{
{
Name: "default",
SearchOptions: &internal.SearchOptions{},
Expected: func(t *testing.T, data map[int64]*internal.IndexerData, result *internal.SearchResult) {
assert.Len(t, result.Hits, len(data))
assert.Equal(t, len(data), int(result.Total))
Expected: allResults,
},
{
Name: "empty keyword",
SearchOptions: &internal.SearchOptions{
Keyword: "",
},
Expected: allResults,
},
{
Name: "whitespace keyword",
SearchOptions: &internal.SearchOptions{
Keyword: " ",
},
Expected: allResults,
},
{
Name: "dangling slash in keyword",
SearchOptions: &internal.SearchOptions{
Keyword: "\\",
},
Expected: allResults,
},
{
Name: "dangling quote in keyword",
SearchOptions: &internal.SearchOptions{
Keyword: "\"",
},
Expected: allResults,
},
{
Name: "empty",