diff --git a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
index 1b1d8a0f..ab7f901f 100644
--- a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
+++ b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
@@ -36,6 +36,11 @@ struct XMLCoderElement: Equatable, Sendable {
return key.isEmpty
}
+ private var isPracticallyEmpty: Bool {
+ guard stringValue == nil else { return false }
+ return elements.allSatisfy { $0.key.isEmpty && $0.isPracticallyEmpty }
+ }
+
init(
key: String,
elements: [XMLCoderElement] = [],
@@ -313,7 +318,7 @@ struct XMLCoderElement: Equatable, Sendable {
formatXMLAttributes(formatting, &string, escapedCharacters.attributes)
}
- if !elements.isEmpty || formatting.contains(.noEmptyElements) {
+ if !isPracticallyEmpty || formatting.contains(.noEmptyElements) {
let hasOnlyIntrinsicContent = elements.allSatisfy { element in
element.key.isEmpty && !element.elements.contains { !$0.key.isEmpty }
}
diff --git a/Tests/XMLCoderTests/AdvancedFeatures/InlinePropertyTests.swift b/Tests/XMLCoderTests/AdvancedFeatures/InlinePropertyTests.swift
index a0085711..a7fe268f 100644
--- a/Tests/XMLCoderTests/AdvancedFeatures/InlinePropertyTests.swift
+++ b/Tests/XMLCoderTests/AdvancedFeatures/InlinePropertyTests.swift
@@ -15,22 +15,25 @@ import XCTest
private enum InlineChoice: Equatable, Codable {
case simple(Nested1)
case nested(Nested1, labeled: Nested2)
-
+ case attributesOnly(AttributesOnly)
+
enum CodingKeys: String, CodingKey, XMLChoiceCodingKey {
- case simple, nested
+ case simple, nested, attributesOnly
}
-
+
enum SimpleCodingKeys: String, CodingKey { case _0 = "" }
-
+
enum NestedCodingKeys: String, CodingKey {
case _0 = ""
case labeled
}
-
+
+ enum AttributesOnlyCodingKeys: String, CodingKey { case _0 = "" }
+
struct Nested1: Equatable, Codable, DynamicNodeEncoding {
var attr = "n1_a1"
var val = "n1_v1"
-
+
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
switch key {
case CodingKeys.attr: return .attribute
@@ -42,6 +45,18 @@ private enum InlineChoice: Equatable, Codable {
struct Nested2: Equatable, Codable {
var val = "n2_val"
}
+
+ struct AttributesOnly: Equatable, Codable, DynamicNodeEncoding {
+ var name = "attr"
+
+ enum CodingKeys: String, CodingKey {
+ case name = "Name"
+ }
+
+ public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
+ return .attribute
+ }
+ }
}
final class InlinePropertyTests: XCTestCase {
@@ -75,9 +90,12 @@ final class InlinePropertyTests: XCTestCase {
encoder.outputFormatting = .prettyPrinted
encoder.prettyPrintIndentation = .spaces(4)
- let original: [InlineChoice] = [.nested(.init(), labeled: .init()), .simple(.init())]
+ let original: [InlineChoice] = [
+ .nested(.init(), labeled: .init()),
+ .simple(.init()),
+ .attributesOnly(.init())
+ ]
let encoded = try encoder.encode(original, withRootKey: "container")
- print(String(data: encoded, encoding: .utf8)!)
XCTAssertEqual(
String(data: encoded, encoding: .utf8),
"""
@@ -91,6 +109,7 @@ final class InlinePropertyTests: XCTestCase {
n1_v1
+
"""
)
diff --git a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift
index 1825efcb..e7639de0 100644
--- a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift
+++ b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift
@@ -94,7 +94,7 @@ class XMLElementTests: XCTestCase {
)
XCTAssertEqual(result, """
-
+
""")
}