Skip to content

Commit

Permalink
Fixed a bug in which certain characters, such as the + character, w…
Browse files Browse the repository at this point in the history
…ere not being properly percent-encoded in `Dictionary.formURLEncoded()`.
  • Loading branch information
Peter-Schorn committed Dec 26, 2024
1 parent f4747d0 commit 29329ba
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1610"
LastUpgradeVersion = "1620"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion .swiftpm/xcode/xcshareddata/xcschemes/SpotifyAPI.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1610"
LastUpgradeVersion = "1620"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.0.1] - 12/25/2024

* Fixed a bug in which certain characters, such as the `+` character, were not being properly percent-encoded in `Dictionary.formURLEncoded()`. See [#70](https://github.com/Peter-Schorn/SpotifyAPI/pull/70).

## [4.0.0] - 12-10-2024

* Added `SpotifyAPI.audiobookChapters(_:market:limit:offset:)`.
Expand Down
20 changes: 15 additions & 5 deletions Sources/SpotifyWebAPI/Other/MiscellaneousUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ public extension Dictionary where Key == String, Value == String {
utf-8 character encoding.
*/
func formURLEncoded() -> Data? {

let formDataString = self.map { key, value in
key.addingPercentEncoding(
withAllowedCharacters: .formURLAllowed
)! + "=" + value.addingPercentEncoding(
withAllowedCharacters: .formURLAllowed
)!
}.joined(separator: "&")

var urlComponents = URLComponents()
urlComponents.queryItems = self.map { item in
URLQueryItem(name: item.key, value: item.value)
}
return (urlComponents.percentEncodedQuery ?? "").data(using: .utf8)
return formDataString.data(using: .utf8)

}

}
Expand Down Expand Up @@ -225,6 +230,11 @@ public extension CharacterSet {
.urlPathAllowed
)

/// The characters that are allowed in a form-urlencoded string.
static let formURLAllowed = CharacterSet(
charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"
)

}

/// Generates an array of offests to get each page of results.
Expand Down

0 comments on commit 29329ba

Please sign in to comment.