From 0aea22899ed9616c769c9c2812caf6df282c1b38 Mon Sep 17 00:00:00 2001 From: Skye Free Date: Tue, 16 Mar 2021 13:51:31 -0700 Subject: [PATCH 1/2] Add support for copyInstanceOnItemCopy field for metadata templates --- BoxSDK.xcodeproj/project.pbxproj | 6 ++ .../Modules/MetadataModule+Templates.swift | 6 ++ Sources/Responses/MetadataTemplate.swift | 3 + Tests/Modules/MetadataModuleSpecs.swift | 84 ++++++++++++++++++- .../Metadata/CreateMetadataTemplate2.json | 16 ++++ docs/usage/metadata.md | 4 +- 6 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 Tests/Stubs/Resources/Metadata/CreateMetadataTemplate2.json diff --git a/BoxSDK.xcodeproj/project.pbxproj b/BoxSDK.xcodeproj/project.pbxproj index 124da5f89..f9cf708aa 100644 --- a/BoxSDK.xcodeproj/project.pbxproj +++ b/BoxSDK.xcodeproj/project.pbxproj @@ -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 */; }; @@ -871,6 +873,7 @@ B40558BA25AE57C10068784E /* ZipDownloadStatus.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = ZipDownloadStatus.json; sourceTree = ""; }; B41D9DB225D33456000BFE59 /* SearchResult200.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = SearchResult200.json; sourceTree = ""; }; B41D9DB425D3347C000BFE59 /* SearchResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResult.swift; sourceTree = ""; }; + B47486C926013A30005F4475 /* CreateMetadataTemplate2.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = CreateMetadataTemplate2.json; sourceTree = ""; }; B4882E6D25EEBE1800EB57BA /* FolderLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FolderLock.swift; sourceTree = ""; }; B4882E6F25EEE12F00EB57BA /* FolderLock.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = FolderLock.json; sourceTree = ""; }; B4882E7225EEFBC600EB57BA /* FolderLocks.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = FolderLocks.json; sourceTree = ""; }; @@ -1515,6 +1518,7 @@ 224E1BEB22C5365400F31F3A /* GetEnterpriseTemplates.json */, 224E1BE922C522D800F31F3A /* UpdateMetadataTemplate.json */, 2268B33422BD880700ABBD47 /* CreateMetadataTemplate.json */, + B47486C926013A30005F4475 /* CreateMetadataTemplate2.json */, 2268B33522BD880700ABBD47 /* GetMetadataTemplate.json */, ); path = Metadata; @@ -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 */, @@ -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 */, diff --git a/Sources/Modules/MetadataModule+Templates.swift b/Sources/Modules/MetadataModule+Templates.swift index 65424cdeb..61256b627 100644 --- a/Sources/Modules/MetadataModule+Templates.swift +++ b/Sources/Modules/MetadataModule+Templates.swift @@ -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. @@ -60,6 +61,7 @@ public extension MetadataModule { templateKey: String, displayName: String, hidden: Bool, + copyInstanceOnItemCopy: Bool? = nil, fields: [MetadataField], completion: @escaping Callback ) { @@ -70,6 +72,10 @@ public extension MetadataModule { "hidden": hidden ] + if let unwrappedCopyInstanceOnItemCopy = copyInstanceOnItemCopy { + json["copyInstanceOnItemCopy"] = unwrappedCopyInstanceOnItemCopy + } + json["fields"] = fields.map { $0.bodyDictWithDefaultKeys } boxClient.post( diff --git a/Sources/Responses/MetadataTemplate.swift b/Sources/Responses/MetadataTemplate.swift index 6a6c186a5..1d5901d9c 100644 --- a/Sources/Responses/MetadataTemplate.swift +++ b/Sources/Responses/MetadataTemplate.swift @@ -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]? @@ -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") } } diff --git a/Tests/Modules/MetadataModuleSpecs.swift b/Tests/Modules/MetadataModuleSpecs.swift index e21288bdc..3b6cc5203 100644 --- a/Tests/Modules/MetadataModuleSpecs.swift +++ b/Tests/Modules/MetadataModuleSpecs.swift @@ -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"] @@ -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") { + 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()") { diff --git a/Tests/Stubs/Resources/Metadata/CreateMetadataTemplate2.json b/Tests/Stubs/Resources/Metadata/CreateMetadataTemplate2.json new file mode 100644 index 000000000..82ec838db --- /dev/null +++ b/Tests/Stubs/Resources/Metadata/CreateMetadataTemplate2.json @@ -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 + } + ] +} + diff --git a/docs/usage/metadata.md b/docs/usage/metadata.md index 02acf1fdd..6fe581e7a 100644 --- a/docs/usage/metadata.md +++ b/docs/usage/metadata.md @@ -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. @@ -504,4 +504,4 @@ client.metadata.delete( } ``` -[delete-md-folder]: https://opensource.box.com/box-ios-sdk/Classes/MetadataModule.html#/s:6BoxSDK14MetadataModuleC6delete15forFolderWithId5scope11templateKey10completionySS_S2Sys6ResultOyytAA0A8SDKErrorCGctF \ No newline at end of file +[delete-md-folder]: https://opensource.box.com/box-ios-sdk/Classes/MetadataModule.html#/s:6BoxSDK14MetadataModuleC6delete15forFolderWithId5scope11templateKey10completionySS_S2Sys6ResultOyytAA0A8SDKErrorCGctF From 43363bb3697f07db486bf54bd1d35dd36cba206f Mon Sep 17 00:00:00 2001 From: Skye Free Date: Tue, 16 Mar 2021 16:00:00 -0700 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bde9f24d..df90ca8ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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:__