Skip to content

Commit

Permalink
fix: support object value in BoxConflictErrorContextInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mwwoda committed Nov 29, 2023
1 parent ff55a63 commit 012ccde
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Box.V2.Test/Box.V2.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@
<None Update="Fixtures\BoxWebLinks\CreateWebLinkSharedLink200.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Fixtures\Converters\SingleOrCollectionConverter\Empty.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Fixtures\Converters\SingleOrCollectionConverter\EmptyArray.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Fixtures\Converters\SingleOrCollectionConverter\Array.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Fixtures\Converters\SingleOrCollectionConverter\SingleObject.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestData\smalltest.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
50 changes: 50 additions & 0 deletions Box.V2.Test/Converters/SingleOrCollectionConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Box.V2.Converter;
using Box.V2.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Box.V2.Test
{
[TestClass]
public class SingleOrCollectionConverterTest : BoxResourceManagerTest
{
private readonly IBoxConverter _converter;

public SingleOrCollectionConverterTest()
{
_converter = new BoxJsonConverter();
}

[TestMethod]
public void SingleObject()
{
var json = LoadFixtureFromJson("Fixtures/Converters/SingleOrCollectionConverter/SingleObject.json");
var error = _converter.Parse<BoxConflictErrorContextInfo<BoxFolder>>(json);
Assert.AreEqual(error.Conflicts[0].Name, "Test Folder");
}

[TestMethod]
public void Array()
{
var json = LoadFixtureFromJson("Fixtures/Converters/SingleOrCollectionConverter/Array.json");
var error = _converter.Parse<BoxConflictErrorContextInfo<BoxFolder>>(json);
Assert.AreEqual(error.Conflicts[0].Name, "Test Folder");
Assert.AreEqual(error.Conflicts[1].Name, "Test Folder 2");
}

[TestMethod]
public void EmptyArray()
{
var json = LoadFixtureFromJson("Fixtures/Converters/SingleOrCollectionConverter/EmptyArray.json");
var error = _converter.Parse<BoxConflictErrorContextInfo<BoxFolder>>(json);
Assert.AreEqual(error.Conflicts.Count, 0);
}

[TestMethod]
public void Empty()
{
var json = LoadFixtureFromJson("Fixtures/Converters/SingleOrCollectionConverter/Empty.json");
var error = _converter.Parse<BoxConflictErrorContextInfo<BoxFolder>>(json);
Assert.IsNull(error.Conflicts);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"conflicts": [
{
"type": "folder",
"id": "123",
"sequence_id": "0",
"etag": "0",
"name": "Test Folder"
},
{
"type": "folder",
"id": "124",
"sequence_id": "0",
"etag": "0",
"name": "Test Folder 2"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"conflicts": [
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"conflicts": {
"type": "folder",
"id": "123",
"sequence_id": "0",
"etag": "0",
"name": "Test Folder"
}
}
27 changes: 27 additions & 0 deletions Box.V2/Wrappers/BoxErrorContextInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.ObjectModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

namespace Box.V2
{
Expand All @@ -14,6 +17,8 @@ public class BoxConflictErrorContextInfo<T> where T : class
/// </summary>
/// <value>The conflicts.</value>
[JsonProperty(PropertyName = "conflicts")]
//in case of copyFolder conflict object is returned instead of an array
[JsonConverter(typeof(SingleOrCollectionConverter))]
public Collection<T> Conflicts { get; set; }
}

Expand All @@ -30,4 +35,26 @@ public class BoxPreflightCheckConflictErrorContextInfo<T> where T : class
[JsonProperty(PropertyName = "conflicts")]
public T Conflict { get; set; }
}


internal class SingleOrCollectionConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => true;

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jToken = serializer.Deserialize<JToken>(reader);

return jToken is JArray ?
jToken.ToObject(objectType, serializer) :
new JArray(jToken).ToObject(objectType, serializer);
}

public override bool CanWrite => false;

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}

0 comments on commit 012ccde

Please sign in to comment.