Skip to content

Commit 0b38c78

Browse files
Merge pull request #793 from pauchan/pawel/update-url-transform
Allow URLTransform to accept custom character set
2 parents 1324e46 + cb355f0 commit 0b38c78

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

ObjectMapper.xcodeproj/project.pbxproj

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
030E75F61E588BF80027D94A /* IntegerOperators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038F0A021E55FE2400613148 /* IntegerOperators.swift */; };
1919
030E75F71E588BFC0027D94A /* IntegerOperators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038F0A021E55FE2400613148 /* IntegerOperators.swift */; };
2020
038F0A031E55FE2400613148 /* IntegerOperators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038F0A021E55FE2400613148 /* IntegerOperators.swift */; };
21+
1865416F1E972FA800F95A19 /* URLTransformTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1865416E1E972FA800F95A19 /* URLTransformTests.swift */; };
2122
37AFD9B91AAD191C00AB59B5 /* CustomDateFormatTransform.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37AFD9B81AAD191C00AB59B5 /* CustomDateFormatTransform.swift */; };
2223
3BAD2C0C1BDDB10D00E6B203 /* Mappable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BAD2C0B1BDDB10D00E6B203 /* Mappable.swift */; };
2324
3BAD2C0D1BDDB10D00E6B203 /* Mappable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BAD2C0B1BDDB10D00E6B203 /* Mappable.swift */; };
@@ -222,6 +223,7 @@
222223
030114A81D95187600FBFD4F /* ImmutableMappable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImmutableMappable.swift; sourceTree = "<group>"; };
223224
030114AA1D95197100FBFD4F /* ImmutableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ImmutableTests.swift; path = ObjectMapperTests/ImmutableTests.swift; sourceTree = "<group>"; };
224225
038F0A021E55FE2400613148 /* IntegerOperators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IntegerOperators.swift; sourceTree = "<group>"; };
226+
1865416E1E972FA800F95A19 /* URLTransformTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = URLTransformTests.swift; path = ObjectMapperTests/URLTransformTests.swift; sourceTree = "<group>"; };
225227
37AFD9B81AAD191C00AB59B5 /* CustomDateFormatTransform.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomDateFormatTransform.swift; sourceTree = "<group>"; };
226228
3BAD2C0B1BDDB10D00E6B203 /* Mappable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Mappable.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
227229
3BAD2C0F1BDDC0B000E6B203 /* MappableExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MappableExtensionsTests.swift; path = ObjectMapperTests/MappableExtensionsTests.swift; sourceTree = "<group>"; };
@@ -348,7 +350,6 @@
348350
6A9EEBD41AC5BFA30011F22C /* README.md */,
349351
6AAC8F7819F03C2900E7A677 /* ObjectMapper */,
350352
6AAC8F8219F03C2900E7A677 /* ObjectMapperTests */,
351-
6AAC8F7719F03C2900E7A677 /* Products */,
352353
);
353354
sourceTree = "<group>";
354355
usesTabs = 1;
@@ -365,6 +366,7 @@
365366
6A05B7AF1BE274BE00F19B53 /* ObjectMapper-tvOSTests.xctest */,
366367
);
367368
name = Products;
369+
path = ..;
368370
sourceTree = "<group>";
369371
};
370372
6AAC8F7819F03C2900E7A677 /* ObjectMapper */ = {
@@ -410,6 +412,8 @@
410412
6AAC8F8519F03C2900E7A677 /* ObjectMapperTests.swift */,
411413
6A412A231BB0DA26001C3F67 /* PerformanceTests.swift */,
412414
6A0BF1FE1C0B53470083D1AF /* ToObjectTests.swift */,
415+
1865416E1E972FA800F95A19 /* URLTransformTests.swift */,
416+
6AAC8F7719F03C2900E7A677 /* Products */,
413417
6AAC8F8319F03C2900E7A677 /* Supporting Files */,
414418
);
415419
name = ObjectMapperTests;
@@ -873,6 +877,7 @@
873877
C135CAB71D76303E00BA9338 /* DataTransformTests.swift in Sources */,
874878
6A442CA11CE251F100AB4F1F /* MapContextTests.swift in Sources */,
875879
6A6AEB961A93874F002573D3 /* BasicTypesTestsFromJSON.swift in Sources */,
880+
1865416F1E972FA800F95A19 /* URLTransformTests.swift in Sources */,
876881
6AECC9E61D79E29100222E7A /* DictionaryTransformTests.swift in Sources */,
877882
6A0BF1FF1C0B53470083D1AF /* ToObjectTests.swift in Sources */,
878883
CD44374D1AAE9C1100A271BA /* NestedKeysTests.swift in Sources */,

Sources/URLTransform.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ open class URLTransform: TransformType {
3232
public typealias Object = URL
3333
public typealias JSON = String
3434
private let shouldEncodeURLString: Bool
35+
private let allowedCharacterSet: CharacterSet
3536

3637
/**
3738
Initializes the URLTransform with an option to encode URL strings before converting them to an NSURL
3839
- parameter shouldEncodeUrlString: when true (the default) the string is encoded before passing
3940
to `NSURL(string:)`
4041
- returns: an initialized transformer
4142
*/
42-
public init(shouldEncodeURLString: Bool = true) {
43+
public init(shouldEncodeURLString: Bool = true, allowedCharacterSet: CharacterSet = .urlQueryAllowed) {
4344
self.shouldEncodeURLString = shouldEncodeURLString
45+
self.allowedCharacterSet = allowedCharacterSet
4446
}
4547

4648
open func transformFromJSON(_ value: Any?) -> URL? {
@@ -49,8 +51,8 @@ open class URLTransform: TransformType {
4951
if !shouldEncodeURLString {
5052
return URL(string: URLString)
5153
}
52-
53-
guard let escapedURLString = URLString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) else {
54+
55+
guard let escapedURLString = URLString.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) else {
5456
return nil
5557
}
5658
return URL(string: escapedURLString)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// URLTransformTests.swift
3+
// ObjectMapper
4+
//
5+
// Created by pawel-rusin on 4/7/17.
6+
// Copyright © 2017 hearst. All rights reserved.
7+
//
8+
9+
import XCTest
10+
import ObjectMapper
11+
12+
class URLTransformTests: XCTestCase {
13+
14+
func testUrlQueryAllowed() {
15+
let urlTransform = URLTransform()
16+
let input = "https://example.com/search?query=foo"
17+
let output = urlTransform.transformFromJSON(input)
18+
19+
XCTAssertEqual(output, URL(string: "https://example.com/search?query=foo"))
20+
}
21+
22+
func testCanPassInAllowedCharacterSet() {
23+
var characterSet = CharacterSet.urlQueryAllowed
24+
characterSet.insert(charactersIn: "%")
25+
let urlTransform = URLTransform(allowedCharacterSet: characterSet)
26+
let input = "https://example.com/%25"
27+
let output = urlTransform.transformFromJSON(input)
28+
29+
XCTAssertEqual(output, URL(string: "https://example.com/%25"))
30+
}
31+
}

0 commit comments

Comments
 (0)