Review this generated order decoder against its boundary contract.
Decode orders whose id and total are required and whose date is ISO 8601. A missing coupon becomes empty, explicit null is rejected, failures retain diagnostic paths, and raw payloads are never logged.
swift
import Foundation
struct IncomingOrder: Decodable {
let id: Int
let coupon: String
let createdAt: Date
let totalCents: Int
enum CodingKeys: String, CodingKey { case id, coupon, createdAt, totalCents }
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
id = (try? values.decode(Int.self, forKey: .id)) ?? 0
coupon = try values.decodeIfPresent(String.self, forKey: .coupon) ?? ""
createdAt = try values.decode(Date.self, forKey: .createdAt)
totalCents = try values.decode(Int.self, forKey: .totalCents)
}
}
func decodeOrder(_ data: Data) -> IncomingOrder? {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
print("order payload:", String(decoding: data, as: UTF8.self))
return try? decoder.decode(IncomingOrder.self, from: data)
}
generated code is illustrative, not from any one model