Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Sources/OpenAPIRuntime/Base/ContentDisposition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
//
//===----------------------------------------------------------------------===//

// Full Foundation needed for String.trimmingCharacters
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

/// A parsed representation of the `content-disposition` header described by RFC 6266 containing only
/// the features relevant to OpenAPI multipart bodies.
Expand Down Expand Up @@ -104,15 +107,15 @@ extension ContentDisposition: RawRepresentable {
/// https://datatracker.ietf.org/doc/html/rfc6266#section-4.1
/// - Parameter rawValue: The raw value to use for the new instance.
init?(rawValue: String) {
var components = rawValue.split(separator: ";").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
var components = rawValue.split(separator: ";").map { $0.trimmingLeadingAndTrailingSpaces }
guard !components.isEmpty else { return nil }
self.dispositionType = DispositionType(rawValue: components.removeFirst())
let parameterTuples: [(ParameterName, String)] = components.compactMap {
(component: String) -> (ParameterName, String)? in
let parameterComponents = component.split(separator: "=", maxSplits: 1)
.map { $0.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) }
.map { $0.trimmingLeadingAndTrailingSpaces }
guard parameterComponents.count == 2 else { return nil }
let valueWithoutQuotes = parameterComponents[1].trimmingCharacters(in: ["\""])
let valueWithoutQuotes = parameterComponents[1].trimming(while: { $0 == "\"" })
return (.init(rawValue: parameterComponents[0]), valueWithoutQuotes)
}
self.parameters = Dictionary(parameterTuples, uniquingKeysWith: { a, b in a })
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenAPIRuntime/Conversion/Converter+Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ extension Converter {
let acceptValues = acceptHeader.split(separator: ",")
.map { value in
// Drop everything after the optional semicolon (q, extensions, ...)
value.split(separator: ";")[0].trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
value.split(separator: ";")[0].trimmingLeadingAndTrailingSpaces.lowercased()
}
if acceptValues.isEmpty { return }
guard let parsedSubstring = OpenAPIMIMEType(substring) else {
Expand Down
18 changes: 15 additions & 3 deletions Sources/OpenAPIRuntime/Conversion/FoundationExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@
//
//===----------------------------------------------------------------------===//

#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

extension String {

extension StringProtocol {
/// Returns the string with leading and trailing whitespace (such as spaces
/// and newlines) removed.
var trimmingLeadingAndTrailingSpaces: Self { trimmingCharacters(in: .whitespacesAndNewlines) }
var trimmingLeadingAndTrailingSpaces: String { self.trimming { $0.isWhitespace } }

/// Returns a new string by removing leading and trailing characters
/// that satisfy the given predicate.
func trimming(while predicate: (Character) -> Bool) -> String {
guard let start = self.firstIndex(where: { !predicate($0) }) else { return "" }
let end = self.lastIndex(where: { !predicate($0) })!

return String(self[start...end])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftOpenAPIGenerator open source project
//
// Copyright (c) 2026 Apple Inc. and the SwiftOpenAPIGenerator project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import Foundation
@testable import OpenAPIRuntime

final class Test_FoundationExtensions: Test_Runtime {

func testTrimmingMatchesFoundationBehavior() {
let testCases = [
" Hello World ", // Standard spaces
"\n\tHello World\r\n", // Newlines and tabs
"NoTrimmingNeeded", // No whitespace
" ", // Only spaces
"", // Empty string
" Hello\nWorld ", // Internal whitespace (should stay)
"\u{00A0}Unicode\u{00A0}", // Non-breaking space
"\u{3000}Ideographic\u{3000}", // Japanese/Chinese full-width space
]

for input in testCases {
let foundationResult = input.trimmingCharacters(in: .whitespacesAndNewlines)
let swiftNativeResult = input.trimmingLeadingAndTrailingSpaces

XCTAssertEqual(swiftNativeResult, foundationResult, "Failed for input: \(input)")
}
}

func testGenericTrimming() {
let numericString = "0001234500"
let result = numericString.trimming(while: { $0 == "0" })
XCTAssertEqual(result, "12345")

let punctuationString = "...Hello!.."
let puncResult = punctuationString.trimming(while: { $0.isPunctuation })
XCTAssertEqual(puncResult, "Hello")
}
}
Loading