Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDATA Decoding not working #168

Closed
Kirow opened this issue Apr 9, 2020 · 3 comments · Fixed by #170
Closed

CDATA Decoding not working #168

Kirow opened this issue Apr 9, 2020 · 3 comments · Fixed by #170
Labels
bug Something isn't working

Comments

@Kirow
Copy link

Kirow commented Apr 9, 2020

struct Container: Codable {
    let value: Int
    let data: String
}

let xmlString =
"""
<container>
   <value>42</value>
   <data><![CDATA[lorem ipsum]]></data>
</container>
"""
let xmlData = xmlString.data(using: .utf8)!
let result = try decoder.decode(Container.self, from: xmlData)

result.data decoded into "". Actually there is an error in XMLDecoder.swift:212 - Expected String but found null instead.

If we go deeper - KeyedBox.swift:27 return elements[""].first as? SimpleBox. It will return nil because key is "#CDATA" in this case.

I've made simple code change that fixed problem in this case, but have no idea what it can break later.

.patch
diff --git a/Sources/XMLCoder/Auxiliaries/Box/KeyedBox.swift b/Sources/XMLCoder/Auxiliaries/Box/KeyedBox.swift
index 9930bfb..a3e8742 100644
--- a/Sources/XMLCoder/Auxiliaries/Box/KeyedBox.swift
+++ b/Sources/XMLCoder/Auxiliaries/Box/KeyedBox.swift
@@ -24,7 +24,7 @@ struct KeyedBox {
     }
 
     var value: SimpleBox? {
-        return elements[""].first as? SimpleBox
+        return elements.values.first as? SimpleBox
     }
 }
 
diff --git a/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift b/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift
index 0615ca2..a3fcb50 100644
--- a/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift
+++ b/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift
@@ -23,6 +23,10 @@ struct KeyedStorage<Key: Hashable & Comparable, Value> {
     var keys: [Key] {
         return buffer.map { $0.0 }
     }
+    
+    var values: [Value] {
+        return buffer.map { $0.1 }
+    }
 
     init<S>(_ sequence: S) where S: Sequence, S.Element == (Key, Value) {
         buffer = Buffer()

MaxDesiatov added a commit that referenced this issue Apr 9, 2020
@MaxDesiatov MaxDesiatov added the bug Something isn't working label Apr 9, 2020
MaxDesiatov added a commit that referenced this issue Apr 9, 2020
@MaxDesiatov
Copy link
Collaborator

Thanks for reporting it @Kirow, the fix for it will be included in the next release.

@kasperkronborg
Copy link

Hi @MaxDesiatov,

For when is the next release scheduled? Waiting in anticipation for this.

Thanks for a great XML coder!

@MaxDesiatov
Copy link
Collaborator

Hey @kasperkronborg, this is now published in the 0.11 release and is available with SwiftPM, CocoPods and Carthage. Thanks for your feedback, I appreciate your support 🙂

arjungupta0107 pushed a commit to salido/XMLCoder that referenced this issue Jun 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants