Skip to content

Commit 5b8627d

Browse files
author
Oleksii Dykan
committed
If the value pushed a container before throwing, pop it back off to restore state
1 parent 1384814 commit 5b8627d

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

CodableFirebase/Encoder.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,20 @@ extension _FirebaseEncoder {
393393
}
394394

395395
// The value should request a container from the _FirebaseEncoder.
396-
let depth = storage.count
397-
try value.encode(to: self)
396+
let depth = self.storage.count
397+
do {
398+
try value.encode(to: self)
399+
} catch {
400+
// If the value pushed a container before throwing, pop it back off to restore state.
401+
if self.storage.count > depth {
402+
let _ = self.storage.popContainer()
403+
}
404+
405+
throw error
406+
}
398407

399408
// The top container should be a new container.
400-
guard storage.count > depth else {
409+
guard self.storage.count > depth else {
401410
return nil
402411
}
403412

0 commit comments

Comments
 (0)