From 901768709584f395267303c6cedd0f9ad570c9c0 Mon Sep 17 00:00:00 2001 From: Toomas Vahter Date: Sat, 4 Jan 2025 11:17:44 +0200 Subject: [PATCH] Do not store empty dictionary when updating PollDTO.custom and PollOptionDTO.custom because it leads to errors in decoding later (#3551) --- Sources/StreamChat/Database/DTOs/PollDTO.swift | 7 ++++++- Sources/StreamChat/Database/DTOs/PollOptionDTO.swift | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Sources/StreamChat/Database/DTOs/PollDTO.swift b/Sources/StreamChat/Database/DTOs/PollDTO.swift index d052f3444c..e4206c39c9 100644 --- a/Sources/StreamChat/Database/DTOs/PollDTO.swift +++ b/Sources/StreamChat/Database/DTOs/PollDTO.swift @@ -155,7 +155,6 @@ extension NSManagedObjectContext { pollDto.name = payload.name pollDto.updatedAt = payload.updatedAt.bridgeDate pollDto.voteCount = payload.voteCount - pollDto.custom = try JSONEncoder.default.encode(payload.custom) pollDto.voteCountsByOption = payload.voteCountsByOption pollDto.isClosed = payload.isClosed ?? false if let maxVotesAllowed = payload.maxVotesAllowed { @@ -163,6 +162,12 @@ extension NSManagedObjectContext { } pollDto.votingVisibility = payload.votingVisibility + if let custom = payload.custom, !custom.isEmpty { + pollDto.custom = try JSONEncoder.default.encode(custom) + } else { + pollDto.custom = nil + } + if let userPayload = payload.createdBy { pollDto.createdBy = try saveUser(payload: userPayload, query: nil, cache: cache) } else { diff --git a/Sources/StreamChat/Database/DTOs/PollOptionDTO.swift b/Sources/StreamChat/Database/DTOs/PollOptionDTO.swift index 4c78fcfe18..4e83ac8e2b 100644 --- a/Sources/StreamChat/Database/DTOs/PollOptionDTO.swift +++ b/Sources/StreamChat/Database/DTOs/PollOptionDTO.swift @@ -93,7 +93,11 @@ extension NSManagedObjectContext { cache: cache ) dto.text = payload.text - dto.custom = try JSONEncoder.default.encode(payload.custom) + if let custom = payload.custom, !custom.isEmpty { + dto.custom = try JSONEncoder.default.encode(custom) + } else { + dto.custom = nil + } return dto }