Skip to content

Commit

Permalink
Fix a bug when decoding a key with one character only (CoreOffice#96)
Browse files Browse the repository at this point in the history
I found an issue with the `convertFromCapitalized` key decoding strategy which threw an error when the key had just one characted.

* Fix a bug when decoding a key with one character only
* Add KeyDecodingStrategyTest
  • Loading branch information
flowbe authored and Arjun Gupta committed Jun 26, 2020
1 parent d1af199 commit f8a7903
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
5 changes: 2 additions & 3 deletions Sources/XMLCoder/Decoder/XMLDecoder.swift
Expand Up @@ -166,9 +166,8 @@ open class XMLDecoder {
guard !stringKey.isEmpty else {
return stringKey
}
var result = stringKey
let range = result.startIndex...result.index(after: result.startIndex)
result.replaceSubrange(range, with: result[range].lowercased())
let firstLetter = stringKey.prefix(1).lowercased()
let result = firstLetter + stringKey.dropFirst()
return result
}

Expand Down
30 changes: 30 additions & 0 deletions Tests/XMLCoderTests/KeyDecodingStrategyTest.swift
@@ -0,0 +1,30 @@
//
// KeyDecodingStrategyTest.swift
// XMLCoder
//
// Created by Max Desiatov on 02/05/2019.
//

import XCTest
@testable import XMLCoder

private let capitalized = """
<si><T>blah</T></si>
""".data(using: .utf8)!

private struct Item: Codable, Equatable {
public let text: String

enum CodingKeys: String, CodingKey {
case text = "t"
}
}

final class KeyDecodingStrategyTest: XCTestCase {
func testCapitalized() throws {
let decoder = XMLDecoder()
decoder.keyDecodingStrategy = .convertFromCapitalized
let result = try decoder.decode(Item.self, from: capitalized)
XCTAssertEqual(result.text, "blah")
}
}
4 changes: 4 additions & 0 deletions XMLCoder.xcodeproj/project.pbxproj
Expand Up @@ -89,6 +89,7 @@
D158F12F2229892C0032B449 /* DynamicNodeDecoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = D158F12E2229892C0032B449 /* DynamicNodeDecoding.swift */; };
D162674321F9B2AF0056D1D8 /* OptionalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D162674121F9B2850056D1D8 /* OptionalTests.swift */; };
D1761D1F2247F04500F53CEF /* DynamicNodeDecodingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1761D1E2247F04500F53CEF /* DynamicNodeDecodingTest.swift */; };
D1A3D4AD227AD9BF00F28969 /* KeyDecodingStrategyTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1A3D4AB227AD9BC00F28969 /* KeyDecodingStrategyTest.swift */; };
D1AC9466224E3E63004AB49B /* AttributedEnumIntrinsicTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1AC9464224E3E1F004AB49B /* AttributedEnumIntrinsicTest.swift */; };
D1CB1EF521EA9599009CAF02 /* RJITest.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_37 /* RJITest.swift */; };
D1CFC8242226B13F00B03222 /* NamespaceTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1CFC8222226AFB400B03222 /* NamespaceTest.swift */; };
Expand Down Expand Up @@ -210,6 +211,7 @@
D158F12E2229892C0032B449 /* DynamicNodeDecoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicNodeDecoding.swift; sourceTree = "<group>"; };
D162674121F9B2850056D1D8 /* OptionalTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OptionalTests.swift; sourceTree = "<group>"; };
D1761D1E2247F04500F53CEF /* DynamicNodeDecodingTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicNodeDecodingTest.swift; sourceTree = "<group>"; };
D1A3D4AB227AD9BC00F28969 /* KeyDecodingStrategyTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyDecodingStrategyTest.swift; sourceTree = "<group>"; };
D1AC9464224E3E1F004AB49B /* AttributedEnumIntrinsicTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributedEnumIntrinsicTest.swift; sourceTree = "<group>"; };
D1CFC8222226AFB400B03222 /* NamespaceTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NamespaceTest.swift; sourceTree = "<group>"; };
D1E0C85121D8E6540042A261 /* ErrorContextTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorContextTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -408,6 +410,7 @@
OBJ_37 /* RJITest.swift */,
D14D8A8521F1D6B300B0D31A /* SingleChildTests.swift */,
D11815BF227788BA008836E4 /* SpacePreserveTest.swift */,
D1A3D4AB227AD9BC00F28969 /* KeyDecodingStrategyTest.swift */,
);
name = XMLCoderTests;
path = Tests/XMLCoderTests;
Expand Down Expand Up @@ -668,6 +671,7 @@
BF9457CF21CBB516005ACFDE /* IntBoxTests.swift in Sources */,
BF9457E021CBB68A005ACFDE /* DataBoxTests.swift in Sources */,
D162674321F9B2AF0056D1D8 /* OptionalTests.swift in Sources */,
D1A3D4AD227AD9BF00F28969 /* KeyDecodingStrategyTest.swift in Sources */,
BF9457F521CBB6BC005ACFDE /* DecimalTests.swift in Sources */,
OBJ_83 /* CDTest.swift in Sources */,
BF63EF6721D0F874001D38C5 /* XMLKeyTests.swift in Sources */,
Expand Down

0 comments on commit f8a7903

Please sign in to comment.