Allow params field to be omitted in requests for methods that don't require them (#17)
* Allow `params` field to be omitted in requests for methods with empty parameters * Add ping request to roundtrip test * Generalize decoding of non-required parameters
This commit is contained in:
parent
3e628db6d8
commit
6f1f65a392
1 changed files with 206 additions and 25 deletions
|
|
@ -2,7 +2,13 @@ import Foundation
|
|||
|
||||
private let jsonrpc = "2.0"
|
||||
|
||||
public struct Empty: Hashable, Codable, Sendable {}
|
||||
public protocol NotRequired {
|
||||
init()
|
||||
}
|
||||
|
||||
public struct Empty: NotRequired, Hashable, Codable, Sendable {
|
||||
public init() {}
|
||||
}
|
||||
|
||||
// MARK: -
|
||||
|
||||
|
|
@ -78,14 +84,11 @@ public struct Request<M: Method>: Hashable, Identifiable, Codable, Sendable {
|
|||
try container.encode(jsonrpc, forKey: .jsonrpc)
|
||||
try container.encode(id, forKey: .id)
|
||||
try container.encode(method, forKey: .method)
|
||||
if M.Parameters.self != Empty.self {
|
||||
try container.encode(params, forKey: .params)
|
||||
} else {
|
||||
// Encode empty object for Empty parameters
|
||||
try container.encode(Empty(), forKey: .params)
|
||||
}
|
||||
try container.encode(params, forKey: .params)
|
||||
}
|
||||
}
|
||||
|
||||
extension Request {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
let version = try container.decode(String.self, forKey: .jsonrpc)
|
||||
|
|
@ -95,15 +98,11 @@ public struct Request<M: Method>: Hashable, Identifiable, Codable, Sendable {
|
|||
}
|
||||
id = try container.decode(ID.self, forKey: .id)
|
||||
method = try container.decode(String.self, forKey: .method)
|
||||
if M.Parameters.self == Empty.self {
|
||||
if (try? container.decodeNil(forKey: .params)) != nil {
|
||||
params = Empty() as! M.Parameters
|
||||
} else if (try? container.decode(Empty.self, forKey: .params)) != nil {
|
||||
params = Empty() as! M.Parameters
|
||||
} else {
|
||||
// If params field is missing, use Empty
|
||||
params = Empty() as! M.Parameters
|
||||
}
|
||||
|
||||
if M.Parameters.self is NotRequired.Type {
|
||||
params =
|
||||
(try container.decodeIfPresent(M.Parameters.self, forKey: .params)
|
||||
?? (M.Parameters.self as! NotRequired.Type).init() as! M.Parameters)
|
||||
} else {
|
||||
params = try container.decode(M.Parameters.self, forKey: .params)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue