Skip to content

Commit

Permalink
Fix #8340: Prevent null reference exception in SearchResponse<T>
Browse files Browse the repository at this point in the history
- Added null checks for `HitsMetadata` in `SearchResponse<T>.Documents`
- Ensured `HitsMetadata` and `Hits` are properly validated before accessing them
- Prevented potential crashes when the response contains no hits
  • Loading branch information
sadeghpour committed Mar 4, 2025
1 parent ffe9e46 commit 330d842
Showing 1 changed file with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
Expand All @@ -11,10 +12,10 @@ namespace Elastic.Clients.Elasticsearch;
public partial class SearchResponse<TDocument>
{
[JsonIgnore]
public IReadOnlyCollection<Core.Search.Hit<TDocument>> Hits => HitsMetadata.Hits;
public IReadOnlyCollection<Core.Search.Hit<TDocument>> Hits => HitsMetadata?.Hits ?? [];

[JsonIgnore]
public IReadOnlyCollection<TDocument> Documents => HitsMetadata.Hits.Select(s => s.Source).ToReadOnlyCollection();
public IReadOnlyCollection<TDocument> Documents => HitsMetadata?.Hits?.Select(s => s.Source).ToReadOnlyCollection() ?? [];

[JsonIgnore]
public long Total => HitsMetadata?.Total?.Item1?.Value ?? HitsMetadata?.Total?.Item2 ?? -1;
Expand Down

0 comments on commit 330d842

Please sign in to comment.