From 5b8627dbbe58fc45a4895686079c2bedeea248ac Mon Sep 17 00:00:00 2001 From: Oleksii Dykan Date: Tue, 20 Feb 2018 23:00:29 +0100 Subject: [PATCH] If the value pushed a container before throwing, pop it back off to restore state --- CodableFirebase/Encoder.swift | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/CodableFirebase/Encoder.swift b/CodableFirebase/Encoder.swift index 5a2096b..69fe0ef 100644 --- a/CodableFirebase/Encoder.swift +++ b/CodableFirebase/Encoder.swift @@ -393,11 +393,20 @@ extension _FirebaseEncoder { } // The value should request a container from the _FirebaseEncoder. - let depth = storage.count - try value.encode(to: self) + let depth = self.storage.count + do { + try value.encode(to: self) + } catch { + // If the value pushed a container before throwing, pop it back off to restore state. + if self.storage.count > depth { + let _ = self.storage.popContainer() + } + + throw error + } // The top container should be a new container. - guard storage.count > depth else { + guard self.storage.count > depth else { return nil }