Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [],
Expand Down Expand Up @@ -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 }
}
Expand Down
35 changes: 27 additions & 8 deletions Tests/XMLCoderTests/AdvancedFeatures/InlinePropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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),
"""
Expand All @@ -91,6 +109,7 @@ final class InlinePropertyTests: XCTestCase {
<simple attr="n1_a1">
<val>n1_v1</val>
</simple>
<attributesOnly Name="attr" />
</container>
"""
)
Expand Down
2 changes: 1 addition & 1 deletion Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class XMLElementTests: XCTestCase {
)

XCTAssertEqual(result, """
<Input xmlns="https://example.com"><Nested xmlns:xsi="https://example.com" xsi:someName="nestedAttrValue"></Nested></Input>
<Input xmlns="https://example.com"><Nested xmlns:xsi="https://example.com" xsi:someName="nestedAttrValue" /></Input>
""")
}

Expand Down