Skip to content

URL.appending(queryItems:) adds a ? when passing in an empty array of query items #1548

@BrentMifsud

Description

@BrentMifsud

Describe the bug
Passing in an empty array of query items to URL.appending(queryItems: [URLQueryItem]) adds a ? to the end of the url. Resulting in a potentially invalid URL.

To Reproduce

let url = "www.google.com"
let updatedUrl = url.appending(queryItems: [])
print(updatedUrl) // "www.google.com?"

Expected behavior
If no query items are provided, the url should not be changed.

Configuration (please complete the following information):

  • Swift Version: 6.2 (Xcode 26.1 beta 2)
  • OS: iOS
  • OS Version: iOS 26.0.1

Regression information:
n/a

Additional context

Probably a minor issue, but I am currently building out an HTTP Client package for my current job.

I had to write an extension on url to get around this issue:

public extension HTTPRequest {
    private func constructURL(from baseURL: URL) -> URL {
        var url = baseURL.appending(path: path)

        if !queryItems.isEmpty {
            url = url.appending(queryItems: queryItems)
        }

        return url
    }
}

instead of being able to rely on:

var url = baseURL.appending(path: path).appending(queryItems: queryItems)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions