Skip to content

Commit 9a3038d

Browse files
committed
Precondition for the mismatch container type when requesing for the nested container with the same key
1 parent 1750e04 commit 9a3038d

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

stdlib/public/Darwin/Foundation/JSONEncoder.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,10 @@ fileprivate struct _JSONKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCon
500500

501501
public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> {
502502
let containerKey = _converted(key).stringValue
503-
let dictionary = self.container[containerKey] as? NSMutableDictionary ?? NSMutableDictionary()
503+
let existingContainer = self.container[containerKey]
504+
precondition(existingContainer is NSMutableDictionary?,
505+
"Attempt to request for keyed container with the key that previously unkeyed container already requested.")
506+
let dictionary = existingContainer as? NSMutableDictionary ?? NSMutableDictionary()
504507
self.container[containerKey] = dictionary
505508

506509
self.codingPath.append(key)
@@ -512,7 +515,10 @@ fileprivate struct _JSONKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCon
512515

513516
public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
514517
let containerKey = _converted(key).stringValue
515-
let array = self.container[containerKey] as? NSMutableArray ?? NSMutableArray()
518+
let existingContainer = self.container[containerKey]
519+
precondition(existingContainer is NSMutableArray?,
520+
"Attempt to request for unkeyed container with the key that previously keyed container already requested.")
521+
let array = existingContainer as? NSMutableArray ?? NSMutableArray()
516522
self.container[containerKey] = array
517523

518524
self.codingPath.append(key)

stdlib/public/Darwin/Foundation/PlistEncoder.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ fileprivate struct _PlistKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCo
266266

267267
public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> {
268268
let containerKey = key.stringValue
269-
let dictionary = self.container[containerKey] as? NSMutableDictionary ?? NSMutableDictionary()
269+
let existingContainer = self.container[containerKey]
270+
precondition(existingContainer is NSMutableDictionary?,
271+
"Attempt to request for keyed container with the key that previously unkeyed container already requested.")
272+
let dictionary = existingContainer as? NSMutableDictionary ?? NSMutableDictionary()
270273
self.container[containerKey] = dictionary
271274

272275
self.codingPath.append(key)
@@ -278,7 +281,10 @@ fileprivate struct _PlistKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCo
278281

279282
public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
280283
let containerKey = key.stringValue
281-
let array = self.container[containerKey] as? NSMutableArray ?? NSMutableArray()
284+
let existingContainer = self.container[containerKey]
285+
precondition(existingContainer is NSMutableArray?,
286+
"Attempt to request for unkeyed container with the key that previously keyed container already requested.")
287+
let array = existingContainer as? NSMutableArray ?? NSMutableArray()
282288
self.container[containerKey] = array
283289

284290
self.codingPath.append(key)

0 commit comments

Comments
 (0)