CoreOffice / XMLCoder

Easy XML parsing using Codable protocols in Swift
https://coreoffice.github.io/XMLCoder/
MIT License
801 stars 112 forks source link

NSXMLParserErrorDomain Code=111 but works in lldb #271

Open piotrekjeremicz opened 1 year ago

piotrekjeremicz commented 1 year ago

Hi everybody!

I found a very strange problem. My goal is to load a simple XML file from the web. I am downloading data and cleaning it. At the end, my app tries to parse it with a specific model. When the app is running, I am getting an error: The operation couldn’t be completed. (NSXMLParserErrorDomain error 111.). When I make a breakpoint on decode(_:) function and call the same method in lldb everything is working, and I am getting the result.

How to reproduce

  1. So, for example. I am getting XML data from some source. Could be this for our purposes: https://www.wwdcnotes.com/feed.rss

  2. I am removing <content> tags because sample code breaks decoder.

    func dataMiddleware(_ data: Data) -> Data {
        let stringData = String(data: data, encoding: .utf8)?
            .replacingOccurrences(of: "(<content:encoded>)([\\s\\S]*?)(</content:encoded>)", with: "", options: .regularExpression)
    
        return stringData?.data(using: .utf8) ?? data
    }
  3. After this transformation this specific xml looks like:

    
    <?xml version = "1.0" encoding="UTF-8"?>
    <rss version="2.0"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>WWDC NOTES</title>
        <description>Open-source and community-driven effort to collect notes for all Apple's WWDC videos. ❤️</description>
        <link>https://www.wwdcnotes.com</link>
        <language>en</language>
        <lastBuildDate>Sun, 3 Sep 2023 20:06:50 +0200</lastBuildDate>
        <pubDate>Sun, 3 Sep 2023 20:06:50 +0200</pubDate>
        <ttl>250</ttl>
        <atom:link href="https://www.wwdcnotes.com/feed.rss" rel="self" type="application/rss+xml"/>
        <item>
            <guid isPermaLink="true">https://www.wwdcnotes.com/notes/wwdc23/10257</guid>
            <title>Create animated symbols</title>
            <description>Discover animation presets and learn how to use them with SF Symbols and custom symbols. We'll show you how to experiment with different options and configurations to find the perfect animation for your app. Learn how to update custom symbols for animation using annotation features, find out how to modify your custom symbols with symbol components, and explore the redesigned export process to help keep symbols looking great on all platforms.

To get the most out of this session, check out “What’s new in SF Symbols 5” from WWDC23. https://www.wwdcnotes.com/notes/wwdc23/10257

Sun, 3 Sep 2023 16:38:00 +0200
    </item>
</channel>


4. Next I am decoding the data into model:
```swift
let feed = try decoder.decode(SomeModel.self, from: dataMiddleware(data))

struct SomeModel {
    let channel: Channel

    struct Channel: Codable {
        enum CodingKeys: String, CodingKey {
            case title, link, lastBuildDate
            case items = "item"
        }

        let title: String
        let link: URL
        let lastBuildDate: Date
        let items: [Item]

        struct Item: Codable {
            let title: String
            let link: URL
            let guid: String
            let pubDate: Date
        }
    }
}

Results

  1. When I am calling lldb command I am getting proper output:
    
    (lldb) po try decoder.decode(SomeModel.self, from: dataMiddleware(data))
    ▿ SomeModel
    ▿ channel : Channel
    - title : "WWDC NOTES"
    ▿ link : https://www.wwdcnotes.com
      - _url : https://www.wwdcnotes.com
    ▿ lastBuildDate : 2023-09-03 18:06:50 +0000
      - timeIntervalSinceReferenceDate : 715457210.0
    ▿ items : 100 elements
      ▿ 0 : Item
        - title : "Create animated symbols"
        ▿ link : https://www.wwdcnotes.com/notes/wwdc23/10257
          - _url : https://www.wwdcnotes.com/notes/wwdc23/10257
        - guid : "https://www.wwdcnotes.com/notes/wwdc23/10257"
        ▿ pubDate : 2023-09-03 14:38:00 +0000
          - timeIntervalSinceReferenceDate : 715444680.0

2. The same code is running in Vapor scope and the same function throws an error:

[ INFO ] The operation couldn’t be completed. (NSXMLParserErrorDomain error 111.) (App/Service.swift:46) [ WARNING ] Error Domain=NSXMLParserErrorDomain Code=111 "(null)" (App/services.swift:35)



### Summary
I can not find anything that helps me to solve this problem. Error 111 looks like it indicates a bad data structure but these work properly during debugging.
pushkardeshmukh1992 commented 1 year ago

Any workaround for this?