Skip to content

URLQueryItem does not properly implement Hashable #4737

Open
@jbelkins

Description

@jbelkins

The URLQueryItem type does not conform to the requirements of the Hashable protocol, in that equal values may have differing hashes.

To demonstrate, consider this code sample:

import Foundation

let x1 = URLQueryItem(name: "abc", value: "def")
let x2 = URLQueryItem(name: "abc", value: "def")
let x3 = URLQueryItem(name: "abc", value: "def")

if x1 == x2 && x2 == x3 && x3 == x1 {
    print("x1, x2, x3 are all equal by ==")
} else {
    print("x1, x2, x3 are not all equal by ==")
}

if x1.hashValue == x2.hashValue && x2.hashValue == x3.hashValue && x3.hashValue == x1.hashValue {
    print("x1, x2, x3 all have equal hash values")
} else {
    print("x1, x2, x3 do not all have equal hash values")
}

Run this Swift code on Mac, then on Linux, and compare outputs.

On Mac, you get:

x1, x2, x3 are all equal by ==
x1, x2, x3 all have equal hash values

This is as expected.

But on Linux, you get:

x1, x2, x3 are all equal by ==
x1, x2, x3 do not all have equal hash values

The Linux behavior is a violation of the requirements of Hashable since values that are equal by == have differing hash values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions