Skip to content

Commit

Permalink
Add TrimDuplicates to Search
Browse files Browse the repository at this point in the history
  • Loading branch information
pschaeflein committed Jul 31, 2023
1 parent 4137b02 commit aa7d025
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/Models/SearchQuery.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

Expand All @@ -12,7 +12,7 @@ public SearchQuery()
this.Request = new RequestProperties();
}

public SearchQuery(string queryText, List<string> selectProperties = null, List<Sort> sortList = null, long? startRow = null, int? rowLimit = null, int? rowsPerPage = null)
public SearchQuery(string queryText, List<string> selectProperties = null, List<Sort> sortList = null, long? startRow = null, int? rowLimit = null, int? rowsPerPage = null, bool? trimDuplicates = false)
: this()
{
this.Request.Querytext = queryText;
Expand All @@ -29,6 +29,8 @@ public SearchQuery(string queryText, List<string> selectProperties = null, List<
{
this.Request.SortList.SortProperties.AddRange(sortList);
}

this.Request.TrimDuplicates = trimDuplicates;
}

[JsonPropertyName("request")]
Expand All @@ -55,6 +57,8 @@ public class RequestProperties
public int? RowsPerPage { get; set; }
public bool ShouldSerializeRowsPerPage() => this.RowsPerPage.HasValue;

public bool? TrimDuplicates { get; set; }

public RequestProperties()
{
this.SelectProperties = new SelectProperties();
Expand Down
11 changes: 9 additions & 2 deletions src/Serializer/SPSearchQueryConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text.Json;
Expand Down Expand Up @@ -49,7 +49,14 @@ public override void Write(Utf8JsonWriter writer, SearchQuery value, JsonSeriali
{
requestObjectToSerialize.RowsPerPage = value.Request.RowsPerPage.Value;
}

if (value.Request.TrimDuplicates.HasValue)
{
requestObjectToSerialize.TrimDuplicates = value.Request.TrimDuplicates.Value;
}
else
{
requestObjectToSerialize.TrimDuplicates = false;
}

var objectToSerialize = new
{
Expand Down
3 changes: 0 additions & 3 deletions test/Mocks/MockHttpProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ public class MockHttpProvider : Mock<IHttpProvider>
public MockHttpProvider(HttpResponseMessage httpResponseMessage, ISerializer serializer)
: base(MockBehavior.Strict)
{
var paul = "Debug";

this.Setup(provider => provider.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<HttpCompletionOption>(), It.IsAny<CancellationToken>()))
.Callback<HttpRequestMessage, HttpCompletionOption, CancellationToken>(async (req, opt, tok) => await this.ReadRequestContent(req))
.ReturnsAsync(httpResponseMessage);


ISerializer s() {
var paul = "debug";
return serializer;
}
this.SetupGet(provider => provider.Serializer).Returns(s());
Expand Down
4 changes: 2 additions & 2 deletions test/SearchRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public async Task PostQuery_GeneratesCorrectRequest()
{
// ARRANGE
var expectedUri = new Uri($"{mockWebUrl}/_api/search/postquery");
var mockPostQueryRequest = new SearchQuery("sharepoint", new List<string> { "Title", "Author" });
var expectedContent = "{\"request\":{\"Querytext\":\"sharepoint\",\"SelectProperties\":{\"results\":[\"Title\",\"Author\"]}}}";
var mockPostQueryRequest = new SearchQuery("sharepoint", new List<string> { "Title", "Author" }, trimDuplicates: true);
var expectedContent = "{\"request\":{\"Querytext\":\"sharepoint\",\"SelectProperties\":{\"results\":[\"Title\",\"Author\"]},\"TrimDuplicates\":true}}";

using HttpResponseMessage response = new HttpResponseMessage();
using TestGraphServiceClient gsc = TestGraphServiceClient.Create(response);
Expand Down

0 comments on commit aa7d025

Please sign in to comment.