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

null/~/NULL/Null parsed as strings, not nil #157

Closed
finestructure opened this issue Jan 28, 2019 · 3 comments
Closed

null/~/NULL/Null parsed as strings, not nil #157

finestructure opened this issue Jan 28, 2019 · 3 comments

Comments

@finestructure
Copy link
Contributor

I'm probably just holding it wrong but when I try to decode yml values with nil values - which I believe are represented as null | ~ | NULL | Null in yml - I get the corresponding Strings back and not nil:

    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 failed: "~"
        XCTAssertNil(t.n2)  // XCTAssertNil failed: "null"
        XCTAssertNil(t.n3)  // XCTAssertNil failed: "NULL"
        XCTAssertNil(t.n4)  // XCTAssertNil failed: "Null"
        XCTAssertNil(t.n5)  // XCTAssertNil failed: ""
    }

This is with swift-4.2.1 and Yams

          "revision": "26ab35f50ea891e8edefcc9d975db2f6b67e1d68",
          "version": "1.0.1"

I see references to all these null tags in

XCTAssertEqual(resolver.resolveTag(of: "null"), .str)
and so I'm wondering: is there something wrong with my test setup above? Or is this perhaps a 🐛?

@finestructure
Copy link
Contributor Author

PS: For reference, this passes:

    func test_null_json() throws {
        let d = """
            {
                "n1": null,
            }
            """.data(using: .utf8)!
        struct Test: Decodable {
            let n1: String?
        }
        let t = try JSONDecoder().decode(Test.self, from: d)
        XCTAssertNil(t.n1)
    }

@norio-nomura
Copy link
Collaborator

Thanks for reporting! This is a bug in YAMLDecoder.
I opened #158 that fixes this issue.

@norio-nomura
Copy link
Collaborator

side note:
YAMLDecoder does not use Resolver and calls construct(from:) directly for each type.
In addition, decodeNil() uses Node.null that calls NSNull.construct(from:).
https://github.com/jpsim/Yams/blob/master/Sources/Yams/Decoder.swift#L223

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants