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

[yams-1.0-branch] _KeyedDecodingContainer and _UnkeyedDecodingContainer did not properly decode null #167

Merged
merged 1 commit into from
Jan 30, 2019
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
[Norio Nomura](https://github.com/norio-nomura)
[#146](https://github.com/jpsim/Yams/pull/146)

* Fix null/~/NULL/Null were parsed as strings, not nil by `YAMLDecoder`.
[Norio Nomura](https://github.com/norio-nomura)
[#157](https://github.com/jpsim/Yams/issues/157)

## 1.0.1

##### Breaking
Expand Down
9 changes: 2 additions & 7 deletions Sources/Yams/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private struct _KeyedDecodingContainer<Key: CodingKey> : KeyedDecodingContainerP
func contains(_ key: Key) -> Bool { return mapping[key.stringValue] != nil }

func decodeNil(forKey key: Key) throws -> Bool {
return try node(for: key) == Node("null", Tag(.null))
return try decoder(for: key).decodeNil()
}

func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T: Decodable & ScalarConstructible {
Expand Down Expand Up @@ -166,12 +166,7 @@ private struct _UnkeyedDecodingContainer: UnkeyedDecodingContainer {

mutating func decodeNil() throws -> Bool {
try throwErrorIfAtEnd(Any?.self)
if currentNode == Node("null", Tag(.null)) {
currentIndex += 1
return true
} else {
return false
}
return try currentDecoder { $0.decodeNil() }
}

mutating func decode<T>(_ type: T.Type) throws -> T where T: Decodable & ScalarConstructible {
Expand Down
26 changes: 25 additions & 1 deletion Tests/YamsTests/EncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,29 @@ class EncoderTests: XCTestCase { // swiftlint:disable:this type_body_length
expectEqual(type(of: decoded), Employee.self, "Expected decoded value to be of type Employee; got \(type(of: decoded)) instead.")
}

func test_null_yml() throws {
let s = """
n1: ~
n2: null
n3: NULL
n4: Null
n5:
"""
struct Test: Decodable {
let n1: String?
let n2: String?
let n3: String?
let n4: String?
let n5: String?
}
let t = try YAMLDecoder().decode(Test.self, from: s)
XCTAssertNil(t.n1)
XCTAssertNil(t.n2)
XCTAssertNil(t.n3)
XCTAssertNil(t.n4)
XCTAssertNil(t.n5)
}

// MARK: - Helper Functions

private func _testRoundTrip<T>(of value: T,
Expand Down Expand Up @@ -1089,7 +1112,8 @@ extension EncoderTests {
("testValuesInUnkeyedContainer", testValuesInUnkeyedContainer),
("testDictionary", testDictionary),
("testNodeTypeMismatch", testNodeTypeMismatch),
("testDecodingConcreteTypeParameter", testDecodingConcreteTypeParameter)
("testDecodingConcreteTypeParameter", testDecodingConcreteTypeParameter),
("test_null_yml", test_null_yml)
]
}
}
Expand Down