Skip to content

Commit

Permalink
fix(rule): correctly deserialize legacy 'remove' field in Consequence…
Browse files Browse the repository at this point in the history
…Params

[changelog]
  • Loading branch information
aseure committed Aug 30, 2019
1 parent 0ecdf01 commit 3ea17d0
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/Algolia.Search.Test/Serializer/SerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,28 @@ void AssertOredResult(List<List<string>> result)
}
}

[Test]
[Parallelizable]
public void TestConsequenceQueryConverter()
{
string json = "{\"remove\": [\"term1\", \"term2\"], \"edits\": [{\"type\": \"remove\", \"delete\": \"term3\"}]}";
ConsequenceQuery deserialized = JsonConvert.DeserializeObject<ConsequenceQuery>(json, new ConsequenceQueryConverter());

Assert.AreEqual(3, deserialized.Edits.Count());

Assert.True(deserialized.Edits.ElementAt(0).Type.Equals("remove"));
Assert.True(deserialized.Edits.ElementAt(0).Delete.Equals("term1"));
Assert.Null(deserialized.Edits.ElementAt(0).Insert);

Assert.True(deserialized.Edits.ElementAt(1).Type.Equals("remove"));
Assert.True(deserialized.Edits.ElementAt(1).Delete.Equals("term2"));
Assert.Null(deserialized.Edits.ElementAt(1).Insert);

Assert.True(deserialized.Edits.ElementAt(2).Type.Equals("remove"));
Assert.True(deserialized.Edits.ElementAt(2).Delete.Equals("term3"));
Assert.Null(deserialized.Edits.ElementAt(2).Insert);
}

[Test]
[Parallelizable]
public void TestEditConverter()
Expand Down
1 change: 1 addition & 0 deletions src/Algolia.Search/Models/Rules/ConsequenceParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ConsequenceParams : Query
/// When providing a string, it replaces the entire query string.
/// When providing an object, it describes incremental edits to be made to the query string (but you can’t do both).
/// </summary>
[JsonConverter(typeof(ConsequenceQueryConverter))]
public ConsequenceQuery Query { get; set; }

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions src/Algolia.Search/Models/Rules/ConsequenceQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
* THE SOFTWARE.
*/

using Algolia.Search.Serializer;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Algolia.Search.Models.Rules
Expand All @@ -35,7 +33,6 @@ public class ConsequenceQuery
/// <summary>
/// List of edits
/// </summary>
[JsonConverter(typeof(EditConverter))]
public IEnumerable<Edit> Edits { get; set; }
}
}
124 changes: 124 additions & 0 deletions src/Algolia.Search/Serializer/ConsequenceQueryConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright (c) 2018 Algolia
* http://www.algolia.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using Algolia.Search.Models.Enums;
using Algolia.Search.Models.Rules;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Algolia.Search.Serializer
{
/// <summary>
/// Custom converter for legacy EDIT objects
/// </summary>
public class ConsequenceQueryConverter : JsonConverter<ConsequenceQuery>
{
/// <summary>
/// Converter for handling legacy Edit
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="hasExistingValue"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public override ConsequenceQuery ReadJson(JsonReader reader, Type objectType, ConsequenceQuery existingValue,
bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
if (reader.TokenType != JsonToken.StartObject)
return null;

ConsequenceQuery ret = new ConsequenceQuery();

var tokens = JToken.Load(reader);
foreach (var token in tokens)
{
var isProperty = token.Type == JTokenType.Property;
if (!isProperty)
{
continue;
}

var property = (JProperty)token;
var isArray = property.Value.Type == JTokenType.Array;
if (!isArray)
{
continue;
}

var array = (JArray)property.Value;
List<Edit> edits = new List<Edit>();

switch (property.Name)
{
case "remove":
List<string> removeList = array.ToObject<List<string>>();
if (removeList.Count > 0)
{
foreach (var remove in removeList)
{
edits.Add(new Edit { Type = EditType.Remove, Delete = remove });
}
}
break;

case "edits":
edits = array.ToObject<List<Edit>>();
break;
}

if (ret.Edits == null)
{
ret.Edits = edits;
}
else
{
ret.Edits = Enumerable.Concat(ret.Edits, edits);
}
}

return ret;
}

/// <summary>
/// No need to implement this method as we want to keep the default writer
/// </summary>
/// <param name="writer"></param>
/// <param name="value"></param>
/// <param name="serializer"></param>
public override void WriteJson(JsonWriter writer, ConsequenceQuery value, JsonSerializer serializer)
{
throw new NotImplementedException();
}

/// <summary>
/// Disable write JSON
/// </summary>
public override bool CanWrite => false;
}
}

0 comments on commit 3ea17d0

Please sign in to comment.