From d527d8f12fe4db30cc406e1945c7884d7dcd574d Mon Sep 17 00:00:00 2001 From: tg-msft Date: Thu, 25 Jun 2020 08:58:48 -0700 Subject: [PATCH] Search: Make SuggestResults.Results read only (#13003) * Search: Make SuggestResults.Results read only Fixes #12936 --- .../Azure.Search.Documents.netstandard2.0.cs | 4 ++-- .../src/Models/SuggestResults{T}.cs | 24 ++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/sdk/search/Azure.Search.Documents/api/Azure.Search.Documents.netstandard2.0.cs b/sdk/search/Azure.Search.Documents/api/Azure.Search.Documents.netstandard2.0.cs index c57f6963b5ff7..8fffa12c1278f 100644 --- a/sdk/search/Azure.Search.Documents/api/Azure.Search.Documents.netstandard2.0.cs +++ b/sdk/search/Azure.Search.Documents/api/Azure.Search.Documents.netstandard2.0.cs @@ -2006,7 +2006,7 @@ public static partial class SearchModelFactory public static Azure.Search.Documents.Indexes.Models.SearchServiceLimits SearchServiceLimits(int? maxFieldsPerIndex, int? maxFieldNestingDepthPerIndex, int? maxComplexCollectionFieldsPerIndex, int? maxComplexObjectsInCollectionsPerDocument) { throw null; } public static Azure.Search.Documents.Indexes.Models.SearchServiceStatistics SearchServiceStatistics(Azure.Search.Documents.Indexes.Models.SearchServiceCounters counters, Azure.Search.Documents.Indexes.Models.SearchServiceLimits limits) { throw null; } public static Azure.Search.Documents.Models.SearchSuggestion SearchSuggestion(T document, string text) { throw null; } - public static Azure.Search.Documents.Models.SuggestResults SuggestResults(System.Collections.Generic.IList> results, double? coverage) { throw null; } + public static Azure.Search.Documents.Models.SuggestResults SuggestResults(System.Collections.Generic.IReadOnlyList> results, double? coverage) { throw null; } } public enum SearchQueryType { @@ -2049,7 +2049,7 @@ public partial class SuggestResults { internal SuggestResults() { } public double? Coverage { get { throw null; } } - public System.Collections.Generic.IList> Results { get { throw null; } } + public System.Collections.Generic.IReadOnlyList> Results { get { throw null; } } } public partial class ValueFacetResult { diff --git a/sdk/search/Azure.Search.Documents/src/Models/SuggestResults{T}.cs b/sdk/search/Azure.Search.Documents/src/Models/SuggestResults{T}.cs index 1e136fc4c1f2e..6acf2ac769109 100644 --- a/sdk/search/Azure.Search.Documents/src/Models/SuggestResults{T}.cs +++ b/sdk/search/Azure.Search.Documents/src/Models/SuggestResults{T}.cs @@ -3,8 +3,10 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; +using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading; @@ -37,8 +39,7 @@ public partial class SuggestResults /// /// The sequence of suggestions returned by the query. /// - public IList> Results { get; internal set; } = - new List>(); + public IReadOnlyList> Results { get; internal set; } /// /// Initializes a new instance of the SuggestResults class. @@ -86,6 +87,7 @@ await JsonDocument.ParseAsync(json, cancellationToken: cancellationToken).Config } else if (prop.NameEquals(Constants.ValueKeyJson.EncodedUtf8Bytes)) { + List> results = new List>(); foreach (JsonElement element in prop.Value.EnumerateArray()) { SearchSuggestion suggestion = await SearchSuggestion.DeserializeAsync( @@ -97,8 +99,9 @@ await JsonDocument.ParseAsync(json, cancellationToken: cancellationToken).Config async, cancellationToken) .ConfigureAwait(false); - suggestions.Results.Add(suggestion); + results.Add(suggestion); } + suggestions.Results = new ReadOnlyCollection>(results); } } return suggestions; @@ -109,14 +112,19 @@ public static partial class SearchModelFactory { /// Initializes a new instance of SearchResult. /// - /// The .NET type that maps to the index schema. Instances of this type can - /// be retrieved as documents from the index. + /// The .NET type that maps to the index schema. Instances of this type + /// can be retrieved as documents from the index. /// - /// - /// + /// + /// The sequence of suggestions returned by the query. + /// + /// + /// A value indicating the percentage of the index that was included in + /// the query, or null if minimumCoverage was not set in the request. + /// /// A new SuggestResults instance for mocking. public static SuggestResults SuggestResults( - IList> results, + IReadOnlyList> results, double? coverage) => new SuggestResults() { Coverage = coverage, Results = results }; }