From bbef7041820e589f42f102027c777f7c947661e4 Mon Sep 17 00:00:00 2001 From: Mohit Chakraborty <8271806+Mohit-Chakraborty@users.noreply.github.com> Date: Wed, 13 Apr 2022 10:25:42 -0700 Subject: [PATCH] [Search] Add missing property when cloning `SearchOptions` (#27582) * Use reflection to clone `SearchOptions` Copy all properties using reflection to alleviate missing some properties being copied * Simplify code by using `PropertyInfo` efficiently * Add a test for `SearchOptions` clone method * Do not use reflection to copy over `SearchOptions` properties --- .../src/Options/SearchOptions.cs | 1 + .../tests/DocumentOperations/SearchTests.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/sdk/search/Azure.Search.Documents/src/Options/SearchOptions.cs b/sdk/search/Azure.Search.Documents/src/Options/SearchOptions.cs index 2e1d58b37588..4e33155700be 100644 --- a/sdk/search/Azure.Search.Documents/src/Options/SearchOptions.cs +++ b/sdk/search/Azure.Search.Documents/src/Options/SearchOptions.cs @@ -334,6 +334,7 @@ private static void Copy(SearchOptions source, SearchOptions destination) destination.Select = source.Select; destination.SemanticConfigurationName = source.SemanticConfigurationName; destination.SemanticFields = source.SemanticFields; + destination.SessionId = source.SessionId; destination.Size = source.Size; destination.Skip = source.Skip; } diff --git a/sdk/search/Azure.Search.Documents/tests/DocumentOperations/SearchTests.cs b/sdk/search/Azure.Search.Documents/tests/DocumentOperations/SearchTests.cs index 813699c91690..df11eb6cb380 100644 --- a/sdk/search/Azure.Search.Documents/tests/DocumentOperations/SearchTests.cs +++ b/sdk/search/Azure.Search.Documents/tests/DocumentOperations/SearchTests.cs @@ -1018,6 +1018,34 @@ await resources.GetQueryClient().SearchAsync( ids); } + [Test] + public void SearchOptionsCanBeCopied() + { + SearchOptions source = new(); + + source.Facets = new List { "facet1", "facet2" }; + source.Filter = "searchFilter"; + // source.IncludeTotalCount = null; + source.QueryCaptionHighlightEnabled = false; + // source.QueryType = null; + source.Select = null; + source.SessionId = "SessionId"; + source.Size = 100; + source.Skip = null; + + SearchOptions clonedSearchOptions = source.Clone(); + + CollectionAssert.AreEquivalent(source.Facets, clonedSearchOptions.Facets); // A non-null collection with multiple items + Assert.AreEqual(source.Filter, clonedSearchOptions.Filter); // A string value + Assert.IsNull(clonedSearchOptions.IncludeTotalCount); // An unset bool? value + Assert.AreEqual(source.QueryCaptionHighlightEnabled, clonedSearchOptions.QueryCaptionHighlightEnabled); // A bool? value + Assert.IsNull(source.QueryType); // An unset enum? value + Assert.IsNull(clonedSearchOptions.Select); // A `null` collection + Assert.AreEqual(source.SessionId, clonedSearchOptions.SessionId); // A string value + Assert.AreEqual(source.Size, clonedSearchOptions.Size); // An int? value + Assert.IsNull(clonedSearchOptions.Skip); // An int? value set as `null` + } + /* TODO: Enable these Track 1 tests when we have support for index creation protected void TestCanSearchWithDateTimeInStaticModel() {