Skip to content

Commit ea4a8b1

Browse files
committed
PropertyListEncoder: Reference the property on the class itself instead of the options struct
1 parent e5cff55 commit ea4a8b1

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

Sources/FoundationEssentials/PropertyList/PlistEncoder.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ open class PropertyListEncoder {
9595
/// - throws: `EncodingError.invalidValue` if a non-conforming floating-point value is encountered during encoding, and the encoding strategy is `.throw`.
9696
/// - throws: An error if any value throws an error during encoding.
9797
open func encode<Value : Encodable>(_ value: Value) throws -> Data {
98+
let format = self.outputFormat
9899
do {
99-
switch options.outputFormat {
100+
switch format {
100101
case .binary:
101102
return try _encodeBPlist(value)
102103
case .xml:
@@ -105,7 +106,7 @@ open class PropertyListEncoder {
105106
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Property list format .openStep not supported for writing"])
106107
#if FOUNDATION_FRAMEWORK
107108
@unknown default:
108-
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Unknown property list format \(options.outputFormat)"])
109+
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Unknown property list format \(format)"])
109110
#endif
110111
}
111112
} catch {
@@ -166,8 +167,9 @@ open class PropertyListEncoder {
166167

167168
@available(FoundationPreview 0.1, *)
168169
open func encode<T : EncodableWithConfiguration>(_ value: T, configuration: T.EncodingConfiguration) throws -> Data {
170+
let format = self.outputFormat
169171
do {
170-
switch options.outputFormat {
172+
switch format {
171173
case .binary:
172174
return try _encodeBPlist(value, configuration: configuration)
173175
case .xml:
@@ -176,7 +178,7 @@ open class PropertyListEncoder {
176178
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Property list format .openStep not supported for writing"])
177179
#if FOUNDATION_FRAMEWORK
178180
@unknown default:
179-
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Unknown property list format \(options.outputFormat)"])
181+
throw CocoaError(.propertyListWriteInvalid, userInfo: [NSDebugDescriptionErrorKey:"Unknown property list format \(format)"])
180182
#endif
181183
}
182184
} catch {

Tests/FoundationEssentialsTests/PropertyListEncoderTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,15 @@ data1 = <7465
15031503
_testRoundTrip(of: input, in: .xml)
15041504
}
15051505
}
1506+
1507+
func testCustomSubclass() throws {
1508+
// verify we consult the subclass for the output format
1509+
let encodeMe = ["hello":"world"]
1510+
let encoder = XMLOnlyEncoder()
1511+
let data = try encoder.encode(encodeMe)
1512+
let dataAsStr = String(data: data, encoding: .utf8)!
1513+
XCTAssertTrue(dataAsStr.hasPrefix("<?xml"))
1514+
}
15061515
}
15071516

15081517

@@ -2036,3 +2045,11 @@ private struct MultipleDecodeOptionsTestType : Codable, Equatable {
20362045
}
20372046
}
20382047

2048+
// MARK: - Helper Class
2049+
2050+
class XMLOnlyEncoder : PropertyListEncoder, @unchecked Sendable {
2051+
override var outputFormat: PropertyListDecoder.PropertyListFormat {
2052+
get { return .xml }
2053+
set { }
2054+
}
2055+
}

0 commit comments

Comments
 (0)