diff --git a/src/Elastic.Clients.Elasticsearch/_Shared/Api/SearchResponse.cs b/src/Elastic.Clients.Elasticsearch/_Shared/Api/SearchResponse.cs index d3bb84283e..a8bae0d4b5 100644 --- a/src/Elastic.Clients.Elasticsearch/_Shared/Api/SearchResponse.cs +++ b/src/Elastic.Clients.Elasticsearch/_Shared/Api/SearchResponse.cs @@ -11,10 +11,10 @@ namespace Elastic.Clients.Elasticsearch; public partial class SearchResponse { [JsonIgnore] - public IReadOnlyCollection> Hits => HitsMetadata.Hits; + public IReadOnlyCollection> Hits => HitsMetadata?.Hits ?? []; [JsonIgnore] - public IReadOnlyCollection Documents => HitsMetadata.Hits.Select(s => s.Source).ToReadOnlyCollection(); + public IReadOnlyCollection Documents => HitsMetadata?.Hits?.Select(s => s.Source).ToReadOnlyCollection() ?? []; [JsonIgnore] public long Total => HitsMetadata?.Total?.Value1?.Value ?? HitsMetadata?.Total?.Value2 ?? -1; diff --git a/src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Field/FieldConverter.cs b/src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Field/FieldConverter.cs index d32d7a1cfd..b12a2e16a5 100644 --- a/src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Field/FieldConverter.cs +++ b/src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Field/FieldConverter.cs @@ -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(); var fieldName = settings.Inferrer.Field(value); @@ -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(); writer.WritePropertyName(settings.Inferrer.Field(value)); diff --git a/src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Fields/FieldsConverter.cs b/src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Fields/FieldsConverter.cs index 69d0860e26..cd5af1af70 100644 --- a/src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Fields/FieldsConverter.cs +++ b/src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Fields/FieldsConverter.cs @@ -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); } } @@ -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); } }