Skip to content

Commit 8b69de1

Browse files
authored
fix!: return proper object type from GetFileVersionsUnderRetentionForAssignmentAsync method (#875)
1 parent 70f7a89 commit 8b69de1

11 files changed

+81
-22
lines changed

Box.V2.Test/BoxRetentionPoliciesManagerTest.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,16 @@ public async Task GetFileVersionsUnderRetentionForAssignment_ValidResponse()
260260
var retentionPolicyAssignmentId = "12345";
261261
var responseString = "{ \"entries\": [{ \"id\": 12345, \"etag\": 1, \"type\": \"file_version\", \"sequence_id\": 3, \"name\": \"Contract.pdf\", \"sha1\": \"85136C79CBF9FE36BB9D05D0639C70C265C18D37\", \"file_version\": { \"id\": 123456, \"type\": \"file_version\", \"sha1\": \"134b65991ed521fcfe4724b7d814ab8ded5185dc\" }, \"applied_at\": \"2012-12-12T10:53:43-08:00\" } ], \"limit\": 1000, \"marker\": \"some marker\" }";
262262
IBoxRequest boxRequest = null;
263-
Handler.Setup(h => h.ExecuteAsync<BoxCollectionMarkerBased<BoxFileVersion>>(It.IsAny<IBoxRequest>()))
264-
.Returns(Task.FromResult<IBoxResponse<BoxCollectionMarkerBased<BoxFileVersion>>>(new BoxResponse<BoxCollectionMarkerBased<BoxFileVersion>>()
263+
Handler.Setup(h => h.ExecuteAsync<BoxCollectionMarkerBased<BoxFile>>(It.IsAny<IBoxRequest>()))
264+
.Returns(Task.FromResult<IBoxResponse<BoxCollectionMarkerBased<BoxFile>>>(new BoxResponse<BoxCollectionMarkerBased<BoxFile>>()
265265
{
266266
Status = ResponseStatus.Success,
267267
ContentString = responseString
268268
}))
269269
.Callback<IBoxRequest>(r => boxRequest = r);
270270

271271
/*** Act ***/
272-
BoxCollectionMarkerBased<BoxFileVersion> results = await _retentionPoliciesManager.GetFileVersionsUnderRetentionForAssignmentAsync(retentionPolicyAssignmentId);
272+
BoxCollectionMarkerBased<BoxFile> results = await _retentionPoliciesManager.GetFileVersionsUnderRetentionForAssignmentAsync(retentionPolicyAssignmentId);
273273

274274
/*** Assert ***/
275275

@@ -281,7 +281,7 @@ public async Task GetFileVersionsUnderRetentionForAssignment_ValidResponse()
281281
// Response check
282282
Assert.AreEqual("12345", results.Entries[0].Id);
283283
Assert.AreEqual("Contract.pdf", results.Entries[0].Name);
284-
Assert.AreEqual("file_version", results.Entries[0].Type);
284+
Assert.AreEqual("file", results.Entries[0].Type);
285285
Assert.AreEqual("file_version", results.Entries[0].FileVersion.Type);
286286
}
287287
}

Box.V2/Box.V2.csproj

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
@@ -82,6 +82,8 @@
8282
<Compile Include="CCGAuth\BoxCCGAuth.cs" />
8383
<Compile Include="CCGAuth\CCGAuthRepository.cs" />
8484
<Compile Include="Config\BoxConfigBuilder.cs" />
85+
<Compile Include="Converter\BoxFileVersionsUnderRetentionItemConverter.cs" />
86+
<Compile Include="Converter\BoxFileVersionsUnderRetentionJsonConverter.cs" />
8587
<Compile Include="IBoxClient.cs" />
8688
<Compile Include="Converter\BoxZipConflictConverter.cs" />
8789
<Compile Include="JWTAuth\BoxJWTAuth.cs" />
@@ -333,4 +335,4 @@
333335
<None Include="packages.config" />
334336
</ItemGroup>
335337
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
336-
</Project>
338+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using Box.V2.Config;
3+
using Box.V2.Models;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace Box.V2.Converter
7+
{
8+
// workaround for https://developer.box.com/reference/get-retention-policy-assignments-id-file-versions-under-retention/
9+
// which currently returns 'file-version' instead of 'file' in 'type' field
10+
internal class BoxFileVersionsUnderRetentionItemConverter : BoxItemConverter
11+
{
12+
protected override BoxEntity Create(Type objectType, JObject jObject)
13+
{
14+
// we need to identify the top level node somehow so we check if 'file version' field is present
15+
if (FieldExists(ItemType, jObject) && FieldExists("file_version", jObject))
16+
{
17+
switch (jObject[ItemType].ToString())
18+
{
19+
// should work even if this bug is fixed
20+
case Constants.TypeFileVersion:
21+
case Constants.TypeFile:
22+
jObject[ItemType] = Constants.TypeFile;
23+
return new BoxFile();
24+
}
25+
}
26+
27+
return base.Create(objectType, jObject);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Box.V2.Converter
4+
{
5+
internal class BoxFileVersionsUnderRetentionJsonConverter : BoxJsonConverter
6+
{
7+
public override T Parse<T>(string content)
8+
{
9+
return JsonConvert.DeserializeObject<T>(content, new BoxFileVersionsUnderRetentionItemConverter());
10+
}
11+
}
12+
}

Box.V2/Converter/BoxItemConverter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Box.V2.Converter
99
{
1010
internal class BoxItemConverter : JsonCreationConverter<BoxEntity>
1111
{
12-
private const string ItemType = "type";
12+
protected const string ItemType = "type";
1313
private const string EventSourceItemType = "item_type";
1414
private const string WatermarkType = "watermark";
1515
private const string GroupId = "group_id";
@@ -143,7 +143,7 @@ protected override BoxEntity Create(Type objectType, JObject jObject)
143143
return new BoxEntity();
144144
}
145145

146-
private bool FieldExists(string fieldName, JObject jObject)
146+
protected bool FieldExists(string fieldName, JObject jObject)
147147
{
148148
return jObject[fieldName] != null;
149149
}

Box.V2/Converter/BoxJsonConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public BoxJsonConverter()
2525
/// <typeparam name="T">The type that the content should be parsed into</typeparam>
2626
/// <param name="content">The JSON string</param>
2727
/// <returns>The box representation of the JSON</returns>
28-
public T Parse<T>(string content)
28+
public virtual T Parse<T>(string content)
2929
{
3030
return JsonConvert.DeserializeObject<T>(content, new BoxItemConverter());
3131
}

Box.V2/Managers/BoxResourceManager.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ protected IBoxRequest AddDefaultHeaders(IBoxRequest request)
6969
return request;
7070
}
7171

72-
protected async Task<IBoxResponse<T>> ToResponseAsync<T>(IBoxRequest request, bool queueRequest = false)
72+
protected async Task<IBoxResponse<T>> ToResponseAsync<T>(IBoxRequest request, bool queueRequest = false,
73+
IBoxConverter converter = null)
7374
where T : class
7475
{
7576
AddDefaultHeaders(request);
7677
AddAuthorization(request);
7778
var response = await ExecuteRequest<T>(request, queueRequest).ConfigureAwait(false);
7879

79-
return response.ParseResults(_converter);
80+
return converter != null ? response.ParseResults(converter) : response.ParseResults(_converter);
8081
}
8182

8283
private async Task<IBoxResponse<T>> ExecuteRequest<T>(IBoxRequest request, bool queueRequest)

Box.V2/Managers/BoxRetentionPoliciesManager.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public async Task<BoxCollectionMarkerBased<BoxFile>> GetFilesUnderRetentionForAs
283283
/// <param name="marker">Take from "next_marker" column of a prior call to get the next page.</param>
284284
/// <param name="autoPaginate">Whether or not to auto-paginate to fetch all items; defaults to false.</param>
285285
/// <returns>Returns the list of all file versions under retentions for the assignment.</returns>
286-
public async Task<BoxCollectionMarkerBased<BoxFileVersion>> GetFileVersionsUnderRetentionForAssignmentAsync(string retentionPolicyAssignmentId, IEnumerable<string> fields = null, int limit = 100, string marker = null, bool autoPaginate = false)
286+
public async Task<BoxCollectionMarkerBased<BoxFile>> GetFileVersionsUnderRetentionForAssignmentAsync(string retentionPolicyAssignmentId, IEnumerable<string> fields = null, int limit = 100, string marker = null, bool autoPaginate = false)
287287
{
288288
BoxRequest request = new BoxRequest(_config.RetentionPolicyAssignmentsUri, string.Format(Constants.FileVersionsUnderRetentionEndpointString, retentionPolicyAssignmentId))
289289
.Param("retention_policy_assignment_id", retentionPolicyAssignmentId)
@@ -293,11 +293,12 @@ public async Task<BoxCollectionMarkerBased<BoxFileVersion>> GetFileVersionsUnder
293293

294294
if (autoPaginate)
295295
{
296-
return await AutoPaginateMarker<BoxFileVersion>(request, limit).ConfigureAwait(false);
296+
return await AutoPaginateMarker<BoxFile>(request, limit).ConfigureAwait(false);
297297
}
298298
else
299299
{
300-
var response = await ToResponseAsync<BoxCollectionMarkerBased<BoxFileVersion>>(request).ConfigureAwait(false);
300+
var response = await ToResponseAsync<BoxCollectionMarkerBased<BoxFile>>(request, false,
301+
new BoxFileVersionsUnderRetentionJsonConverter()).ConfigureAwait(false);
301302
return response.ResponseObject;
302303
}
303304
}

Box.V2/Managers/IBoxRetentionPoliciesManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,6 @@ public interface IBoxRetentionPoliciesManager
134134
/// <param name="marker">Take from "next_marker" column of a prior call to get the next page.</param>
135135
/// <param name="autoPaginate">Whether or not to auto-paginate to fetch all items; defaults to false.</param>
136136
/// <returns>Returns the list of all file versions under retentions for the assignment.</returns>
137-
Task<BoxCollectionMarkerBased<BoxFileVersion>> GetFileVersionsUnderRetentionForAssignmentAsync(string retentionPolicyAssignmentId, IEnumerable<string> fields = null, int limit = 100, string marker = null, bool autoPaginate = false);
137+
Task<BoxCollectionMarkerBased<BoxFile>> GetFileVersionsUnderRetentionForAssignmentAsync(string retentionPolicyAssignmentId, IEnumerable<string> fields = null, int limit = 100, string marker = null, bool autoPaginate = false);
138138
}
139139
}

Box.V2/Models/BoxFileVersion.cs

-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class BoxFileVersion : BoxEntity
2020
public const string FieldPurgedAt = "purged_at";
2121
public const string FieldRestoredAt = "restored_at";
2222
public const string FieldRestoredBy = "restored_by";
23-
public const string FieldFileVersion = "file_version";
2423
public const string FieldVersionNumber = "version_number";
2524

2625
/// <summary>
@@ -98,12 +97,6 @@ public class BoxFileVersion : BoxEntity
9897
[JsonProperty(PropertyName = FieldRestoredBy)]
9998
public virtual BoxUser RestoredBy { get; private set; }
10099

101-
/// <summary>
102-
/// Represents a version of a file on Box
103-
/// </summary>
104-
[JsonProperty(PropertyName = FieldFileVersion)]
105-
public virtual BoxFileVersion FileVersion { get; private set; }
106-
107100
/// <summary>
108101
/// The version number of the file version
109102
/// </summary>

docs/upgrades/4.x.x to 5.x.x.md

+20
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ OAuthSession(string access_token, string refresh_token, int expires_in, string t
116116
OAuthSession(string access_token, string refresh_token, int expires_in, string token_type)
117117
```
118118

119+
#### BoxRetentionPoliciesManager/IBoxRetentionPoliciesManager
120+
```c#
121+
//Old
122+
Task<BoxCollectionMarkerBased<BoxFileVersion>> GetFileVersionsUnderRetentionForAssignmentAsync(...)
123+
124+
//New
125+
Task<BoxCollectionMarkerBased<BoxFile>> GetFileVersionsUnderRetentionForAssignmentAsync(...)
126+
```
127+
119128
### Deprecated fields
120129

121130
Some old, deprecated fields have been removed from version 5.x.x. Read this section further to see a new, alternative fields.
@@ -186,6 +195,17 @@ AuthVersion
186195
//Box API have now one auth version so there is no need to differentiate between them.
187196
```
188197

198+
#### BoxFileVersion
199+
200+
```c#
201+
//Old
202+
BoxFileVersion
203+
204+
//New
205+
//No alternative as this field was used only in GetFileVersionsUnderRetentionForAssignmentAsync(...).
206+
//This method now returns proper BoxFile object instead.
207+
```
208+
189209
## Box.V2
190210

191211
### Minimal .NET runtime version upgrade

0 commit comments

Comments
 (0)