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

Add support for copyInstanceOnItemCopy field for metadata templates #763

Merged
merged 2 commits into from
Mar 17, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions BoxSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@
B41D9DB325D33456000BFE59 /* SearchResult200.json in Resources */ = {isa = PBXBuildFile; fileRef = B41D9DB225D33456000BFE59 /* SearchResult200.json */; };
B41D9DB525D3347C000BFE59 /* SearchResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = B41D9DB425D3347C000BFE59 /* SearchResult.swift */; };
B41D9DB625D335F2000BFE59 /* SearchResult200.json in Resources */ = {isa = PBXBuildFile; fileRef = B41D9DB225D33456000BFE59 /* SearchResult200.json */; };
B47486CA26013A30005F4475 /* CreateMetadataTemplate2.json in Resources */ = {isa = PBXBuildFile; fileRef = B47486C926013A30005F4475 /* CreateMetadataTemplate2.json */; };
B47486CB26013B02005F4475 /* CreateMetadataTemplate2.json in Resources */ = {isa = PBXBuildFile; fileRef = B47486C926013A30005F4475 /* CreateMetadataTemplate2.json */; };
B4882E6E25EEBE1800EB57BA /* FolderLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4882E6D25EEBE1800EB57BA /* FolderLock.swift */; };
B4882E7025EEE12F00EB57BA /* FolderLock.json in Resources */ = {isa = PBXBuildFile; fileRef = B4882E6F25EEE12F00EB57BA /* FolderLock.json */; };
B4882E7125EEE14100EB57BA /* FolderLock.json in Resources */ = {isa = PBXBuildFile; fileRef = B4882E6F25EEE12F00EB57BA /* FolderLock.json */; };
Expand Down Expand Up @@ -871,6 +873,7 @@
B40558BA25AE57C10068784E /* ZipDownloadStatus.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = ZipDownloadStatus.json; sourceTree = "<group>"; };
B41D9DB225D33456000BFE59 /* SearchResult200.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = SearchResult200.json; sourceTree = "<group>"; };
B41D9DB425D3347C000BFE59 /* SearchResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResult.swift; sourceTree = "<group>"; };
B47486C926013A30005F4475 /* CreateMetadataTemplate2.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = CreateMetadataTemplate2.json; sourceTree = "<group>"; };
B4882E6D25EEBE1800EB57BA /* FolderLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FolderLock.swift; sourceTree = "<group>"; };
B4882E6F25EEE12F00EB57BA /* FolderLock.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = FolderLock.json; sourceTree = "<group>"; };
B4882E7225EEFBC600EB57BA /* FolderLocks.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = FolderLocks.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1515,6 +1518,7 @@
224E1BEB22C5365400F31F3A /* GetEnterpriseTemplates.json */,
224E1BE922C522D800F31F3A /* UpdateMetadataTemplate.json */,
2268B33422BD880700ABBD47 /* CreateMetadataTemplate.json */,
B47486C926013A30005F4475 /* CreateMetadataTemplate2.json */,
2268B33522BD880700ABBD47 /* GetMetadataTemplate.json */,
);
path = Metadata;
Expand Down Expand Up @@ -2119,6 +2123,7 @@
B40558B025AD2ABF0068784E /* ZipDownload.json in Resources */,
B4882E7325EEFBC600EB57BA /* FolderLocks.json in Resources */,
8093D29822FA398500DB628E /* GetWebLink.json in Resources */,
B47486CA26013A30005F4475 /* CreateMetadataTemplate2.json in Resources */,
B4882E7025EEE12F00EB57BA /* FolderLock.json in Resources */,
8093D29A22FA399D00DB628E /* UpdateWebLink.json in Resources */,
80E5679822FB931300798E3A /* FullWebLink.json in Resources */,
Expand All @@ -2134,6 +2139,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B47486CB26013B02005F4475 /* CreateMetadataTemplate2.json in Resources */,
B4882E7425EEFBD900EB57BA /* FolderLocks.json in Resources */,
B4882E7125EEE14100EB57BA /* FolderLock.json in Resources */,
B41D9DB625D335F2000BFE59 /* SearchResult200.json in Resources */,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ __Breaking Changes:__
__New Features and Enhancements:__

