Skip to content

Commit 8f279ce

Browse files
committed
Add tests around nullable attributes and remove unnecessary encoding code
1 parent c9d3885 commit 8f279ce

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

Sources/JSONAPI/Resource/Attribute.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,6 @@ extension TransformedAttribute {
8080

8181
public func encode(to encoder: Encoder) throws {
8282
var container = encoder.singleValueContainer()
83-
84-
// See note in decode above about the weirdness
85-
// going on here.
86-
// let anyNil: Any? = nil
87-
// let nilRawValue = anyNil as? Transformer.From
88-
// guard rawValue != nilRawValue else {
89-
// try container.encodeNil()
90-
// return
91-
// }
9283

9384
try container.encode(rawValue)
9485
}

Tests/JSONAPITests/Attribute/AttributeTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ class AttributeTests: XCTestCase {
2929
func test_TransformedAttributeReversNoThrow() {
3030
XCTAssertNoThrow(try TransformedAttribute<String, TestTransformer>(transformedValue: 10))
3131
}
32+
33+
func test_NullableIsNullIfNil() {
34+
struct Wrapper: Codable {
35+
let dummy: Attribute<String?>
36+
}
37+
let data = encoded(value: Wrapper(dummy: .init(value: nil)))
38+
let string = String(data: data, encoding: .utf8)!
39+
40+
XCTAssertEqual(string, "{\"dummy\":null}")
41+
}
42+
43+
func test_NullableIsEqualToNonNullableIfNotNil() {
44+
struct Wrapper1: Codable {
45+
let dummy: Attribute<String?>
46+
}
47+
struct Wrapper2: Codable {
48+
let dummy: Attribute<String>
49+
}
50+
let data1 = encoded(value: Wrapper1(dummy: .init(value: "hello")))
51+
let data2 = encoded(value: Wrapper2(dummy: .init(value: "hello")))
52+
53+
XCTAssertEqual(data1, data2)
54+
}
3255
}
3356

3457
// MARK: Test types

Tests/JSONAPITests/Entity/EntityTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ extension EntityTests {
207207
XCTAssertNil(entity[\.maybeHere])
208208
XCTAssertNil(entity[\.maybeNull])
209209
XCTAssertNoThrow(try TestEntity6.check(entity))
210+
211+
print(encodable: entity)
210212
}
211213

212214
func test_entityOneNullAndOneOmittedAttribute_encode() {

0 commit comments

Comments
 (0)