Skip to content

Compatibility behaviors for Swift URL #1113

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

Merged
merged 1 commit into from
Jan 8, 2025

Conversation

jrflat
Copy link
Contributor

@jrflat jrflat commented Jan 8, 2025

This PR addresses main behavioral differences of the new Swift URL implementation in order to preserve bincompat.

  • Adds a compatibility path to the parser allowing the scheme to be empty for URL. This was the behavior in the old implementation, and can cause URL to return different components for URL strings starting with :.
  • Returns nil host for URLs like https:/// instead of returning empty string. RFC 3986 suggests this should be an empty string, but I don't think this conformance outweighs compatibility here.
  • Linked-on-or-after-check where URL(filePath:) will check for full file: and http: URL strings that were incorrectly passed. It drops the file: scheme or passes the http: string to URL(string:) instead. It also logs an "API MISUSE" fault in these cases. This fixes apps that rely on old buggy behavior where passing a full URL string as filePath could happen to work itself out if a different API like .appendingPathComponent(_:) is called immediately after.
  • Makes .appending(component:) behave like .appending(path:) for file URLs, so that it does not percent-encode / to %2F. The old implementation had a bug where this actually didn't happen for file URLs, so we should maintain that old behavior to prevent breakage.
  • Restores URL(string:relativeTo:) returning nil for empty string and instead uses an internal URL(stringOrEmpty:relativeTo:) in cases where we do need to initialize with the empty string, such as when .deletingLastPathComponent() of a single-component path. We can still pass the empty string RFC tests using URL(stringOrEmpty:relativeTo:).
  • Restores file path initializers' behavior of replacing an empty input path with ./ or .

@jrflat
Copy link
Contributor Author

jrflat commented Jan 8, 2025

@swift-ci please test

@jrflat jrflat requested review from parkera and jmschonfeld January 8, 2025 18:17
Copy link
Contributor

@parkera parkera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good compromise for compatibility.

@jrflat jrflat merged commit b1b2add into swiftlang:main Jan 8, 2025
3 checks passed
jrflat added a commit to jrflat/swift-foundation that referenced this pull request Mar 5, 2025
jrflat added a commit to jrflat/swift-foundation that referenced this pull request Mar 5, 2025
parkera pushed a commit that referenced this pull request Mar 5, 2025
* (141549683) Restore behavior of URL(string: "") returning nil (#1103)

* (142076445) Allow URL.standardized to return an empty string URL (#1110)

* (142076445) Allow URL.standardized to return an empty string URL

* Add ?? self to prevent force-unwrap

* (142446243) Compatibility behaviors for Swift URL (#1113)

* (142589056) URLComponents.string should percent-encode colons in first path segment if needed (#1117)

* (142667792) URL.absoluteString crashes if baseURL starts with colon (#1119)

* (143159003) Don't encode colon if URLComponents path starts with colon (#1139)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants