Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support single object value in BoxConflictErrorContextInfo #930

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}
}
}