Skip to content

Commit

Permalink
fix!: return proper object type from `GetFileVersionsUnderRetentionFo…
Browse files Browse the repository at this point in the history
…rAssignmentAsync` method (#875)
  • Loading branch information
mwwoda authored Dec 14, 2022
1 parent 70f7a89 commit 8b69de1
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 22 deletions.
8 changes: 4 additions & 4 deletions Box.V2.Test/BoxRetentionPoliciesManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,16 @@ public async Task GetFileVersionsUnderRetentionForAssignment_ValidResponse()
var retentionPolicyAssignmentId = "12345";
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\" }";
IBoxRequest boxRequest = null;
Handler.Setup(h => h.ExecuteAsync<BoxCollectionMarkerBased<BoxFileVersion>>(It.IsAny<IBoxRequest>()))
.Returns(Task.FromResult<IBoxResponse<BoxCollectionMarkerBased<BoxFileVersion>>>(new BoxResponse<BoxCollectionMarkerBased<BoxFileVersion>>()
Handler.Setup(h => h.ExecuteAsync<BoxCollectionMarkerBased<BoxFile>>(It.IsAny<IBoxRequest>()))
.Returns(Task.FromResult<IBoxResponse<BoxCollectionMarkerBased<BoxFile>>>(new BoxResponse<BoxCollectionMarkerBased<BoxFile>>()
{
Status = ResponseStatus.Success,
ContentString = responseString
}))
.Callback<IBoxRequest>(r => boxRequest = r);

/*** Act ***/
BoxCollectionMarkerBased<BoxFileVersion> results = await _retentionPoliciesManager.GetFileVersionsUnderRetentionForAssignmentAsync(retentionPolicyAssignmentId);
BoxCollectionMarkerBased<BoxFile> results = await _retentionPoliciesManager.GetFileVersionsUnderRetentionForAssignmentAsync(retentionPolicyAssignmentId);

/*** Assert ***/

Expand All @@ -281,7 +281,7 @@ public async Task GetFileVersionsUnderRetentionForAssignment_ValidResponse()
// Response check
Assert.AreEqual("12345", results.Entries[0].Id);
Assert.AreEqual("Contract.pdf", results.Entries[0].Name);
Assert.AreEqual("file_version", results.Entries[0].Type);
Assert.AreEqual("file", results.Entries[0].Type);
Assert.AreEqual("file_version", results.Entries[0].FileVersion.Type);
}
}
Expand Down
6 changes: 4 additions & 2 deletions Box.V2/Box.V2.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -82,6 +82,8 @@
<Compile Include="CCGAuth\BoxCCGAuth.cs" />
<Compile Include="CCGAuth\CCGAuthRepository.cs" />
<Compile Include="Config\BoxConfigBuilder.cs" />
<Compile Include="Converter\BoxFileVersionsUnderRetentionItemConverter.cs" />
<Compile Include="Converter\BoxFileVersionsUnderRetentionJsonConverter.cs" />
<Compile Include="IBoxClient.cs" />
<Compile Include="Converter\BoxZipConflictConverter.cs" />
<Compile Include="JWTAuth\BoxJWTAuth.cs" />
Expand Down Expand Up @@ -333,4 +335,4 @@
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
30 changes: 30 additions & 0 deletions Box.V2/Converter/BoxFileVersionsUnderRetentionItemConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using Box.V2.Config;
using Box.V2.Models;
using Newtonsoft.Json.Linq;

namespace Box.V2.Converter
{
// workaround for https://developer.box.com/reference/get-retention-policy-assignments-id-file-versions-under-retention/
// which currently returns 'file-version' instead of 'file' in 'type' field
internal class BoxFileVersionsUnderRetentionItemConverter : BoxItemConverter
{
protected override BoxEntity Create(Type objectType, JObject jObject)
{
// we need to identify the top level node somehow so we check if 'file version' field is present
if (FieldExists(ItemType, jObject) && FieldExists("file_version", jObject))
{
switch (jObject[ItemType].ToString())
{
// should work even if this bug is fixed
case Constants.TypeFileVersion:
case Constants.TypeFile:
jObject[ItemType] = Constants.TypeFile;
return new BoxFile();
}
}

return base.Create(objectType, jObject);
}
}
}
12 changes: 12 additions & 0 deletions Box.V2/Converter/BoxFileVersionsUnderRetentionJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace Box.V2.Converter
{
internal class BoxFileVersionsUnderRetentionJsonConverter : BoxJsonConverter
{
public override T Parse<T>(string content)
{
return JsonConvert.DeserializeObject<T>(content, new BoxFileVersionsUnderRetentionItemConverter());
}
}
}
4 changes: 2 additions & 2 deletions Box.V2/Converter/BoxItemConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Box.V2.Converter
{
internal class BoxItemConverter : JsonCreationConverter<BoxEntity>
{
private const string ItemType = "type";
protected const string ItemType = "type";
private const string EventSourceItemType = "item_type";
private const string WatermarkType = "watermark";
private const string GroupId = "group_id";
Expand Down Expand Up @@ -143,7 +143,7 @@ protected override BoxEntity Create(Type objectType, JObject jObject)
return new BoxEntity();
}

private bool FieldExists(string fieldName, JObject jObject)
protected bool FieldExists(string fieldName, JObject jObject)
{
return jObject[fieldName] != null;
}
Expand Down
2 changes: 1 addition & 1 deletion Box.V2/Converter/BoxJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public BoxJsonConverter()
/// <typeparam name="T">The type that the content should be parsed into</typeparam>
/// <param name="content">The JSON string</param>
/// <returns>The box representation of the JSON</returns>
public T Parse<T>(string content)
public virtual T Parse<T>(string content)
{
return JsonConvert.DeserializeObject<T>(content, new BoxItemConverter());
}
Expand Down
5 changes: 3 additions & 2 deletions Box.V2/Managers/BoxResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ protected IBoxRequest AddDefaultHeaders(IBoxRequest request)
return request;
}

protected async Task<IBoxResponse<T>> ToResponseAsync<T>(IBoxRequest request, bool queueRequest = false)
protected async Task<IBoxResponse<T>> ToResponseAsync<T>(IBoxRequest request, bool queueRequest = false,
IBoxConverter converter = null)
where T : class
{
AddDefaultHeaders(request);
AddAuthorization(request);
var response = await ExecuteRequest<T>(request, queueRequest).ConfigureAwait(false);

return response.ParseResults(_converter);
return converter != null ? response.ParseResults(converter) : response.ParseResults(_converter);
}

private async Task<IBoxResponse<T>> ExecuteRequest<T>(IBoxRequest request, bool queueRequest)
Expand Down
7 changes: 4 additions & 3 deletions Box.V2/Managers/BoxRetentionPoliciesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public async Task<BoxCollectionMarkerBased<BoxFile>> GetFilesUnderRetentionForAs
/// <param name="marker">Take from "next_marker" column of a prior call to get the next page.</param>
/// <param name="autoPaginate">Whether or not to auto-paginate to fetch all items; defaults to false.</param>
/// <returns>Returns the list of all file versions under retentions for the assignment.</returns>
public async Task<BoxCollectionMarkerBased<BoxFileVersion>> GetFileVersionsUnderRetentionForAssignmentAsync(string retentionPolicyAssignmentId, IEnumerable<string> fields = null, int limit = 100, string marker = null, bool autoPaginate = false)
public async Task<BoxCollectionMarkerBased<BoxFile>> GetFileVersionsUnderRetentionForAssignmentAsync(string retentionPolicyAssignmentId, IEnumerable<string> fields = null, int limit = 100, string marker = null, bool autoPaginate = false)
{
BoxRequest request = new BoxRequest(_config.RetentionPolicyAssignmentsUri, string.Format(Constants.FileVersionsUnderRetentionEndpointString, retentionPolicyAssignmentId))
.Param("retention_policy_assignment_id", retentionPolicyAssignmentId)
Expand All @@ -293,11 +293,12 @@ public async Task<BoxCollectionMarkerBased<BoxFileVersion>> GetFileVersionsUnder

if (autoPaginate)
{
return await AutoPaginateMarker<BoxFileVersion>(request, limit).ConfigureAwait(false);
return await AutoPaginateMarker<BoxFile>(request, limit).ConfigureAwait(false);
}
else
{
var response = await ToResponseAsync<BoxCollectionMarkerBased<BoxFileVersion>>(request).ConfigureAwait(false);
var response = await ToResponseAsync<BoxCollectionMarkerBased<BoxFile>>(request, false,
new BoxFileVersionsUnderRetentionJsonConverter()).ConfigureAwait(false);
return response.ResponseObject;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Box.V2/Managers/IBoxRetentionPoliciesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ public interface IBoxRetentionPoliciesManager
/// <param name="marker">Take from "next_marker" column of a prior call to get the next page.</param>
/// <param name="autoPaginate">Whether or not to auto-paginate to fetch all items; defaults to false.</param>
/// <returns>Returns the list of all file versions under retentions for the assignment.</returns>
Task<BoxCollectionMarkerBased<BoxFileVersion>> GetFileVersionsUnderRetentionForAssignmentAsync(string retentionPolicyAssignmentId, IEnumerable<string> fields = null, int limit = 100, string marker = null, bool autoPaginate = false);
Task<BoxCollectionMarkerBased<BoxFile>> GetFileVersionsUnderRetentionForAssignmentAsync(string retentionPolicyAssignmentId, IEnumerable<string> fields = null, int limit = 100, string marker = null, bool autoPaginate = false);
}
}
7 changes: 0 additions & 7 deletions Box.V2/Models/BoxFileVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class BoxFileVersion : BoxEntity
public const string FieldPurgedAt = "purged_at";
public const string FieldRestoredAt = "restored_at";
public const string FieldRestoredBy = "restored_by";
public const string FieldFileVersion = "file_version";
public const string FieldVersionNumber = "version_number";

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

/// <summary>
/// Represents a version of a file on Box
/// </summary>
[JsonProperty(PropertyName = FieldFileVersion)]
public virtual BoxFileVersion FileVersion { get; private set; }

/// <summary>
/// The version number of the file version
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions docs/upgrades/4.x.x to 5.x.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ OAuthSession(string access_token, string refresh_token, int expires_in, string t
OAuthSession(string access_token, string refresh_token, int expires_in, string token_type)
```

#### BoxRetentionPoliciesManager/IBoxRetentionPoliciesManager
```c#
//Old
Task<BoxCollectionMarkerBased<BoxFileVersion>> GetFileVersionsUnderRetentionForAssignmentAsync(...)

//New
Task<BoxCollectionMarkerBased<BoxFile>> GetFileVersionsUnderRetentionForAssignmentAsync(...)
```

### Deprecated fields

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

#### BoxFileVersion

```c#
//Old
BoxFileVersion

//New
//No alternative as this field was used only in GetFileVersionsUnderRetentionForAssignmentAsync(...).
//This method now returns proper BoxFile object instead.
```

## Box.V2

### Minimal .NET runtime version upgrade
Expand Down

0 comments on commit 8b69de1

Please sign in to comment.