- Add support for folder lock functionality ([#759](https://github.com/box/box-ios-sdk/pull/759))
- Add support for copyInstanceOnItemCopy field for metadata templates ([#763](https://github.com/box/box-ios-sdk/pull/763))

__Bug Fixes:__

Expand Down
6 changes: 6 additions & 0 deletions Sources/Modules/MetadataModule+Templates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public extension MetadataModule {
/// - templateKey: A unique identifier for the template.
/// - displayName: The display name of the template.
/// - hidden: Whether this template is hidden in the UI. Defaults to false.
/// - copyInstanceOnItemCopy: Whether to copy any metadata attached to a file or folder when it is copied. Defaults to false.
/// - fields: Definition of fields for this metadata template.
/// - completion: Returns success or an error if template is invalid or
/// the user doesn't have access to the file.
Expand All @@ -60,6 +61,7 @@ public extension MetadataModule {
templateKey: String,
displayName: String,
hidden: Bool,
copyInstanceOnItemCopy: Bool? = nil,
fields: [MetadataField],
completion: @escaping Callback<MetadataTemplate>
) {
Expand All @@ -70,6 +72,10 @@ public extension MetadataModule {
"hidden": hidden
]

if let unwrappedCopyInstanceOnItemCopy = copyInstanceOnItemCopy {
json["copyInstanceOnItemCopy"] = unwrappedCopyInstanceOnItemCopy
}

json["fields"] = fields.map { $0.bodyDictWithDefaultKeys }

boxClient.post(
Expand Down
3 changes: 3 additions & 0 deletions Sources/Responses/MetadataTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public class MetadataTemplate: BoxModel {
public let displayName: String?
/// Whether this template is hidden in the UI.
public let hidden: Bool?
/// Whether or not to copy any metadata attached to a file or folder when it is copied. By default, metadata is not copied along with a file or folder when it is copied.
public let copyInstanceOnItemCopy: Bool?
/// The ordered set of key:value pairs for the template.
public let fields: [MetadataField]?

Expand All @@ -126,6 +128,7 @@ public class MetadataTemplate: BoxModel {
scope = try BoxJSONDecoder.optionalDecode(json: json, forKey: "scope")
displayName = try BoxJSONDecoder.optionalDecode(json: json, forKey: "displayName")
hidden = try BoxJSONDecoder.optionalDecode(json: json, forKey: "hidden")
copyInstanceOnItemCopy = try BoxJSONDecoder.optionalDecode(json: json, forKey: "copyInstanceOnItemCopy")
fields = try BoxJSONDecoder.optionalDecodeCollection(json: json, forKey: "fields")
}
}
84 changes: 83 additions & 1 deletion Tests/Modules/MetadataModuleSpecs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,28 @@ class MetadataModuleSpecs: QuickSpec {

describe("createTemplate()") {
it("should make API call to create metadata template and produce file model when API call succeeds") {
stub(condition: isHost("api.box.com") && isPath("/2.0/metadata_templates/schema") && isMethodPOST()) { _ in
stub(
condition: isHost("api.box.com") &&
isPath("/2.0/metadata_templates/schema") &&
isMethodPOST() &&
hasJsonBody([
"scope": "enterprise_490685",
"templateKey": "customer",
"displayName": "Customer",
"hidden": false,
"fields": [
["type": "string", "key": "customerTeam", "displayName": "Customer team", "hidden": false],
["type": "multiSelect", "hidden": false, "options": [
["key": "FY11"],
["key": "FY12"],
["key": "FY13"],
["key": "FY14"],
["key": "FY15"]
],
"key": "fy", "displayName": "FY"]
]
])
) { _ in
OHHTTPStubsResponse(
fileAtPath: OHPathForFile("CreateMetadataTemplate.json", type(of: self))!,
statusCode: 201, headers: ["Content-Type": "application/json"]
Expand Down Expand Up @@ -187,6 +208,67 @@ class MetadataModuleSpecs: QuickSpec {
}
}
}

it("should make API call to create metadata template with copyInstanceOnItemCopy and produce file model when API call succeeds") {
sujaygarlanka marked this conversation as resolved.
Show resolved Hide resolved
stub(
condition: isHost("api.box.com") &&
isPath("/2.0/metadata_templates/schema") &&
isMethodPOST() &&
hasJsonBody([
"scope": "enterprise_490685",
"templateKey": "customer",
"displayName": "Customer",
"hidden": false,
"copyInstanceOnItemCopy": true,
"fields": [
["type": "string", "key": "customerTeam", "displayName": "Customer team", "hidden": false]
]
])
) { _ in
OHHTTPStubsResponse(
fileAtPath: OHPathForFile("CreateMetadataTemplate2.json", type(of: self))!,
statusCode: 201, headers: ["Content-Type": "application/json"]
)
}

waitUntil(timeout: 10) { done in
self.sut.metadata.createTemplate(
scope: "enterprise_490685",
templateKey: "customer",
displayName: "Customer",
hidden: false,
copyInstanceOnItemCopy: true,
fields: [
MetadataField(id: nil, type: "string", key: "customerTeam", displayName: "Customer team", options: nil, hidden: false)
]
) { result in
switch result {
case let .success(template):
expect(template).toNot(beNil())
expect(template.templateKey).to(equal("customer"))
expect(template.scope).to(equal("enterprise_490685"))
expect(template.displayName).to(equal("Customer"))
expect(template.copyInstanceOnItemCopy).to(equal(true))

guard let firstField = template.fields?[0] else {
fail()
done()
return
}

expect(firstField).toNot(beNil())
expect(firstField.type).to(equal("string"))
expect(firstField.key).to(equal("customerTeam"))
expect(firstField.displayName).to(equal("Customer team"))
expect(firstField.hidden).to(equal(false))

case let .failure(error):
fail("Expected call to createTemplate to succeed, but instead got \(error)")
}
done()
}
}
}
}

describe("updateTemplate()") {
Expand Down
16 changes: 16 additions & 0 deletions Tests/Stubs/Resources/Metadata/CreateMetadataTemplate2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"templateKey": "customer",
"scope": "enterprise_490685",
"displayName": "Customer",
"copyInstanceOnItemCopy": true,
"hidden": false,
"fields": [
{
"type": "string",
"key": "customerTeam",
"displayName": "Customer team",
"hidden": false
}
]
}

4 changes: 2 additions & 2 deletions docs/usage/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Create Metadata Template
------------------------

To create a new metadata template, call
[`client.metadata.createTemplate(scope:templateKey:displayName:hidden:fields:completion:)`][create-md-template]
[`client.metadata.createTemplate(scope:templateKey:displayName:hidden:copyInstanceOnItemCopy:fields:completion:)`][create-md-template]
with the scope and name of the template, as well as the fields the template should contain.

<!-- sample post_metadata_templates_schema -->
Expand Down Expand Up @@ -504,4 +504,4 @@ client.metadata.delete(
}
```

[delete-md-folder]: https://opensource.box.com/box-ios-sdk/Classes/MetadataModule.html#/s:6BoxSDK14MetadataModuleC6delete15forFolderWithId5scope11templateKey10completionySS_S2Sys6ResultOyytAA0A8SDKErrorCGctF
[delete-md-folder]: https://opensource.box.com/box-ios-sdk/Classes/MetadataModule.html#/s:6BoxSDK14MetadataModuleC6delete15forFolderWithId5scope11templateKey10completionySS_S2Sys6ResultOyytAA0A8SDKErrorCGctF