@@ -114,10 +114,20 @@ public struct OpenAPIValueContainer: Codable, Hashable, Sendable {
114114 self . init ( validatedValue: item)
115115 } else if let item = try ? container. decode ( String . self) {
116116 self . init ( validatedValue: item)
117- } else if let item = try ? container. decode ( [ OpenAPIValueContainer ] . self) {
118- self . init ( validatedValue: item. map ( \. value) )
119- } else if let item = try ? container. decode ( [ String : OpenAPIValueContainer ] . self) {
120- self . init ( validatedValue: item. mapValues ( \. value) )
117+ } else if var container = try ? decoder. unkeyedContainer ( ) {
118+ var items : [ ( any Sendable ) ? ] = [ ]
119+ if let count = container. count { items. reserveCapacity ( count) }
120+ while !container. isAtEnd {
121+ let item = try container. decode ( OpenAPIValueContainer . self)
122+ items. append ( item. value)
123+ }
124+ self . init ( validatedValue: items)
125+ } else if let container = try ? decoder. container ( keyedBy: StringKey . self) {
126+ let keyValuePairs = try container. allKeys. map { key -> ( String , ( any Sendable ) ? ) in
127+ let item = try container. decode ( OpenAPIValueContainer . self, forKey: key)
128+ return ( key. stringValue, item. value)
129+ }
130+ self . init ( validatedValue: Dictionary ( uniqueKeysWithValues: keyValuePairs) )
121131 } else {
122132 throw DecodingError . dataCorruptedError (
123133 in: container,
@@ -133,36 +143,53 @@ public struct OpenAPIValueContainer: Codable, Hashable, Sendable {
133143 /// - Parameter encoder: The encoder to which the value should be encoded.
134144 /// - Throws: An error if the encoding process encounters issues or if the value is invalid.
135145 public func encode( to encoder: any Encoder ) throws {
136- var container = encoder. singleValueContainer ( )
137146 guard let value = value else {
147+ var container = encoder. singleValueContainer ( )
138148 try container. encodeNil ( )
139149 return
140150 }
141151 #if canImport(Foundation)
142152 if value is NSNull {
153+ var container = encoder. singleValueContainer ( )
143154 try container. encodeNil ( )
144155 return
145156 }
146157 #if canImport(CoreFoundation)
147158 if let nsNumber = value as? NSNumber {
159+ var container = encoder. singleValueContainer ( )
148160 try encode ( nsNumber, to: & container)
149161 return
150162 }
151163 #endif
152164 #endif
153165 switch value {
154- case let value as Bool : try container. encode ( value)
155- case let value as Int : try container. encode ( value)
156- case let value as Double : try container. encode ( value)
157- case let value as String : try container. encode ( value)
166+ case let value as Bool :
167+ var container = encoder. singleValueContainer ( )
168+ try container. encode ( value)
169+ case let value as Int :
170+ var container = encoder. singleValueContainer ( )
171+ try container. encode ( value)
172+ case let value as Double :
173+ var container = encoder. singleValueContainer ( )
174+ try container. encode ( value)
175+ case let value as String :
176+ var container = encoder. singleValueContainer ( )
177+ try container. encode ( value)
158178 case let value as [ ( any Sendable ) ? ] :
159- try container. encode ( value. map ( OpenAPIValueContainer . init ( validatedValue: ) ) )
179+ var container = encoder. unkeyedContainer ( )
180+ for item in value {
181+ let containerItem = OpenAPIValueContainer ( validatedValue: item)
182+ try container. encode ( containerItem)
183+ }
160184 case let value as [ String : ( any Sendable ) ? ] :
161- try container. encode ( value. mapValues ( OpenAPIValueContainer . init ( validatedValue: ) ) )
185+ var container = encoder. container ( keyedBy: StringKey . self)
186+ for (itemKey, itemValue) in value {
187+ try container. encode ( OpenAPIValueContainer ( validatedValue: itemValue) , forKey: StringKey ( itemKey) )
188+ }
162189 default :
163190 throw EncodingError . invalidValue (
164191 value,
165- . init( codingPath: container . codingPath, debugDescription: " OpenAPIValueContainer cannot be encoded " )
192+ . init( codingPath: encoder . codingPath, debugDescription: " OpenAPIValueContainer cannot be encoded " )
166193 )
167194 }
168195 }
@@ -357,36 +384,29 @@ public struct OpenAPIObjectContainer: Codable, Hashable, Sendable {
357384
358385 // MARK: Decodable
359386
360- /// Creates an `OpenAPIValueContainer` by decoding it from a single-value container in a given decoder.
361- ///
362- /// - Parameter decoder: The decoder used to decode the container.
363- /// - Throws: An error if the decoding process encounters an issue or if the data does not match the expected format.
387+ // swift-format-ignore: AllPublicDeclarationsHaveDocumentation
364388 public init ( from decoder: any Decoder ) throws {
365- let container = try decoder. singleValueContainer ( )
366- let item = try container. decode ( [ String : OpenAPIValueContainer ] . self)
367- self . init ( validatedValue: item. mapValues ( \. value) )
389+ let container = try decoder. container ( keyedBy: StringKey . self)
390+ let keyValuePairs = try container. allKeys. map { key -> ( String , ( any Sendable ) ? ) in
391+ let item = try container. decode ( OpenAPIValueContainer . self, forKey: key)
392+ return ( key. stringValue, item. value)
393+ }
394+ self . init ( validatedValue: Dictionary ( uniqueKeysWithValues: keyValuePairs) )
368395 }
369396
370397 // MARK: Encodable
371398
372- /// Encodes the `OpenAPIValueContainer` into a format that can be stored or transmitted via the given encoder.
373- ///
374- /// - Parameter encoder: The encoder used to perform the encoding.
375- /// - Throws: An error if the encoding process encounters an issue or if the data does not match the expected format.
399+ // swift-format-ignore: AllPublicDeclarationsHaveDocumentation
376400 public func encode( to encoder: any Encoder ) throws {
377- var container = encoder. singleValueContainer ( )
378- try container. encode ( value. mapValues ( OpenAPIValueContainer . init ( validatedValue: ) ) )
401+ var container = encoder. container ( keyedBy: StringKey . self)
402+ for (itemKey, itemValue) in value {
403+ try container. encode ( OpenAPIValueContainer ( validatedValue: itemValue) , forKey: StringKey ( itemKey) )
404+ }
379405 }
380406
381407 // MARK: Equatable
382408
383- /// Compares two `OpenAPIObjectContainer` instances for equality by comparing their inner key-value dictionaries.
384- ///
385- /// - Parameters:
386- /// - lhs: The left-hand side `OpenAPIObjectContainer` to compare.
387- /// - rhs: The right-hand side `OpenAPIObjectContainer` to compare.
388- ///
389- /// - Returns: `true` if the `OpenAPIObjectContainer` instances are equal, `false` otherwise.
409+ // swift-format-ignore: AllPublicDeclarationsHaveDocumentation
390410 public static func == ( lhs: OpenAPIObjectContainer , rhs: OpenAPIObjectContainer ) -> Bool {
391411 let lv = lhs. value
392412 let rv = rhs. value
@@ -401,9 +421,7 @@ public struct OpenAPIObjectContainer: Codable, Hashable, Sendable {
401421
402422 // MARK: Hashable
403423
404- /// Hashes the `OpenAPIObjectContainer` instance into the provided `Hasher`.
405- ///
406- /// - Parameter hasher: The `Hasher` into which the hash value is combined.
424+ // swift-format-ignore: AllPublicDeclarationsHaveDocumentation
407425 public func hash( into hasher: inout Hasher ) {
408426 for (key, itemValue) in value {
409427 hasher. combine ( key)
@@ -474,9 +492,14 @@ public struct OpenAPIArrayContainer: Codable, Hashable, Sendable {
474492 /// - Parameter decoder: The decoder to use for decoding the array of values.
475493 /// - Throws: An error if the decoding process fails or if the decoded values cannot be validated.
476494 public init ( from decoder: any Decoder ) throws {
477- let container = try decoder. singleValueContainer ( )
478- let item = try container. decode ( [ OpenAPIValueContainer ] . self)
479- self . init ( validatedValue: item. map ( \. value) )
495+ var container = try decoder. unkeyedContainer ( )
496+ var items : [ ( any Sendable ) ? ] = [ ]
497+ if let count = container. count { items. reserveCapacity ( count) }
498+ while !container. isAtEnd {
499+ let item = try container. decode ( OpenAPIValueContainer . self)
500+ items. append ( item. value)
501+ }
502+ self . init ( validatedValue: items)
480503 }
481504
482505 // MARK: Encodable
@@ -486,8 +509,11 @@ public struct OpenAPIArrayContainer: Codable, Hashable, Sendable {
486509 /// - Parameter encoder: The encoder to use for encoding the array of values.
487510 /// - Throws: An error if the encoding process fails.
488511 public func encode( to encoder: any Encoder ) throws {
489- var container = encoder. singleValueContainer ( )
490- try container. encode ( value. map ( OpenAPIValueContainer . init ( validatedValue: ) ) )
512+ var container = encoder. unkeyedContainer ( )
513+ for item in value {
514+ let containerItem = OpenAPIValueContainer ( validatedValue: item)
515+ try container. encode ( containerItem)
516+ }
491517 }
492518
493519 // MARK: Equatable
0 commit comments