diff --git a/Sources/JSONAPI/Resource/Attribute.swift b/Sources/JSONAPI/Resource/Attribute.swift index ebf1e83..e59b3f9 100644 --- a/Sources/JSONAPI/Resource/Attribute.swift +++ b/Sources/JSONAPI/Resource/Attribute.swift @@ -80,15 +80,6 @@ extension TransformedAttribute { public func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() - - // See note in decode above about the weirdness - // going on here. -// let anyNil: Any? = nil -// let nilRawValue = anyNil as? Transformer.From -// guard rawValue != nilRawValue else { -// try container.encodeNil() -// return -// } try container.encode(rawValue) } diff --git a/Tests/JSONAPITests/Attribute/AttributeTests.swift b/Tests/JSONAPITests/Attribute/AttributeTests.swift index bf4f3f0..06a9c5d 100644 --- a/Tests/JSONAPITests/Attribute/AttributeTests.swift +++ b/Tests/JSONAPITests/Attribute/AttributeTests.swift @@ -29,6 +29,29 @@ class AttributeTests: XCTestCase { func test_TransformedAttributeReversNoThrow() { XCTAssertNoThrow(try TransformedAttribute(transformedValue: 10)) } + + func test_NullableIsNullIfNil() { + struct Wrapper: Codable { + let dummy: Attribute + } + let data = encoded(value: Wrapper(dummy: .init(value: nil))) + let string = String(data: data, encoding: .utf8)! + + XCTAssertEqual(string, "{\"dummy\":null}") + } + + func test_NullableIsEqualToNonNullableIfNotNil() { + struct Wrapper1: Codable { + let dummy: Attribute + } + struct Wrapper2: Codable { + let dummy: Attribute + } + let data1 = encoded(value: Wrapper1(dummy: .init(value: "hello"))) + let data2 = encoded(value: Wrapper2(dummy: .init(value: "hello"))) + + XCTAssertEqual(data1, data2) + } } // MARK: Test types diff --git a/Tests/JSONAPITests/Entity/EntityTests.swift b/Tests/JSONAPITests/Entity/EntityTests.swift index b290cc6..8e9acb3 100644 --- a/Tests/JSONAPITests/Entity/EntityTests.swift +++ b/Tests/JSONAPITests/Entity/EntityTests.swift @@ -207,6 +207,8 @@ extension EntityTests { XCTAssertNil(entity[\.maybeHere]) XCTAssertNil(entity[\.maybeNull]) XCTAssertNoThrow(try TestEntity6.check(entity)) + + print(encodable: entity) } func test_entityOneNullAndOneOmittedAttribute_encode() {