nerdishbynature / octokit.swift

A Swift API Client for GitHub and GitHub Enterprise
MIT License
490 stars 126 forks source link

"Invalid URL string" exception when retrieving all the issues from a repo #75

Closed rubiojr closed 5 years ago

rubiojr commented 5 years ago

⚠️ Pretty new to Swift, so it's certainly possible the following doesn't make sense at all...

Sample code:

import OctoKit
import Foundation

let config = TokenConfiguration("secret")
var done = false
let client = Octokit(config)

client.issues(owner: "rubiojr", repository: "test") { response in
    switch response {
    case .success(let issues):
        print(issues[0].labelsURL!)
    case .failure(let error):
        print(error)
    }
    done = true
}

repeat {
    RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1))
} while !done

The exception:

Compile Swift Module 'OctoKit' (14 sources)
Compile Swift Module 'labbelier' (1 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/labbelier
dataCorrupted(Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "labels_url", intValue: nil)], debugDescription: "Invalid URL string.", underlyingError: nil))

I believe this happens because it may return something like https://api.github.com/repos/rubiojr/test/issues/3/labels{/name} as the value of labels_url (see https://developer.github.com/v3/issues/#list-issues-for-a-repository) and URL doesn't like that? Something like:

import Foundation

struct Example: Decodable {
    let url: URL?
}

let foo = try JSONDecoder().decode(Example.self, from: """
    { "url": "https://api.github.com/repos/rubiojr/test/issues/3/labels{/name}" }
    """.data(using: .utf8)!)

Causes:

Fatal error: Error raised at top level: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "url", intValue: nil)], debugDescription: "Invalid URL string.", underlyingError: nil)): file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang_Fall2018/swiftlang_Fall2018-1000.11.42/src/swift/stdlib/public/core/ErrorType.swift, line 191

As a workaround, I'm using a local copy of the lib changing https://github.com/nerdishbynature/octokit.swift/blob/a3b0ead0e5ebcfe5be27d3e25cf76f07c4834e24/OctoKit/Issue.swift#L16 to use a String? instead, which seems to be working as expected but, is there a better way?

swift --version
Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)
Target: x86_64-apple-darwin18.2.0
pietbrauer commented 5 years ago

Hey, thanks for bringing this up and welcome to Swift 👋 .

It seems definitely like the labels_url is not accepted by the URL

I looked into this and the GitHub Team is working on a new Labels API. Maybe the best would be to remove the labelsURL from the repository property and add some new routes to Octokit.swift and use the new labels API.

I would welcome a PullRequest, if you or someone else feels like it.

rubiojr commented 5 years ago

Thanks @pietbrauer, I’ll give it a try.