Skip to content

Commit

Permalink
Merge branch 'main' into feature/field-alias
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Folkerts committed Mar 19, 2024
2 parents 4392f0b + 46ab118 commit 9d8c92c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public async Task<ContentGraphResult> GetResultAsync()
/// </summary>
/// <returns>string</returns>
/// <exception cref="ServiceException"></exception>
public async Task<string?> GetRawResultAsync()
public async Task<string> GetRawResultAsync()
{
string url = GetServiceUrl();

Expand All @@ -253,8 +253,7 @@ public async Task<ContentGraphResult> GetResultAsync()
AdditionalInformation(jsonRequest, body);
using (var reader = new StreamReader(await jsonRequest.GetResponseStream(body), jsonRequest.Encoding))
{
var jsonReader = new JsonTextReader(reader);
return jsonReader.ReadAsString();
return await reader.ReadToEndAsync();
}
}
catch (AggregateException asyncException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,11 @@ public override GraphQueryBuilder ToQuery()
if (!graphObject.OrderBy.IsNullOrEmpty())
{
graphObject.OrderBy = $"orderBy:{{{graphObject.OrderBy}}}";
graphObject.Filter = graphObject.Filter.IsNullOrEmpty() ? graphObject.OrderBy : $"{graphObject.Filter},{graphObject.OrderBy}";
}
if (!graphObject.Locale.IsNullOrEmpty() || !graphObject.Filter.IsNullOrEmpty() || !graphObject.WhereClause.IsNullOrEmpty() || !graphObject.OrderBy.IsNullOrEmpty())
if (!graphObject.Locale.IsNullOrEmpty() || !graphObject.Filter.IsNullOrEmpty() || !graphObject.WhereClause.IsNullOrEmpty())
{
graphObject.Filter = $"({graphObject.Locale}{graphObject.Filter}{graphObject.WhereClause}{graphObject.OrderBy})";
graphObject.Filter = $"({graphObject.Locale}{graphObject.Filter}{graphObject.WhereClause})";
}
if (!graphObject.Facets.IsNullOrEmpty())
{
Expand Down
34 changes: 30 additions & 4 deletions APIs/src/EpiServer.ContentGraph/Api/Result/ContentGraphHits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Dictionary<string, IEnumerable<Facet>> Facets
facets = new Dictionary<string, IEnumerable<Facet>>();
foreach (var key in keys)
{
facets.Add(key, RawFacets[key].ToObject<IEnumerable<Facet>>());
GetJArray(RawFacets[key], key, facets);
}
}
return facets;
Expand All @@ -48,19 +48,45 @@ public Dictionary<string, IEnumerable<string>> AutoComplete
autocompletes = new Dictionary<string, IEnumerable<string>>();
foreach (var key in keys)
{
autocompletes.Add(key, RawAutoComplete[key].ToObject<IEnumerable<string>>());
GetJArray(RawAutoComplete[key], key, autocompletes);
}
}
return autocompletes;
}
}
[JsonProperty("facets")]
private Dictionary<string, JArray> RawFacets { get; set; }
private Dictionary<string, object> RawFacets { get; set; }
[JsonProperty("autocomplete")]
private Dictionary<string, JArray> RawAutoComplete { get; set; }
private Dictionary<string, object> RawAutoComplete { get; set; }
[JsonProperty("cursor")]
public string Cursor { get; set; }
[JsonProperty("total")]
public int Total { get; set; }
private void GetJArray<TReturn>(object jObject, string key, Dictionary<string, IEnumerable<TReturn>> keyValues)
{
switch (jObject.GetType().Name)
{
case "JObject":
foreach (var token in ((JObject)jObject).Values())
{
string tempKey = key;
if (token.GetType() == typeof(JArray))
{
tempKey = $"{tempKey}.{token.Path}";
keyValues.Add(tempKey, ((JArray)token).ToObject<IEnumerable<TReturn>>());
}
else
{
GetJArray(token, tempKey, keyValues);
}
}
break;
case "JArray":
keyValues.Add(key, ((JArray)jObject).ToObject<IEnumerable<TReturn>>());
break;
default:
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Xunit;
using Newtonsoft.Json;
using EPiServer.ContentGraph.Api.Result;

namespace EpiServer.ContentGraph.UnitTests.DeserializationTets
{
public class DynamicAutoCompleteResponseTests
{
const string response = "{\"data\":{\"Object\":{\"autocomplete\":{\"Categories\":{\"ProviderName\":[\"Sample Category\"]},\"WebsiteUrl\":[\"http://example.com/\",\"https://test.com/\"]},\"total\":2}},\"extensions\":{\"correlationId\":\"866380417b4f1fb0\",\"cost\":72,\"costSummary\":[\"CompanyBlock(72)=basicFilter(1)*2+autocomplete(2)*35\"]}}";
[Fact]
public void nested_aotucompletes_should_convert_successfully()
{
var results = JsonSerializer.CreateDefault().Deserialize<ContentGraphResult<object>>(new JsonTextReader(new StringReader(response)));
var autoCompletesDict = results.Content.AutoComplete;
Assert.True(autoCompletesDict.Count.Equals(2));

Assert.NotNull(autoCompletesDict["Categories.ProviderName"]);
Assert.True(autoCompletesDict["Categories.ProviderName"].Count().Equals(1));

Assert.NotNull(autoCompletesDict["WebsiteUrl"]);
Assert.True(autoCompletesDict["WebsiteUrl"].Count().Equals(2));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Xunit;
using Newtonsoft.Json;
using EPiServer.ContentGraph.Api.Result;

namespace EpiServer.ContentGraph.UnitTests.DeserializationTets
{
public class DynamicFacetsResponseTests
{
const string response = "{\"data\":{\"Object\":{\"items\":[{\"Name\":\"Coreware\"}],\"facets\":{\"Categories\":{\"Id\":[{\"name\":\"100\",\"count\":3}],\"Language\":{\"Name\":[{\"name\":\"en\",\"count\":3}]}},\"Name\":[{\"name\":\"Farfetch\",\"count\":3},{\"name\":\"Geta\",\"count\":3}]},\"total\":1154}},\"extensions\":{\"correlationId\":\"8662284c0889108a\",\"cost\":62,\"costSummary\":[\"CompanyBlock(62)=basicFilter(1)*2+facets(2)*30\"]}}";
[Fact]
public void nested_facets_response_should_convert_successfully()
{
var results = JsonSerializer.CreateDefault().Deserialize<ContentGraphResult<object>>(new JsonTextReader(new StringReader(response)));
var facetsDict = results.Content.Facets;
Assert.True(facetsDict.Count.Equals(3));

Assert.NotNull(facetsDict["Categories.Id"]);
Assert.True(facetsDict["Categories.Id"].Count().Equals(1));

Assert.NotNull(facetsDict["Categories.Language.Name"]);
Assert.True(facetsDict["Categories.Language.Name"].Count().Equals(1));

Assert.NotNull(facetsDict["Name"]);
Assert.True(facetsDict["Name"].Count().Equals(2));
}
}
}

0 comments on commit 9d8c92c

Please sign in to comment.