Skip to content

Commit

Permalink
Fix empty additionalProperties dictionary encoding (#103)
Browse files Browse the repository at this point in the history
### Motivation

Fixes apple/swift-openapi-generator#525.

Turns out that the mere act of creating a decoding container is
meaningful and we skipped it as an optimization, causing JSONDecoder to
fail for empty dictionaries when used in additional properties.

### Modifications

Remove the extra guards that skipped creating a container, even when we
already know there are no elements.

### Result

No more failures when encoding empty dictionaries in
additionalProperties.

### Test Plan

Tested manually as this requirement seems to be coming out of
JSONDecoder.
  • Loading branch information
czechboy0 authored Apr 3, 2024
1 parent a875c2d commit 634b7eb
Showing 1 changed file with 0 additions and 2 deletions.
2 changes: 0 additions & 2 deletions Sources/OpenAPIRuntime/Conversion/CodableExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
/// - Parameter additionalProperties: A container of additional properties.
/// - Throws: An error if there are issues with encoding the additional properties.
public func encodeAdditionalProperties(_ additionalProperties: OpenAPIObjectContainer) throws {
guard !additionalProperties.value.isEmpty else { return }
var container = container(keyedBy: StringKey.self)
for (key, value) in additionalProperties.value {
try container.encode(OpenAPIValueContainer(unvalidatedValue: value), forKey: .init(key))
Expand All @@ -116,7 +115,6 @@
/// - Parameter additionalProperties: A container of additional properties.
/// - Throws: An error if there are issues with encoding the additional properties.
public func encodeAdditionalProperties<T: Encodable>(_ additionalProperties: [String: T]) throws {
guard !additionalProperties.isEmpty else { return }
var container = container(keyedBy: StringKey.self)
for (key, value) in additionalProperties { try container.encode(value, forKey: .init(key)) }
}
Expand Down

0 comments on commit 634b7eb

Please sign in to comment.