Skip to content

Commit 2ee233c

Browse files
committed
fix AnyCodable encoding of nil
1 parent 852e86d commit 2ee233c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Sources/OpenAPIKitCore/AnyCodable/AnyCodable.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,24 @@ public struct AnyCodable: @unchecked Sendable {
4949
}
5050
}
5151

52+
protocol _Optional {
53+
var isNil: Bool { get }
54+
}
55+
56+
extension Optional: _Optional {
57+
var isNil: Bool { return self == nil }
58+
}
59+
5260
extension AnyCodable: Encodable {
5361
public func encode(to encoder: Encoder) throws {
5462
var container = encoder.singleValueContainer()
5563

64+
// special nil case
65+
if let optionalValue = value as? _Optional, optionalValue.isNil {
66+
try container.encodeNil()
67+
return
68+
}
69+
5670
switch value {
5771
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
5872
case let number as NSNumber:

Tests/OpenAPIKitCoreTests/URLTemplate/URLTemplateTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ extension URLTemplateTests {
391391
}
392392
393393
""".utf8)
394-
let document = try JSONDecoder().decode(
394+
let _ = try JSONDecoder().decode(
395395
StackFoo.self,
396396
from: data
397397
)

0 commit comments

Comments
 (0)