Skip to content

[Backport 9.0] Fix NPE when deserializing invalid SearchResponse #8517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,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?.Value1?.Value ?? HitsMetadata?.Total?.Value2 ?? -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public override Field Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe

public override void Write(Utf8JsonWriter writer, Field value, JsonSerializerOptions options)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}

var settings = options.GetContext<IElasticsearchClientSettings>();
var fieldName = settings.Inferrer.Field(value);

Expand All @@ -37,6 +42,11 @@ public override Field ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToC

public override void WriteAsPropertyName(Utf8JsonWriter writer, Field value, JsonSerializerOptions options)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}

var settings = options.GetContext<IElasticsearchClientSettings>();

writer.WritePropertyName(settings.Inferrer.Field(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public override Fields Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS

public override void Write(Utf8JsonWriter writer, Fields value, JsonSerializerOptions options)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}

writer.WriteCollectionValue(options, value.ListOfFields, null);
}
}
Expand Down Expand Up @@ -61,6 +66,11 @@ public override Fields Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS

public override void Write(Utf8JsonWriter writer, Fields value, JsonSerializerOptions options)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}

writer.WriteSingleOrManyCollectionValue(options, value.ListOfFields, null);
}
}