diff --git a/README.md b/README.md index ff133972..7565c433 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Add the package dependency in your `Package.swift`: ```swift .package( url: "https://github.com/apple/swift-openapi-runtime", - .upToNextMinor(from: "0.1.0") + .upToNextMinor(from: "0.2.0") ), ``` diff --git a/Sources/OpenAPIRuntime/Conversion/CurrencyExtensions.swift b/Sources/OpenAPIRuntime/Conversion/CurrencyExtensions.swift index 5b444431..fe0b9c25 100644 --- a/Sources/OpenAPIRuntime/Conversion/CurrencyExtensions.swift +++ b/Sources/OpenAPIRuntime/Conversion/CurrencyExtensions.swift @@ -15,13 +15,7 @@ import Foundation extension HeaderField: CustomStringConvertible { public var description: String { - let value: String - if HeaderField.internalRedactedHeaderFields.contains(name.lowercased()) { - value = "" - } else { - value = self.value - } - return "\(name): \(value)" + "\(name): \(value)" } } diff --git a/Sources/OpenAPIRuntime/Deprecated/Deprecated.swift b/Sources/OpenAPIRuntime/Deprecated/Deprecated.swift index 8d04bc44..cb20c7e6 100644 --- a/Sources/OpenAPIRuntime/Deprecated/Deprecated.swift +++ b/Sources/OpenAPIRuntime/Deprecated/Deprecated.swift @@ -14,2137 +14,3 @@ import Foundation // MARK: - Functionality to be removed in the future - -/// A wrapper of a body value with its content type. -@available(*, deprecated) -@_spi(Generated) -public struct EncodableBodyContent: Equatable { - - /// An encodable body value. - public var value: T - - /// The header value of the content type, for example `application/json`. - public var contentType: String - - /// Creates a new content wrapper. - /// - Parameters: - /// - value: An encodable body value. - /// - contentType: The header value of the content type. - public init( - value: T, - contentType: String - ) { - self.value = value - self.contentType = contentType - } -} - -extension Converter { - /// Gets a deserialized value from body data. - /// - Parameters: - /// - type: Type used to decode the data. - /// - data: Encoded body data. - /// - transform: Closure for transforming the Decodable type into a final type. - /// - Returns: Deserialized body value. - @available(*, deprecated) - public func bodyGet( - _ type: T.Type, - from data: Data, - transforming transform: (T) -> C - ) throws -> C { - let decoded = try decoder.decode(type, from: data) - return transform(decoded) - } - - /// Gets a deserialized value from body data. - /// - Parameters: - /// - type: Type used to decode the data. - /// - data: Encoded body data. - /// - transform: Closure for transforming the Decodable type into a final type. - /// - Returns: Deserialized body value. - @available(*, deprecated) - public func bodyGet( - _ type: Data.Type, - from data: Data, - transforming transform: (Data) -> C - ) throws -> C { - return transform(data) - } - - /// Gets a deserialized value from body data, if present. - /// - Parameters: - /// - type: Type used to decode the data. - /// - data: Encoded body data. - /// - transform: Closure for transforming the Decodable type into a final type. - /// - Returns: Deserialized body value, if present. - @available(*, deprecated) - public func bodyGetOptional( - _ type: T.Type, - from data: Data?, - transforming transform: (T) -> C - ) throws -> C? { - guard let data else { - return nil - } - let decoded = try decoder.decode(type, from: data) - return transform(decoded) - } - - /// Gets a deserialized value from body data. - /// - Parameters: - /// - type: Type used to decode the data. - /// - data: Encoded body data. - /// - transform: Closure for transforming the Decodable type into a final type. - /// - Returns: Deserialized body value. - @available(*, deprecated) - public func bodyGetRequired( - _ type: T.Type, - from data: Data?, - transforming transform: (T) -> C - ) throws -> C { - guard let data else { - throw RuntimeError.missingRequiredRequestBody - } - let decoded = try decoder.decode(type, from: data) - return transform(decoded) - } - - /// Gets a deserialized value from body data, if present. - /// - Parameters: - /// - type: Type used to decode the data. - /// - data: Encoded body data. - /// - transform: Closure for transforming the Decodable type into a final type. - /// - Returns: Deserialized body value, if present. - @available(*, deprecated) - public func bodyGetOptional( - _ type: Data.Type, - from data: Data?, - transforming transform: (Data) -> C - ) throws -> C? { - guard let data else { - return nil - } - return transform(data) - } - - /// Gets a deserialized value from body data. - /// - Parameters: - /// - type: Type used to decode the data. - /// - data: Encoded body data. - /// - transform: Closure for transforming the Decodable type into a final type. - /// - Returns: Deserialized body value. - @available(*, deprecated) - public func bodyGetRequired( - _ type: Data.Type, - from data: Data?, - transforming transform: (Data) -> C - ) throws -> C { - guard let data else { - throw RuntimeError.missingRequiredRequestBody - } - return transform(data) - } - - /// Adds a header field with the provided name and Date value. - /// - Parameters: - /// - headerFields: Collection of header fields to add to. - /// - name: The name of the header field. - /// - value: Date value. If nil, header is not added. - @available(*, deprecated) - public func headerFieldAdd( - in headerFields: inout [HeaderField], - name: String, - value: Date? - ) throws { - guard let value = value else { - return - } - let stringValue = try self.configuration.dateTranscoder.encode(value) - headerFields.add(name: name, value: stringValue) - } - - /// Returns the value for the first header field with given name. - /// - Parameters: - /// - headerFields: Collection of header fields to retrieve the field from. - /// - name: The name of the header field (case-insensitive). - /// - type: Date type. - /// - Returns: First value for the given name, if one exists. - @available(*, deprecated) - public func headerFieldGetOptional( - in headerFields: [HeaderField], - name: String, - as type: Date.Type - ) throws -> Date? { - guard let dateString = headerFields.firstValue(name: name) else { - return nil - } - return try self.configuration.dateTranscoder.decode(dateString) - } - - /// Returns the value for the first header field with the given name. - /// - Parameters: - /// - headerFields: Collection of header fields to retrieve the field from. - /// - name: Header name (case-insensitive). - /// - type: Date type. - /// - Returns: First value for the given name. - @available(*, deprecated) - public func headerFieldGetRequired( - in headerFields: [HeaderField], - name: String, - as type: Date.Type - ) throws -> Date { - guard - let value = try headerFieldGetOptional( - in: headerFields, - name: name, - as: type - ) - else { - throw RuntimeError.missingRequiredHeaderField(name) - } - return value - } - - /// Adds a header field with the provided name and encodable value. - /// - /// Encodes the value into minimized JSON. - /// - Parameters: - /// - headerFields: Collection of header fields to add to. - /// - name: Header name. - /// - value: Encodable header value. - @available(*, deprecated) - public func headerFieldAdd( - in headerFields: inout [HeaderField], - name: String, - value: T? - ) throws { - guard let value else { - return - } - if let value = value as? (any _StringConvertible) { - headerFields.add(name: name, value: value.description) - return - } - let data = try headerFieldEncoder.encode(value) - let stringValue = String(decoding: data, as: UTF8.self) - headerFields.add(name: name, value: stringValue) - } - - /// Returns the value of the first header field for the given name. - /// - /// Decodes the value from JSON. - /// - Parameters: - /// - headerFields: Collection of header fields to retrieve the field from. - /// - name: Header name (case-insensitive). - /// - type: Date type. - /// - Returns: First value for the given name, if one exists. - @available(*, deprecated) - public func headerFieldGetOptional( - in headerFields: [HeaderField], - name: String, - as type: T.Type - ) throws -> T? { - guard let stringValue = headerFields.firstValue(name: name) else { - return nil - } - if let myType = T.self as? any _StringConvertible.Type { - return myType.init(stringValue).map { $0 as! T } - } - let data = Data(stringValue.utf8) - return try decoder.decode(T.self, from: data) - } - - /// Returns the first header value for the given (case-insensitive) name. - /// - /// Decodes the value from JSON. - /// - Parameters: - /// - headerFields: Collection of header fields to retrieve the field from. - /// - name: Header name (case-insensitive). - /// - type: Date type. - /// - Returns: First value for the given name. - @available(*, deprecated) - public func headerFieldGetRequired( - in headerFields: [HeaderField], - name: String, - as type: T.Type - ) throws -> T { - guard - let value = try headerFieldGetOptional( - in: headerFields, - name: name, - as: type - ) - else { - throw RuntimeError.missingRequiredHeaderField(name) - } - return value - } - - // MARK: Query - _StringConvertible - - /// Adds a query item with a string-convertible value to the request. - /// - Parameters: - /// - request: Request to add the query item. - /// - name: Query item name. - /// - value: Query item string-convertible value. - @available(*, deprecated) - public func queryAdd( - in request: inout Request, - name: String, - value: T? - ) throws { - request.mutatingQuery { components in - components.addQueryItem( - name: name, - value: value - ) - } - } - - // MARK: Query - Date - - /// Adds a query item with a Date value to the request. - /// - Parameters: - /// - request: Request to add the query item. - /// - name: Query item name. - /// - value: Query item Date value. - @available(*, deprecated) - public func queryAdd( - in request: inout Request, - name: String, - value: Date? - ) throws { - try request.mutatingQuery { components in - try components.addQueryItem( - name: name, - value: value.flatMap { value in - try self.configuration.dateTranscoder.encode(value) - } - ) - } - } - - // MARK: Query - Array of _StringConvertible - - /// Adds a query item with a list of string-convertible values to the request. - /// - Parameters: - /// - request: Request to add the query item. - /// - name: Query item name. - /// - value: Query item string-convertible values. - @available(*, deprecated) - public func queryAdd( - in request: inout Request, - name: String, - value: [T]? - ) throws { - request.mutatingQuery { components in - components.addQueryItem( - name: name, - value: value - ) - } - } - - // MARK: Query - LosslessStringConvertible - - /// Returns a deserialized value for the the first query item - /// found under the provided name. - /// - Parameters: - /// - queryParameters: Query parameters container where the value might exist. - /// - name: Query item name. - /// - type: Query item value type. - /// - Returns: Deserialized query item value, if present. - @available(*, deprecated) - public func queryGetOptional( - in queryParameters: [URLQueryItem], - name: String, - as type: T.Type - ) throws -> T? { - guard let untypedValue = queryParameters.first(where: { $0.name == name })?.value else { - return nil - } - guard let typedValue = T(untypedValue) else { - throw RuntimeError.failedToDecodeStringConvertibleValue( - type: String(describing: T.self) - ) - } - return typedValue - } - - /// Returns a deserialized value for the the first query item - /// found under the provided name. - /// - Parameters: - /// - queryParameters: Query parameters container where the value must exist. - /// - name: Query item name. - /// - type: Query item value type. - /// - Returns: Deserialized query item value. - @available(*, deprecated) - public func queryGetRequired( - in queryParameters: [URLQueryItem], - name: String, - as type: T.Type - ) throws -> T { - guard let untypedValue = queryParameters.first(where: { $0.name == name })?.value else { - throw RuntimeError.missingRequiredQueryParameter(name) - } - guard let typedValue = T(untypedValue) else { - throw RuntimeError.failedToDecodeStringConvertibleValue(type: String(describing: T.self)) - } - return typedValue - } - - // MARK: Query - Date - - /// Returns a deserialized value for the the first query item - /// found under the provided name. - /// - Parameters: - /// - queryParameters: Query parameters container where the value might exist. - /// - name: Query item name. - /// - type: Query item value type. - /// - Returns: Deserialized query item value, if present. - @available(*, deprecated) - public func queryGetOptional( - in queryParameters: [URLQueryItem], - name: String, - as type: Date.Type - ) throws -> Date? { - guard let dateString = queryParameters.first(where: { $0.name == name })?.value else { - return nil - } - return try self.configuration.dateTranscoder.decode(dateString) - } - - /// Returns a deserialized value for the the first query item - /// found under the provided name. - /// - Parameters: - /// - queryParameters: Query parameters container where the value must exist. - /// - name: Query item name. - /// - type: Query item value type. - /// - Returns: Deserialized query item value. - @available(*, deprecated) - public func queryGetRequired( - in queryParameters: [URLQueryItem], - name: String, - as type: Date.Type - ) throws -> Date { - guard let dateString = queryParameters.first(where: { $0.name == name })?.value else { - throw RuntimeError.missingRequiredQueryParameter(name) - } - return try self.configuration.dateTranscoder.decode(dateString) - } - - // MARK: Query - Array of _StringConvertible - - /// Returns an array of deserialized values for all the query items - /// found under the provided name. - /// - Parameters: - /// - queryParameters: Query parameters container where the value might exist. - /// - name: Query item name. - /// - type: Query item value type. - /// - Returns: Deserialized query item value, if present. - @available(*, deprecated) - public func queryGetOptional( - in queryParameters: [URLQueryItem], - name: String, - as type: [T].Type - ) throws -> [T]? { - let items: [T] = - try queryParameters - .filter { $0.name == name } - .compactMap { item in - guard let typedValue = T(item.value ?? "") else { - throw RuntimeError.failedToDecodeStringConvertibleValue( - type: String(describing: T.self) - ) - } - return typedValue - } - guard !items.isEmpty else { - return nil - } - return items - } - - /// Returns an array of deserialized values for all the query items - /// found under the provided name. - /// - Parameters: - /// - queryParameters: Query parameters container where the value must exist. - /// - name: Query item name. - /// - type: Query item value type. - /// - Returns: Deserialized query item value. - @available(*, deprecated) - public func queryGetRequired( - in queryParameters: [URLQueryItem], - name: String, - as type: [T].Type - ) throws -> [T] { - let items: [T] = - try queryParameters - .filter { $0.name == name } - .map { item in - guard let typedValue = T(item.value ?? "") else { - throw RuntimeError.failedToDecodeStringConvertibleValue(type: String(describing: T.self)) - } - return typedValue - } - guard !items.isEmpty else { - throw RuntimeError.missingRequiredQueryParameter(name) - } - return items - } -} - -extension Request { - /// Allows modifying the parsed query parameters of the request. - @available(*, deprecated) - mutating func mutatingQuery(_ closure: (inout URLComponents) throws -> Void) rethrows { - var urlComponents = URLComponents() - if let query { - urlComponents.percentEncodedQuery = query - } - try closure(&urlComponents) - query = urlComponents.percentEncodedQuery - } -} - -extension URLComponents { - /// Adds a query item using the provided name and typed value. - /// - Parameters: - /// - name: Query name. - /// - value: Typed value. - @available(*, deprecated) - mutating func addQueryItem( - name: String, - value: T? - ) { - guard let value = value else { - return - } - queryItems = - (queryItems ?? []) + [ - .init(name: name, value: value.description) - ] - } - - /// Adds query items using the provided name and typed values. - /// - Parameters: - /// - name: Query name. - /// - value: Array of typed values. - @available(*, deprecated) - mutating func addQueryItem( - name: String, - value: [T]? - ) { - guard let items = value else { - return - } - for item in items { - addQueryItem(name: name, value: item) - } - } -} - -/// A wrapper of a body value with its content type. -@_spi(Generated) -@available(*, deprecated, renamed: "EncodableBodyContent") -public struct LegacyEncodableBodyContent: Equatable { - - /// An encodable body value. - public var value: T - - /// The header value of the content type, for example `application/json`. - public var contentType: String - - /// Creates a new content wrapper. - /// - Parameters: - /// - value: An encodable body value. - /// - contentType: The header value of the content type. - public init( - value: T, - contentType: String - ) { - self.value = value - self.contentType = contentType - } -} - -extension Converter { - /// Provides an optional serialized value for the body value. - /// - Parameters: - /// - value: Encodable value to turn into data. - /// - headerFields: Headers container where to add the Content-Type header. - /// - transform: Closure for transforming the Encodable value into body content. - /// - Returns: Data for the serialized body value, or nil if `value` was nil. - @available(*, deprecated, message: "Use the variant with EncodableBodyContent") - public func bodyAddOptional( - _ value: C?, - headerFields: inout [HeaderField], - transforming transform: (C) -> LegacyEncodableBodyContent - ) throws -> Data? { - guard let value else { - return nil - } - return try bodyAddRequired( - value, - headerFields: &headerFields, - transforming: transform - ) - } - - /// Provides a required serialized value for the body value. - /// - Parameters: - /// - value: Encodable value to turn into data. - /// - headerFields: Headers container where to add the Content-Type header. - /// - transform: Closure for transforming the Encodable value into body content. - /// - Returns: Data for the serialized body value. - @available(*, deprecated, message: "Use the variant with EncodableBodyContent") - public func bodyAddRequired( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> LegacyEncodableBodyContent - ) throws -> Data { - let body = transform(value) - headerFields.add(name: "content-type", value: body.contentType) - return try encoder.encode(body.value) - } - - /// Provides an optional serialized value for the body value. - /// - Parameters: - /// - value: Encodable value to turn into data. - /// - headerFields: Headers container where to add the Content-Type header. - /// - transform: Closure for transforming the Encodable value into body content. - /// - Returns: Data for the serialized body value, or nil if `value` was nil. - @available(*, deprecated, message: "Use the variant with EncodableBodyContent") - public func bodyAddOptional( - _ value: C?, - headerFields: inout [HeaderField], - transforming transform: (C) -> LegacyEncodableBodyContent - ) throws -> Data? { - guard let value else { - return nil - } - return try bodyAddRequired( - value, - headerFields: &headerFields, - transforming: transform - ) - } - - /// Provides a required serialized value for the body value. - /// - Parameters: - /// - value: Encodable value to turn into data. - /// - headerFields: Headers container where to add the Content-Type header. - /// - transform: Closure for transforming the Encodable value into body content. - /// - Returns: Data for the serialized body value. - @available(*, deprecated, message: "Use the variant with EncodableBodyContent") - public func bodyAddRequired( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> LegacyEncodableBodyContent - ) throws -> Data { - let body = transform(value) - headerFields.add(name: "content-type", value: body.contentType) - return body.value - } - - /// Provides a serialized value for the provided body value. - /// - Parameters: - /// - value: Encodable value to turn into data. - /// - headerFields: Header fields container where to add the Content-Type header. - /// - transform: Closure for transforming the Encodable value into body content. - /// - Returns: Data for the serialized body value. - @available(*, deprecated, message: "Use the variant with EncodableBodyContent") - public func bodyAdd( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> LegacyEncodableBodyContent - ) throws -> Data { - let body = transform(value) - headerFields.add(name: "content-type", value: body.contentType) - return try encoder.encode(body.value) - } - - /// Provides a serialized value for the provided body value. - /// - Parameters: - /// - value: Encodable value to turn into data. - /// - headers: Headers container where to add the Content-Type header. - /// - transform: Closure for transforming the Encodable value into body content. - /// - Returns: Data for the serialized body value. - @available(*, deprecated, message: "Use the variant with EncodableBodyContent") - public func bodyAdd( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> LegacyEncodableBodyContent - ) throws -> Data { - let body = transform(value) - headerFields.add(name: "content-type", value: body.contentType) - return body.value - } - - /// Returns a deserialized value for the required path variable name. - /// - Parameters: - /// - pathParameters: Path parameters where the value must exist. - /// - name: Path variable name. - /// - type: Path variable type. - /// - Returns: Deserialized path variable value. - @available(*, deprecated) - public func pathGetRequired( - in pathParameters: [String: String], - name: String, - as type: T.Type - ) throws -> T { - guard - let value = try pathGetOptional( - in: pathParameters, - name: name, - as: type - ) - else { - throw RuntimeError.missingRequiredPathParameter(name) - } - return value - } - - /// Returns a deserialized value for the optional path variable name. - /// - Parameters: - /// - pathParameters: Path parameters where the value might exist. - /// - name: Path variable name. - /// - type: Path variable type. - /// - Returns: Deserialized path variable value, if present. - @available(*, deprecated) - public func pathGetOptional( - in pathParameters: [String: String], - name: String, - as type: T.Type - ) throws -> T? { - guard let untypedValue = pathParameters[name] else { - return nil - } - guard let typedValue = T(untypedValue) else { - throw RuntimeError.failedToDecodeStringConvertibleValue(type: String(describing: T.self)) - } - return typedValue - } -} - -extension Converter { - - /// Validates that the Content-Type header field (if present) - /// is compatible with the provided content-type substring. - /// - /// Succeeds if no Content-Type header is found in the response headers. - /// - /// - Parameters: - /// - headerFields: Header fields to inspect for a content type. - /// - substring: Expected content type. - /// - Throws: If the response's Content-Type value is not compatible with - /// the provided substring. - @available(*, deprecated, message: "Use isMatchingContentType instead.") - public func validateContentTypeIfPresent( - in headerFields: [HeaderField], - substring: String - ) throws { - guard let contentType = extractContentTypeIfPresent(in: headerFields) else { - return - } - guard try isMatchingContentType(received: contentType, expectedRaw: substring) else { - throw RuntimeError.unexpectedContentTypeHeader(contentType.description) - } - } - - // | client | set | request body | text | string-convertible | optional | setOptionalRequestBodyAsText | - @available(*, deprecated) - public func setOptionalRequestBodyAsText( - _ value: C?, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data? { - try setOptionalRequestBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertStringConvertibleToTextData - ) - } - - // | client | set | request body | text | string-convertible | required | setRequiredRequestBodyAsText | - @available(*, deprecated) - public func setRequiredRequestBodyAsText( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data { - try setRequiredRequestBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertStringConvertibleToTextData - ) - } - - // | client | set | request body | text | date | optional | setOptionalRequestBodyAsText | - @available(*, deprecated) - public func setOptionalRequestBodyAsText( - _ value: C?, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data? { - try setOptionalRequestBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertDateToTextData - ) - } - - // | client | set | request body | text | date | required | setRequiredRequestBodyAsText | - @available(*, deprecated) - public func setRequiredRequestBodyAsText( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data { - try setRequiredRequestBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertDateToTextData - ) - } - - // | client | set | request body | JSON | codable | optional | setOptionalRequestBodyAsJSON | - @available(*, deprecated) - public func setOptionalRequestBodyAsJSON( - _ value: C?, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data? { - try setOptionalRequestBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertBodyCodableToJSON - ) - } - - // | client | set | request body | JSON | codable | required | setRequiredRequestBodyAsJSON | - @available(*, deprecated) - public func setRequiredRequestBodyAsJSON( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data { - try setRequiredRequestBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertBodyCodableToJSON - ) - } - - // | client | set | request body | binary | data | optional | setOptionalRequestBodyAsBinary | - @available(*, deprecated) - public func setOptionalRequestBodyAsBinary( - _ value: C?, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data? { - try setOptionalRequestBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertDataToBinary - ) - } - - // | client | set | request body | binary | data | required | setRequiredRequestBodyAsBinary | - @available(*, deprecated) - public func setRequiredRequestBodyAsBinary( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data { - try setRequiredRequestBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertDataToBinary - ) - } - - // | server | set | response body | text | string-convertible | required | setResponseBodyAsText | - @available(*, deprecated) - public func setResponseBodyAsText( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data { - try setResponseBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertStringConvertibleToTextData - ) - } - - // | server | set | response body | text | date | required | setResponseBodyAsText | - @available(*, deprecated) - public func setResponseBodyAsText( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data { - try setResponseBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertDateToTextData - ) - } - - // | server | set | response body | JSON | codable | required | setResponseBodyAsJSON | - @available(*, deprecated) - public func setResponseBodyAsJSON( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data { - try setResponseBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertBodyCodableToJSON - ) - } - - // | server | set | response body | binary | data | required | setResponseBodyAsBinary | - @available(*, deprecated) - public func setResponseBodyAsBinary( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent - ) throws -> Data { - try setResponseBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convertDataToBinary - ) - } - - @available(*, deprecated) - public func setRequiredRequestBody( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent, - convert: (T) throws -> Data - ) throws -> Data { - let body = transform(value) - headerFields.add(name: "content-type", value: body.contentType) - let convertibleValue = body.value - return try convert(convertibleValue) - } - - @available(*, deprecated) - public func setOptionalRequestBody( - _ value: C?, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent, - convert: (T) throws -> Data - ) throws -> Data? { - guard let value else { - return nil - } - return try setRequiredRequestBody( - value, - headerFields: &headerFields, - transforming: transform, - convert: convert - ) - } - - @available(*, deprecated) - public func setResponseBody( - _ value: C, - headerFields: inout [HeaderField], - transforming transform: (C) -> EncodableBodyContent, - convert: (T) throws -> Data - ) throws -> Data { - let body = transform(value) - headerFields.add(name: "content-type", value: body.contentType) - let convertibleValue = body.value - return try convert(convertibleValue) - } - - // | client | set | request query | text | string-convertible | both | setQueryItemAsText | - @available(*, deprecated) - public func setQueryItemAsText( - in request: inout Request, - name: String, - value: T? - ) throws { - try setUnescapedQueryItem( - in: &request, - style: nil, - explode: nil, - name: name, - value: value, - convert: { value, _, _ in - try convertStringConvertibleToText(value) - } - ) - } - - // | client | set | request query | text | array of string-convertibles | both | setQueryItemAsText | - @available(*, deprecated) - public func setQueryItemAsText( - in request: inout Request, - name: String, - value: [T]? - ) throws { - try setQueryItems( - in: &request, - style: nil, - explode: nil, - name: name, - values: value, - convert: convertStringConvertibleToText - ) - } - - // | client | set | request query | text | date | both | setQueryItemAsText | - @available(*, deprecated) - public func setQueryItemAsText( - in request: inout Request, - name: String, - value: Date? - ) throws { - try setUnescapedQueryItem( - in: &request, - style: nil, - explode: nil, - name: name, - value: value, - convert: { value, _, _ in - try convertDateToText(value) - } - ) - } - - // | client | set | request query | text | array of dates | both | setQueryItemAsText | - @available(*, deprecated) - public func setQueryItemAsText( - in request: inout Request, - name: String, - value: [Date]? - ) throws { - try setQueryItems( - in: &request, - style: nil, - explode: nil, - name: name, - values: value, - convert: convertDateToText - ) - } - - // | server | get | request query | text | string-convertible | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - public func getOptionalQueryItemAsText( - in queryParameters: [URLQueryItem], - name: String, - as type: T.Type - ) throws -> T? { - try getOptionalQueryItem( - in: queryParameters, - style: nil, - explode: nil, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | server | get | request query | text | string-convertible | required | getRequiredQueryItemAsText | - @available(*, deprecated) - public func getRequiredQueryItemAsText( - in queryParameters: [URLQueryItem], - name: String, - as type: T.Type - ) throws -> T { - try getRequiredQueryItem( - in: queryParameters, - style: nil, - explode: nil, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | server | get | request query | text | array of string-convertibles | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - public func getOptionalQueryItemAsText( - in queryParameters: [URLQueryItem], - name: String, - as type: [T].Type - ) throws -> [T]? { - try getOptionalQueryItems( - in: queryParameters, - style: nil, - explode: nil, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | server | get | request query | text | array of string-convertibles | required | getRequiredQueryItemAsText | - @available(*, deprecated) - public func getRequiredQueryItemAsText( - in queryParameters: [URLQueryItem], - name: String, - as type: [T].Type - ) throws -> [T] { - try getRequiredQueryItems( - in: queryParameters, - style: nil, - explode: nil, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | server | get | request query | text | date | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - public func getOptionalQueryItemAsText( - in queryParameters: [URLQueryItem], - name: String, - as type: Date.Type - ) throws -> Date? { - try getOptionalQueryItem( - in: queryParameters, - style: nil, - explode: nil, - name: name, - as: type, - convert: convertTextToDate - ) - } - - // | server | get | request query | text | date | required | getRequiredQueryItemAsText | - @available(*, deprecated) - public func getRequiredQueryItemAsText( - in queryParameters: [URLQueryItem], - name: String, - as type: Date.Type - ) throws -> Date { - try getRequiredQueryItem( - in: queryParameters, - style: nil, - explode: nil, - name: name, - as: type, - convert: convertTextToDate - ) - } - - // | server | get | request query | text | array of dates | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - public func getOptionalQueryItemAsText( - in queryParameters: [URLQueryItem], - name: String, - as type: [Date].Type - ) throws -> [Date]? { - try getOptionalQueryItems( - in: queryParameters, - style: nil, - explode: nil, - name: name, - as: type, - convert: convertTextToDate - ) - } - - // | server | get | request query | text | array of dates | required | getRequiredQueryItemAsText | - @available(*, deprecated) - public func getRequiredQueryItemAsText( - in queryParameters: [URLQueryItem], - name: String, - as type: [Date].Type - ) throws -> [Date] { - try getRequiredQueryItems( - in: queryParameters, - style: nil, - explode: nil, - name: name, - as: type, - convert: convertTextToDate - ) - } - - // MARK: - Deprecated as part of moving to URI coder - - // | common | set | header field | text | string-convertible | both | setHeaderFieldAsText | - @available(*, deprecated) - public func setHeaderFieldAsText( - in headerFields: inout [HeaderField], - name: String, - value: T? - ) throws { - try setHeaderField( - in: &headerFields, - name: name, - value: value, - convert: convertStringConvertibleToText - ) - } - - // | common | set | header field | text | array of string-convertibles | both | setHeaderFieldAsText | - @available(*, deprecated) - public func setHeaderFieldAsText( - in headerFields: inout [HeaderField], - name: String, - value values: [T]? - ) throws { - try setHeaderFields( - in: &headerFields, - name: name, - values: values, - convert: convertStringConvertibleToText - ) - } - - // | common | set | header field | text | date | both | setHeaderFieldAsText | - @available(*, deprecated) - public func setHeaderFieldAsText( - in headerFields: inout [HeaderField], - name: String, - value: Date? - ) throws { - try setHeaderField( - in: &headerFields, - name: name, - value: value, - convert: convertDateToText - ) - } - - // | common | set | header field | text | array of dates | both | setHeaderFieldAsText | - @available(*, deprecated) - public func setHeaderFieldAsText( - in headerFields: inout [HeaderField], - name: String, - value values: [Date]? - ) throws { - try setHeaderFields( - in: &headerFields, - name: name, - values: values, - convert: convertDateToText - ) - } - - // | common | get | header field | text | string-convertible | optional | getOptionalHeaderFieldAsText | - @available(*, deprecated) - public func getOptionalHeaderFieldAsText( - in headerFields: [HeaderField], - name: String, - as type: T.Type - ) throws -> T? { - try getOptionalHeaderField( - in: headerFields, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | common | get | header field | text | string-convertible | required | getRequiredHeaderFieldAsText | - @available(*, deprecated) - public func getRequiredHeaderFieldAsText( - in headerFields: [HeaderField], - name: String, - as type: T.Type - ) throws -> T { - try getRequiredHeaderField( - in: headerFields, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | common | get | header field | text | array of string-convertibles | optional | getOptionalHeaderFieldAsText | - @available(*, deprecated) - public func getOptionalHeaderFieldAsText( - in headerFields: [HeaderField], - name: String, - as type: [T].Type - ) throws -> [T]? { - try getOptionalHeaderFields( - in: headerFields, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | common | get | header field | text | array of string-convertibles | required | getRequiredHeaderFieldAsText | - @available(*, deprecated) - public func getRequiredHeaderFieldAsText( - in headerFields: [HeaderField], - name: String, - as type: [T].Type - ) throws -> [T] { - try getRequiredHeaderFields( - in: headerFields, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | common | get | header field | text | date | optional | getOptionalHeaderFieldAsText | - @available(*, deprecated) - public func getOptionalHeaderFieldAsText( - in headerFields: [HeaderField], - name: String, - as type: Date.Type - ) throws -> Date? { - try getOptionalHeaderField( - in: headerFields, - name: name, - as: type, - convert: convertHeaderFieldTextToDate - ) - } - - // | common | get | header field | text | date | required | getRequiredHeaderFieldAsText | - @available(*, deprecated) - public func getRequiredHeaderFieldAsText( - in headerFields: [HeaderField], - name: String, - as type: Date.Type - ) throws -> Date { - try getRequiredHeaderField( - in: headerFields, - name: name, - as: type, - convert: convertHeaderFieldTextToDate - ) - } - - // | common | get | header field | text | array of dates | optional | getOptionalHeaderFieldAsText | - @available(*, deprecated) - public func getOptionalHeaderFieldAsText( - in headerFields: [HeaderField], - name: String, - as type: [Date].Type - ) throws -> [Date]? { - try getOptionalHeaderFields( - in: headerFields, - name: name, - as: type, - convert: convertHeaderFieldTextToDate - ) - } - - // | common | get | header field | text | array of dates | required | getRequiredHeaderFieldAsText | - @available(*, deprecated) - public func getRequiredHeaderFieldAsText( - in headerFields: [HeaderField], - name: String, - as type: [Date].Type - ) throws -> [Date] { - try getRequiredHeaderFields( - in: headerFields, - name: name, - as: type, - convert: convertHeaderFieldTextToDate - ) - } - - // | client | set | request path | text | string-convertible | required | renderedRequestPath | - @available(*, deprecated) - public func renderedRequestPath( - template: String, - parameters: [any _StringConvertible] - ) throws -> String { - var renderedString = template - for parameter in parameters { - if let range = renderedString.range(of: "{}") { - renderedString = renderedString.replacingOccurrences( - of: "{}", - with: parameter.description, - range: range - ) - } - } - return renderedString - } - - // | client | set | request query | text | string-convertible | both | setQueryItemAsText | - @available(*, deprecated) - public func setQueryItemAsText( - in request: inout Request, - style: ParameterStyle?, - explode: Bool?, - name: String, - value: T? - ) throws { - try setUnescapedQueryItem( - in: &request, - style: style, - explode: explode, - name: name, - value: value, - convert: { value, _, _ in - try convertStringConvertibleToText(value) - } - ) - } - - // | client | set | request query | text | array of string-convertibles | both | setQueryItemAsText | - @available(*, deprecated) - public func setQueryItemAsText( - in request: inout Request, - style: ParameterStyle?, - explode: Bool?, - name: String, - value: [T]? - ) throws { - try setQueryItems( - in: &request, - style: style, - explode: explode, - name: name, - values: value, - convert: convertStringConvertibleToText - ) - } - - // | client | set | request query | text | date | both | setQueryItemAsText | - @available(*, deprecated) - public func setQueryItemAsText( - in request: inout Request, - style: ParameterStyle?, - explode: Bool?, - name: String, - value: Date? - ) throws { - try setUnescapedQueryItem( - in: &request, - style: style, - explode: explode, - name: name, - value: value, - convert: { value, _, _ in - try convertDateToText(value) - } - ) - } - - // | client | set | request query | text | array of dates | both | setQueryItemAsText | - @available(*, deprecated) - public func setQueryItemAsText( - in request: inout Request, - style: ParameterStyle?, - explode: Bool?, - name: String, - value: [Date]? - ) throws { - try setQueryItems( - in: &request, - style: style, - explode: explode, - name: name, - values: value, - convert: convertDateToText - ) - } - - // | client | get | response body | text | string-convertible | required | getResponseBodyAsText | - @available(*, deprecated) - public func getResponseBodyAsText( - _ type: T.Type, - from data: Data, - transforming transform: (T) -> C - ) throws -> C { - try getResponseBody( - type, - from: data, - transforming: transform, - convert: convertTextDataToStringConvertible - ) - } - - // | client | get | response body | text | date | required | getResponseBodyAsText | - @available(*, deprecated) - public func getResponseBodyAsText( - _ type: Date.Type, - from data: Data, - transforming transform: (Date) -> C - ) throws -> C { - try getResponseBody( - type, - from: data, - transforming: transform, - convert: convertTextDataToDate - ) - } - - // | client | set | request body | text | string-convertible | optional | setOptionalRequestBodyAsText | - @available(*, deprecated) - public func setOptionalRequestBodyAsText( - _ value: T?, - headerFields: inout [HeaderField], - contentType: String - ) throws -> Data? { - try setOptionalRequestBody( - value, - headerFields: &headerFields, - contentType: contentType, - convert: convertStringConvertibleToTextData - ) - } - - // | client | set | request body | text | string-convertible | required | setRequiredRequestBodyAsText | - @available(*, deprecated) - public func setRequiredRequestBodyAsText( - _ value: T, - headerFields: inout [HeaderField], - contentType: String - ) throws -> Data { - try setRequiredRequestBody( - value, - headerFields: &headerFields, - contentType: contentType, - convert: convertStringConvertibleToTextData - ) - } - - // | client | set | request body | text | date | optional | setOptionalRequestBodyAsText | - @available(*, deprecated) - public func setOptionalRequestBodyAsText( - _ value: Date?, - headerFields: inout [HeaderField], - contentType: String - ) throws -> Data? { - try setOptionalRequestBody( - value, - headerFields: &headerFields, - contentType: contentType, - convert: convertDateToTextData - ) - } - - // | client | set | request body | text | date | required | setRequiredRequestBodyAsText | - @available(*, deprecated) - public func setRequiredRequestBodyAsText( - _ value: Date, - headerFields: inout [HeaderField], - contentType: String - ) throws -> Data { - try setRequiredRequestBody( - value, - headerFields: &headerFields, - contentType: contentType, - convert: convertDateToTextData - ) - } - - @available(*, deprecated) - func convertStringConvertibleToText( - _ value: T - ) throws -> String { - value.description - } - - @available(*, deprecated) - func convertStringConvertibleToTextData( - _ value: T - ) throws -> Data { - try Data(convertStringConvertibleToText(value).utf8) - } - - @available(*, deprecated) - func convertDateToText(_ value: Date) throws -> String { - try configuration.dateTranscoder.encode(value) - } - - @available(*, deprecated) - func convertDateToTextData(_ value: Date) throws -> Data { - try Data(convertDateToText(value).utf8) - } - - @available(*, deprecated) - func convertTextToDate(_ stringValue: String) throws -> Date { - try configuration.dateTranscoder.decode(stringValue) - } - - @available(*, deprecated) - func convertTextDataToDate(_ data: Data) throws -> Date { - let stringValue = String(decoding: data, as: UTF8.self) - return try convertTextToDate(stringValue) - } - - @available(*, deprecated) - func convertHeaderFieldTextToDate(_ stringValue: String) throws -> Date { - try convertTextToDate(stringValue) - } - - @available(*, deprecated) - func convertTextToStringConvertible( - _ stringValue: String - ) throws -> T { - guard let value = T.init(stringValue) else { - throw RuntimeError.failedToDecodeStringConvertibleValue( - type: String(describing: T.self) - ) - } - return value - } - - @available(*, deprecated) - func convertTextDataToStringConvertible( - _ data: Data - ) throws -> T { - let stringValue = String(decoding: data, as: UTF8.self) - return try convertTextToStringConvertible(stringValue) - } - - @available(*, deprecated) - func setHeaderFields( - in headerFields: inout [HeaderField], - name: String, - values: [T]?, - convert: (T) throws -> String - ) throws { - guard let values else { - return - } - for value in values { - headerFields.add( - name: name, - value: try convert(value) - ) - } - } - - @available(*, deprecated) - func getOptionalHeaderFields( - in headerFields: [HeaderField], - name: String, - as type: [T].Type, - convert: (String) throws -> T - ) throws -> [T]? { - let values = headerFields.values(name: name) - if values.isEmpty { - return nil - } - return try values.map { value in try convert(value) } - } - - @available(*, deprecated) - func getRequiredHeaderFields( - in headerFields: [HeaderField], - name: String, - as type: [T].Type, - convert: (String) throws -> T - ) throws -> [T] { - let values = headerFields.values(name: name) - if values.isEmpty { - throw RuntimeError.missingRequiredHeaderField(name) - } - return try values.map { value in try convert(value) } - } - - @available(*, deprecated) - func setQueryItems( - in request: inout Request, - style: ParameterStyle?, - explode: Bool?, - name: String, - values: [T]?, - convert: (T) throws -> String - ) throws { - guard let values else { - return - } - let (_, resolvedExplode) = try ParameterStyle.resolvedQueryStyleAndExplode( - name: name, - style: style, - explode: explode - ) - for value in values { - request.addUnescapedQueryItem( - name: name, - value: try convert(value), - explode: resolvedExplode - ) - } - } - - @available(*, deprecated) - func getOptionalQueryItems( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: [T].Type, - convert: (String) throws -> T - ) throws -> [T]? { - let (_, resolvedExplode) = try ParameterStyle.resolvedQueryStyleAndExplode( - name: name, - style: style, - explode: explode - ) - let untypedValues = - queryParameters - .filter { $0.name == name } - .map { $0.value ?? "" } - // If explode is false, some of the items might have multiple - // comma-separate values, so we need to split them here. - let processedValues: [String] - if resolvedExplode { - processedValues = untypedValues - } else { - processedValues = untypedValues.flatMap { multiValue in - multiValue - .split(separator: ",", omittingEmptySubsequences: false) - .map(String.init) - } - } - return try processedValues.map(convert) - } - - @available(*, deprecated) - func getRequiredQueryItems( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: [T].Type, - convert: (String) throws -> T - ) throws -> [T] { - guard - let values = try getOptionalQueryItems( - in: queryParameters, - style: style, - explode: explode, - name: name, - as: type, - convert: convert - ), !values.isEmpty - else { - throw RuntimeError.missingRequiredQueryParameter(name) - } - return values - } - - @available(*, deprecated) - func setUnescapedQueryItem( - in request: inout Request, - style: ParameterStyle?, - explode: Bool?, - name: String, - value: T?, - convert: (T, ParameterStyle, Bool) throws -> String - ) throws { - guard let value else { - return - } - let (resolvedStyle, resolvedExplode) = try ParameterStyle.resolvedQueryStyleAndExplode( - name: name, - style: style, - explode: explode - ) - request.addUnescapedQueryItem( - name: name, - value: try convert(value, resolvedStyle, resolvedExplode), - explode: resolvedExplode - ) - } -} - -extension Request { - /// Adds the provided name and value to the URL's query. - /// - Parameters: - /// - name: The name of the query item. - /// - value: The value of the query item. - /// - explode: A Boolean value indicating whether query items with the - /// same name should be provided as separate key-value pairs (`true`) or - /// if all the values for one key should be concatenated with a comma - /// and provided as a single key-value pair (`false`). - @available(*, deprecated) - mutating func addUnescapedQueryItem(name: String, value: String, explode: Bool) { - mutateQuery { urlComponents in - urlComponents.addUnescapedStringQueryItem( - name: name, - value: value, - explode: explode - ) - } - } -} - -extension URLComponents { - - /// Adds the provided name and value to the URL's query. - /// - Parameters: - /// - name: The name of the query item. - /// - value: The value of the query item. - /// - explode: A Boolean value indicating whether query items with the - /// same name should be provided as separate key-value pairs (`true`) or - /// if all the values for one key should be concatenated with a comma - /// and provided as a single key-value pair (`false`). - @available(*, deprecated) - mutating func addUnescapedStringQueryItem( - name: String, - value: String, - explode: Bool - ) { - if explode { - queryItems = - (queryItems ?? []) + [ - .init(name: name, value: value) - ] - return - } - // When explode is false, we need to collect all the potential existing - // values from the array with the same name, add the new one, and - // concatenate them with a comma. - let originalQueryItems = queryItems ?? [] - struct GroupedQueryItems { - var matchingValues: [String] = [] - var otherItems: [URLQueryItem] = [] - } - let groups = - originalQueryItems - .reduce(into: GroupedQueryItems()) { partialResult, item in - if item.name == name { - partialResult.matchingValues.append(item.value ?? "") - } else { - partialResult.otherItems.append(item) - } - } - let newItem = URLQueryItem( - name: name, - value: (groups.matchingValues + [value]).joined(separator: ",") - ) - queryItems = groups.otherItems + [newItem] - } -} - -extension Converter { - // | server | get | request path | text | string-convertible | required | getPathParameterAsText | - @available(*, deprecated) - public func getPathParameterAsText( - in pathParameters: [String: String], - name: String, - as type: T.Type - ) throws -> T { - try getRequiredRequestPath( - in: pathParameters, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | server | get | request query | text | string-convertible | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - public func getOptionalQueryItemAsText( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: T.Type - ) throws -> T? { - try getOptionalQueryItem( - in: queryParameters, - style: style, - explode: explode, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | server | get | request query | text | string-convertible | required | getRequiredQueryItemAsText | - @available(*, deprecated) - public func getRequiredQueryItemAsText( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: T.Type - ) throws -> T { - try getRequiredQueryItem( - in: queryParameters, - style: style, - explode: explode, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | server | get | request query | text | array of string-convertibles | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - public func getOptionalQueryItemAsText( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: [T].Type - ) throws -> [T]? { - try getOptionalQueryItems( - in: queryParameters, - style: style, - explode: explode, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | server | get | request query | text | array of string-convertibles | required | getRequiredQueryItemAsText | - @available(*, deprecated) - public func getRequiredQueryItemAsText( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: [T].Type - ) throws -> [T] { - try getRequiredQueryItems( - in: queryParameters, - style: style, - explode: explode, - name: name, - as: type, - convert: convertTextToStringConvertible - ) - } - - // | server | get | request query | text | date | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - public func getOptionalQueryItemAsText( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: Date.Type - ) throws -> Date? { - try getOptionalQueryItem( - in: queryParameters, - style: style, - explode: explode, - name: name, - as: type, - convert: convertTextToDate - ) - } - - // | server | get | request query | text | date | required | getRequiredQueryItemAsText | - @available(*, deprecated) - public func getRequiredQueryItemAsText( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: Date.Type - ) throws -> Date { - try getRequiredQueryItem( - in: queryParameters, - style: style, - explode: explode, - name: name, - as: type, - convert: convertTextToDate - ) - } - - // | server | get | request query | text | array of dates | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - public func getOptionalQueryItemAsText( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: [Date].Type - ) throws -> [Date]? { - try getOptionalQueryItems( - in: queryParameters, - style: style, - explode: explode, - name: name, - as: type, - convert: convertTextToDate - ) - } - - // | server | get | request query | text | array of dates | required | getRequiredQueryItemAsText | - @available(*, deprecated) - public func getRequiredQueryItemAsText( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: [Date].Type - ) throws -> [Date] { - try getRequiredQueryItems( - in: queryParameters, - style: style, - explode: explode, - name: name, - as: type, - convert: convertTextToDate - ) - } - - @available(*, deprecated) - func getOptionalQueryItem( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: T.Type, - convert: (String) throws -> T - ) throws -> T? { - // Even though the return value isn't used, the validation - // in the method is important for consistently handling - // style+explode combinations in all the helper functions. - let (_, _) = try ParameterStyle.resolvedQueryStyleAndExplode( - name: name, - style: style, - explode: explode - ) - guard - let untypedValue = - queryParameters - .first(where: { $0.name == name }) - else { - return nil - } - return try convert(untypedValue.value ?? "") - } - - @available(*, deprecated) - func getRequiredQueryItem( - in queryParameters: [URLQueryItem], - style: ParameterStyle?, - explode: Bool?, - name: String, - as type: T.Type, - convert: (String) throws -> T - ) throws -> T { - guard - let value = try getOptionalQueryItem( - in: queryParameters, - style: style, - explode: explode, - name: name, - as: type, - convert: convert - ) - else { - throw RuntimeError.missingRequiredQueryParameter(name) - } - return value - } - - // | server | get | request body | text | string-convertible | optional | getOptionalRequestBodyAsText | - @available(*, deprecated) - public func getOptionalRequestBodyAsText( - _ type: T.Type, - from data: Data?, - transforming transform: (T) -> C - ) throws -> C? { - try getOptionalRequestBody( - type, - from: data, - transforming: transform, - convert: convertTextDataToStringConvertible - ) - } - - // | server | get | request body | text | string-convertible | required | getRequiredRequestBodyAsText | - @available(*, deprecated) - public func getRequiredRequestBodyAsText( - _ type: T.Type, - from data: Data?, - transforming transform: (T) -> C - ) throws -> C { - try getRequiredRequestBody( - type, - from: data, - transforming: transform, - convert: convertTextDataToStringConvertible - ) - } - - // | server | get | request body | text | date | optional | getOptionalRequestBodyAsText | - @available(*, deprecated) - public func getOptionalRequestBodyAsText( - _ type: Date.Type, - from data: Data?, - transforming transform: (Date) -> C - ) throws -> C? { - try getOptionalRequestBody( - type, - from: data, - transforming: transform, - convert: convertTextDataToDate - ) - } - - // | server | get | request body | text | date | required | getRequiredRequestBodyAsText | - @available(*, deprecated) - public func getRequiredRequestBodyAsText( - _ type: Date.Type, - from data: Data?, - transforming transform: (Date) -> C - ) throws -> C { - try getRequiredRequestBody( - type, - from: data, - transforming: transform, - convert: convertTextDataToDate - ) - } - - // | server | set | response body | text | string-convertible | required | setResponseBodyAsText | - @available(*, deprecated) - public func setResponseBodyAsText( - _ value: T, - headerFields: inout [HeaderField], - contentType: String - ) throws -> Data { - try setResponseBody( - value, - headerFields: &headerFields, - contentType: contentType, - convert: convertStringConvertibleToTextData - ) - } - - // | server | set | response body | text | date | required | setResponseBodyAsText | - @available(*, deprecated) - public func setResponseBodyAsText( - _ value: Date, - headerFields: inout [HeaderField], - contentType: String - ) throws -> Data { - try setResponseBody( - value, - headerFields: &headerFields, - contentType: contentType, - convert: convertDateToTextData - ) - } - -} diff --git a/Sources/OpenAPIRuntime/Deprecated/Deprecated_AutoLosslessStringConvertible.swift b/Sources/OpenAPIRuntime/Deprecated/Deprecated_AutoLosslessStringConvertible.swift deleted file mode 100644 index c7588d8e..00000000 --- a/Sources/OpenAPIRuntime/Deprecated/Deprecated_AutoLosslessStringConvertible.swift +++ /dev/null @@ -1,35 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftOpenAPIGenerator open source project -// -// Copyright (c) 2023 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 -// -//===----------------------------------------------------------------------===// - -/// A protocol that provides a RawRepresentable type with a default -/// LosslessStringConvertible implementation. -/// -/// Used by generated string-based enum types. -/// -/// Cannot be marked as SPI, as it's added on public types, but should be -/// considered an internal implementation detail of the generator. -@available(*, deprecated) -public protocol _AutoLosslessStringConvertible: - RawRepresentable, LosslessStringConvertible, _StringConvertible -where RawValue == String {} - -@available(*, deprecated) -extension _AutoLosslessStringConvertible { - public init?(_ description: String) { - self.init(rawValue: description) - } - public var description: String { - rawValue - } -} diff --git a/Sources/OpenAPIRuntime/Deprecated/Deprecated_RedactedHeaderFields.swift b/Sources/OpenAPIRuntime/Deprecated/Deprecated_RedactedHeaderFields.swift deleted file mode 100644 index ccc7e115..00000000 --- a/Sources/OpenAPIRuntime/Deprecated/Deprecated_RedactedHeaderFields.swift +++ /dev/null @@ -1,93 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftOpenAPIGenerator open source project -// -// Copyright (c) 2023 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 -// -//===----------------------------------------------------------------------===// -#if canImport(Darwin) -import Foundation -#else -@preconcurrency import class Foundation.NSLock -#endif - -/// A protected-by-locks storage for ``redactedHeaderFields``. -private class RedactedHeadersStorage: @unchecked Sendable { - /// The underlying storage of ``redactedHeaderFields``, - /// protected by a lock. - private var _locked_redactedHeaderFields: Set = HeaderField.defaultRedactedHeaderFields - - /// The header fields to be redacted. - var redactedHeaderFields: Set { - get { - lock.lock() - defer { - lock.unlock() - } - return _locked_redactedHeaderFields - } - set { - lock.lock() - defer { - lock.unlock() - } - _locked_redactedHeaderFields = newValue - } - } - - /// The lock used for protecting access to `_locked_redactedHeaderFields`. - private let lock: NSLock = { - let lock = NSLock() - lock.name = "com.apple.swift-openapi-runtime.lock.redactedHeaderFields" - return lock - }() -} - -extension HeaderField { - /// Names of the header fields whose values should be redacted. - /// - /// All header field names are lowercased when added to the set. - /// - /// The values of header fields with the provided names will are replaced - /// with "" when using `HeaderField.description`. - /// - /// Use this to avoid leaking sensitive tokens into application logs. - @available(*, deprecated, message: "This feature is deprecated and will be removed in a future version.") - public static var redactedHeaderFields: Set { - set { - internalRedactedHeaderFields = newValue - } - get { - internalRedactedHeaderFields - } - } - - /// Names of the header fields whose values should be redacted. - /// - /// Should be called by code in the runtime library to avoid emitting a deprecation warning. - internal static var internalRedactedHeaderFields: Set { - set { - // Save lowercased versions of the header field names to make - // membership checking O(1). - redactedHeadersStorage.redactedHeaderFields = Set(newValue.map { $0.lowercased() }) - } - get { - return redactedHeadersStorage.redactedHeaderFields - } - } - - /// The default header field names whose values are redacted. - public static let defaultRedactedHeaderFields: Set = [ - "authorization", - "cookie", - "set-cookie", - ] - - private static let redactedHeadersStorage = RedactedHeadersStorage() -} diff --git a/Sources/OpenAPIRuntime/Deprecated/Deprecated_StringConvertible.swift b/Sources/OpenAPIRuntime/Deprecated/Deprecated_StringConvertible.swift deleted file mode 100644 index 69302270..00000000 --- a/Sources/OpenAPIRuntime/Deprecated/Deprecated_StringConvertible.swift +++ /dev/null @@ -1,43 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftOpenAPIGenerator open source project -// -// Copyright (c) 2023 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 Foundation - -/// This marker protocol represents types that are representable as a string, -/// usable in headers, path parameters, query items, and text bodies. -/// -/// Cannot be marked as SPI, as it's added on public types, but should be -/// considered an internal implementation detail of the generator. -@available(*, deprecated) -public protocol _StringConvertible: LosslessStringConvertible {} - -@available(*, deprecated) -extension String: _StringConvertible {} - -@available(*, deprecated) -extension Bool: _StringConvertible {} - -@available(*, deprecated) -extension Int: _StringConvertible {} - -@available(*, deprecated) -extension Int64: _StringConvertible {} - -@available(*, deprecated) -extension Int32: _StringConvertible {} - -@available(*, deprecated) -extension Float: _StringConvertible {} - -@available(*, deprecated) -extension Double: _StringConvertible {} diff --git a/Sources/OpenAPIRuntime/Documentation.docc/Documentation.md b/Sources/OpenAPIRuntime/Documentation.docc/Documentation.md index b0337285..10119b46 100644 --- a/Sources/OpenAPIRuntime/Documentation.docc/Documentation.md +++ b/Sources/OpenAPIRuntime/Documentation.docc/Documentation.md @@ -17,7 +17,7 @@ Add the package dependency in your `Package.swift`: ```swift .package( url: "https://github.com/apple/swift-openapi-runtime", - .upToNextMinor(from: "0.1.0") + .upToNextMinor(from: "0.2.0") ), ``` diff --git a/Tests/OpenAPIRuntimeTests/Conversion/Test_FoundationExtensions.swift b/Tests/OpenAPIRuntimeTests/Conversion/Test_FoundationExtensions.swift deleted file mode 100644 index ef4e6bba..00000000 --- a/Tests/OpenAPIRuntimeTests/Conversion/Test_FoundationExtensions.swift +++ /dev/null @@ -1,43 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftOpenAPIGenerator open source project -// -// Copyright (c) 2023 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 -@testable import OpenAPIRuntime - -final class Test_FoundationExtensions: Test_Runtime { - - @available(*, deprecated) - func testURLComponents_addStringQueryItem() throws { - var components = testComponents - components.addUnescapedStringQueryItem(name: "key", value: "value", explode: true) - XCTAssertEqualURLString(components.url, "/api?key=value") - } - - @available(*, deprecated) - func testURLComponents_addStringQueryItems() throws { - var components = testComponents - components.addUnescapedStringQueryItem(name: "key2", value: "value3", explode: true) - components.addUnescapedStringQueryItem(name: "key", value: "value1", explode: true) - components.addUnescapedStringQueryItem(name: "key", value: "value2", explode: true) - XCTAssertEqualURLString(components.url, "/api?key2=value3&key=value1&key=value2") - } - - @available(*, deprecated) - func testURLComponents_addStringQueryItems_unexploded() throws { - var components = testComponents - components.addUnescapedStringQueryItem(name: "key2", value: "value3", explode: false) - components.addUnescapedStringQueryItem(name: "key", value: "value1", explode: false) - components.addUnescapedStringQueryItem(name: "key", value: "value2", explode: false) - XCTAssertEqualURLString(components.url, "/api?key2=value3&key=value1,value2") - } -} diff --git a/Tests/OpenAPIRuntimeTests/Deprecated/Test_Deprecated.swift b/Tests/OpenAPIRuntimeTests/Deprecated/Test_Deprecated.swift index 7eff20eb..8c030ccc 100644 --- a/Tests/OpenAPIRuntimeTests/Deprecated/Test_Deprecated.swift +++ b/Tests/OpenAPIRuntimeTests/Deprecated/Test_Deprecated.swift @@ -15,981 +15,5 @@ import XCTest @_spi(Generated)@testable import OpenAPIRuntime final class Test_Deprecated: Test_Runtime { - - // | client | set | request body | text | string-convertible | optional | setOptionalRequestBodyAsText | - @available(*, deprecated) - func test_deprecated_setOptionalRequestBodyAsText_stringConvertible() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setOptionalRequestBodyAsText( - testString, - headerFields: &headerFields, - transforming: { value in - .init( - value: value, - contentType: "text/plain" - ) - } - ) - XCTAssertEqual(body, testStringData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } - - // | client | set | request body | text | string-convertible | required | setRequiredRequestBodyAsText | - @available(*, deprecated) - func test_deprecated_setRequiredRequestBodyAsText_stringConvertible() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setRequiredRequestBodyAsText( - testString, - headerFields: &headerFields, - transforming: { value in - .init( - value: value, - contentType: "text/plain" - ) - } - ) - XCTAssertEqual(body, testStringData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } - - // | client | set | request body | text | date | optional | setOptionalRequestBodyAsText | - @available(*, deprecated) - func test_deprecated_setOptionalRequestBodyAsText_date() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setOptionalRequestBodyAsText( - testDate, - headerFields: &headerFields, - transforming: { value in - .init( - value: value, - contentType: "text/plain" - ) - } - ) - XCTAssertEqual(body, testDateStringData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } - - // | client | set | request body | text | date | required | setRequiredRequestBodyAsText | - @available(*, deprecated) - func test_deprecated_setRequiredRequestBodyAsText_date() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setRequiredRequestBodyAsText( - testDate, - headerFields: &headerFields, - transforming: { value in - .init( - value: value, - contentType: "text/plain" - ) - } - ) - XCTAssertEqual(body, testDateStringData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } - - // | client | set | request body | JSON | codable | optional | setOptionalRequestBodyAsJSON | - @available(*, deprecated) - func test_deprecated_setOptionalRequestBodyAsJSON_codable() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setOptionalRequestBodyAsJSON( - testStruct, - headerFields: &headerFields, - transforming: { value in - .init( - value: value, - contentType: "application/json" - ) - } - ) - XCTAssertEqual(body, testStructPrettyData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "application/json") - ] - ) - } - - @available(*, deprecated) - func test_deprecated_setOptionalRequestBodyAsJSON_codable_string() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setOptionalRequestBodyAsJSON( - testString, - headerFields: &headerFields, - transforming: { value in - .init( - value: value, - contentType: "application/json" - ) - } - ) - XCTAssertEqual(body, testQuotedStringData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "application/json") - ] - ) - } - - // | client | set | request body | JSON | codable | required | setRequiredRequestBodyAsJSON | - @available(*, deprecated) - func test_deprecated_setRequiredRequestBodyAsJSON_codable() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setRequiredRequestBodyAsJSON( - testStruct, - headerFields: &headerFields, - transforming: { value in - .init( - value: value, - contentType: "application/json" - ) - } - ) - XCTAssertEqual(body, testStructPrettyData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "application/json") - ] - ) - } - - // | client | set | request body | binary | data | optional | setOptionalRequestBodyAsBinary | - @available(*, deprecated) - func test_deprecated_setOptionalRequestBodyAsBinary_data() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setOptionalRequestBodyAsBinary( - testStringData, - headerFields: &headerFields, - transforming: { value in - .init( - value: value, - contentType: "application/octet-stream" - ) - } - ) - XCTAssertEqual(body, testStringData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "application/octet-stream") - ] - ) - } - - // | client | set | request body | binary | data | required | setRequiredRequestBodyAsBinary | - @available(*, deprecated) - func test_deprecated_setRequiredRequestBodyAsBinary_data() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setRequiredRequestBodyAsBinary( - testStringData, - headerFields: &headerFields, - transforming: { value in - .init( - value: value, - contentType: "application/octet-stream" - ) - } - ) - XCTAssertEqual(body, testStringData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "application/octet-stream") - ] - ) - } - - @available(*, deprecated) - func testValidateContentType_match() throws { - let headerFields: [HeaderField] = [ - .init(name: "content-type", value: "application/json") - ] - XCTAssertNoThrow( - try converter.validateContentTypeIfPresent( - in: headerFields, - substring: "application/json" - ) - ) - } - - @available(*, deprecated) - func testValidateContentType_match_substring() throws { - let headerFields: [HeaderField] = [ - .init(name: "content-type", value: "application/json; charset=utf-8") - ] - XCTAssertNoThrow( - try converter.validateContentTypeIfPresent( - in: headerFields, - substring: "application/json" - ) - ) - } - - @available(*, deprecated) - func testValidateContentType_missing() throws { - let headerFields: [HeaderField] = [] - XCTAssertNoThrow( - try converter.validateContentTypeIfPresent( - in: headerFields, - substring: "application/json" - ) - ) - } - - @available(*, deprecated) - func testValidateContentType_mismatch() throws { - let headerFields: [HeaderField] = [ - .init(name: "content-type", value: "text/plain") - ] - XCTAssertThrowsError( - try converter.validateContentTypeIfPresent( - in: headerFields, - substring: "application/json" - ), - "Was expected to throw error on mismatch", - { error in - guard - let err = error as? RuntimeError, - case .unexpectedContentTypeHeader(let contentType) = err - else { - XCTFail("Unexpected kind of error thrown") - return - } - XCTAssertEqual(contentType, "text/plain") - } - ) - } - - // | server | set | response body | text | string-convertible | required | setResponseBodyAsText | - @available(*, deprecated) - func test_deprecated_setResponseBodyAsText_stringConvertible() throws { - var headers: [HeaderField] = [] - let data = try converter.setResponseBodyAsText( - testString, - headerFields: &headers, - transforming: { - .init( - value: $0, - contentType: "text/plain" - ) - } - ) - XCTAssertEqual(data, testStringData) - XCTAssertEqual( - headers, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } - - // | server | set | response body | text | date | required | setResponseBodyAsText | - @available(*, deprecated) - func test_deprecated_setResponseBodyAsText_date() throws { - var headers: [HeaderField] = [] - let data = try converter.setResponseBodyAsText( - testDate, - headerFields: &headers, - transforming: { - .init( - value: $0, - contentType: "text/plain" - ) - } - ) - XCTAssertEqual(data, testDateStringData) - XCTAssertEqual( - headers, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } - - // | server | set | response body | JSON | codable | required | setResponseBodyAsJSON | - @available(*, deprecated) - func test_deprecated_setResponseBodyAsJSON_codable() throws { - var headers: [HeaderField] = [] - let data = try converter.setResponseBodyAsJSON( - testStruct, - headerFields: &headers, - transforming: { - .init( - value: $0, - contentType: "application/json" - ) - } - ) - XCTAssertEqual(data, testStructPrettyData) - XCTAssertEqual( - headers, - [ - .init(name: "content-type", value: "application/json") - ] - ) - } - - // | server | set | response body | binary | data | required | setResponseBodyAsBinary | - @available(*, deprecated) - func test_deprecated_setResponseBodyAsBinary_data() throws { - var headers: [HeaderField] = [] - let data = try converter.setResponseBodyAsBinary( - testStringData, - headerFields: &headers, - transforming: { - .init( - value: $0, - contentType: "application/octet-stream" - ) - } - ) - XCTAssertEqual(data, testStringData) - XCTAssertEqual( - headers, - [ - .init(name: "content-type", value: "application/octet-stream") - ] - ) - } - - // | common | set | header field | text | string-convertible | both | setHeaderFieldAsText | - @available(*, deprecated) - func test_setHeaderFieldAsText_stringConvertible() throws { - var headerFields: [HeaderField] = [] - try converter.setHeaderFieldAsText( - in: &headerFields, - name: "foo", - value: "bar" - ) - XCTAssertEqual( - headerFields, - [ - .init(name: "foo", value: "bar") - ] - ) - } - - // | common | set | header field | text | array of string-convertibles | both | setHeaderFieldAsText | - @available(*, deprecated) - func test_setHeaderFieldAsText_arrayOfStringConvertible() throws { - var headerFields: [HeaderField] = [] - try converter.setHeaderFieldAsText( - in: &headerFields, - name: "foo", - value: ["bar", "baz"] as [String] - ) - XCTAssertEqual( - headerFields, - [ - .init(name: "foo", value: "bar"), - .init(name: "foo", value: "baz"), - ] - ) - } - - // | common | set | header field | text | date | both | setHeaderFieldAsText | - @available(*, deprecated) - func test_setHeaderFieldAsText_date() throws { - var headerFields: [HeaderField] = [] - try converter.setHeaderFieldAsText( - in: &headerFields, - name: "foo", - value: testDate - ) - XCTAssertEqual( - headerFields, - [ - .init(name: "foo", value: testDateString) - ] - ) - } - - // | common | set | header field | text | array of dates | both | setHeaderFieldAsText | - @available(*, deprecated) - func test_setHeaderFieldAsText_arrayOfDates() throws { - var headerFields: [HeaderField] = [] - try converter.setHeaderFieldAsText( - in: &headerFields, - name: "foo", - value: [testDate, testDate] - ) - XCTAssertEqual( - headerFields, - [ - .init(name: "foo", value: testDateString), - .init(name: "foo", value: testDateString), - ] - ) - } - - // | common | get | header field | text | string-convertible | optional | getOptionalHeaderFieldAsText | - @available(*, deprecated) - func test_getOptionalHeaderFieldAsText_stringConvertible() throws { - let headerFields: [HeaderField] = [ - .init(name: "foo", value: "bar") - ] - let value: String? = try converter.getOptionalHeaderFieldAsText( - in: headerFields, - name: "foo", - as: String.self - ) - XCTAssertEqual(value, "bar") - } - - // | common | get | header field | text | string-convertible | required | getRequiredHeaderFieldAsText | - @available(*, deprecated) - func test_getRequiredHeaderFieldAsText_stringConvertible() throws { - let headerFields: [HeaderField] = [ - .init(name: "foo", value: "bar") - ] - let value: String = try converter.getRequiredHeaderFieldAsText( - in: headerFields, - name: "foo", - as: String.self - ) - XCTAssertEqual(value, "bar") - } - - // | common | get | header field | text | array of string-convertibles | optional | getOptionalHeaderFieldAsText | - @available(*, deprecated) - func test_getOptionalHeaderFieldAsText_arrayOfStringConvertibles() throws { - let headerFields: [HeaderField] = [ - .init(name: "foo", value: "bar"), - .init(name: "foo", value: "baz"), - ] - let value: [String]? = try converter.getOptionalHeaderFieldAsText( - in: headerFields, - name: "foo", - as: [String].self - ) - XCTAssertEqual(value, ["bar", "baz"]) - } - - // | common | get | header field | text | array of string-convertibles | required | getRequiredHeaderFieldAsText | - @available(*, deprecated) - func test_getRequiredHeaderFieldAsText_arrayOfStringConvertibles() throws { - let headerFields: [HeaderField] = [ - .init(name: "foo", value: "bar"), - .init(name: "foo", value: "baz"), - ] - let value: [String] = try converter.getRequiredHeaderFieldAsText( - in: headerFields, - name: "foo", - as: [String].self - ) - XCTAssertEqual(value, ["bar", "baz"]) - } - - // | common | get | header field | text | date | optional | getOptionalHeaderFieldAsText | - @available(*, deprecated) - func test_getOptionalHeaderFieldAsText_date() throws { - let headerFields: [HeaderField] = [ - .init(name: "foo", value: testDateString) - ] - let value: Date? = try converter.getOptionalHeaderFieldAsText( - in: headerFields, - name: "foo", - as: Date.self - ) - XCTAssertEqual(value, testDate) - } - - // | common | get | header field | text | date | required | getRequiredHeaderFieldAsText | - @available(*, deprecated) - func test_getRequiredHeaderFieldAsText_date() throws { - let headerFields: [HeaderField] = [ - .init(name: "foo", value: testDateString) - ] - let value: Date = try converter.getRequiredHeaderFieldAsText( - in: headerFields, - name: "foo", - as: Date.self - ) - XCTAssertEqual(value, testDate) - } - - // | common | get | header field | text | array of dates | optional | getOptionalHeaderFieldAsText | - @available(*, deprecated) - func test_getOptionalHeaderFieldAsText_arrayOfDates() throws { - let headerFields: [HeaderField] = [ - .init(name: "foo", value: testDateString), - .init(name: "foo", value: testDateString), - ] - let value: [Date]? = try converter.getOptionalHeaderFieldAsText( - in: headerFields, - name: "foo", - as: [Date].self - ) - XCTAssertEqual(value, [testDate, testDate]) - } - - // | common | get | header field | text | array of dates | required | getRequiredHeaderFieldAsText | - @available(*, deprecated) - func test_getRequiredHeaderFieldAsText_arrayOfDates() throws { - let headerFields: [HeaderField] = [ - .init(name: "foo", value: testDateString), - .init(name: "foo", value: testDateString), - ] - let value: [Date] = try converter.getRequiredHeaderFieldAsText( - in: headerFields, - name: "foo", - as: [Date].self - ) - XCTAssertEqual(value, [testDate, testDate]) - } - - // | client | set | request path | text | string-convertible | required | renderedRequestPath | - @available(*, deprecated) - func test_renderedRequestPath_stringConvertible() throws { - let renderedPath = try converter.renderedRequestPath( - template: "/items/{}/detail/{}", - parameters: [ - 1 as Int, - "foo" as String, - ] - ) - XCTAssertEqual(renderedPath, "/items/1/detail/foo") - } - - // | client | set | request query | text | string-convertible | both | setQueryItemAsText | - @available(*, deprecated) - func test_setQueryItemAsText_stringConvertible() throws { - var request = testRequest - try converter.setQueryItemAsText( - in: &request, - style: nil, - explode: nil, - name: "search", - value: "foo" - ) - XCTAssertEqual(request.query, "search=foo") - } - - @available(*, deprecated) - func test_setQueryItemAsText_stringConvertible_needsEncoding() throws { - var request = testRequest - try converter.setQueryItemAsText( - in: &request, - style: nil, - explode: nil, - name: "search", - value: "h%llo" - ) - XCTAssertEqual(request.query, "search=h%25llo") - } - - // | client | set | request query | text | array of string-convertibles | both | setQueryItemAsText | - @available(*, deprecated) - func test_setQueryItemAsText_arrayOfStringConvertibles() throws { - var request = testRequest - try converter.setQueryItemAsText( - in: &request, - style: nil, - explode: nil, - name: "search", - value: ["foo", "bar"] - ) - XCTAssertEqual(request.query, "search=foo&search=bar") - } - - // | client | set | request query | text | array of string-convertibles | both | setQueryItemAsText | - @available(*, deprecated) - func test_setQueryItemAsText_arrayOfStringConvertibles_unexploded() throws { - var request = testRequest - try converter.setQueryItemAsText( - in: &request, - style: nil, - explode: false, - name: "search", - value: ["foo", "bar"] - ) - XCTAssertEqual(request.query, "search=foo,bar") - } - - // | client | set | request query | text | date | both | setQueryItemAsText | - @available(*, deprecated) - func test_setQueryItemAsText_date() throws { - var request = testRequest - try converter.setQueryItemAsText( - in: &request, - style: nil, - explode: nil, - name: "search", - value: testDate - ) - XCTAssertEqual(request.query, "search=2023-01-18T10:04:11Z") - } - - // | client | set | request query | text | array of dates | both | setQueryItemAsText | - @available(*, deprecated) - func test_setQueryItemAsText_arrayOfDates() throws { - var request = testRequest - try converter.setQueryItemAsText( - in: &request, - style: nil, - explode: nil, - name: "search", - value: [testDate, testDate] - ) - XCTAssertEqual(request.query, "search=2023-01-18T10:04:11Z&search=2023-01-18T10:04:11Z") - } - - // | client | set | request body | text | string-convertible | optional | setOptionalRequestBodyAsText | - @available(*, deprecated) - func test_setOptionalRequestBodyAsText_stringConvertible() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setOptionalRequestBodyAsText( - testString, - headerFields: &headerFields, - contentType: "text/plain" - ) - XCTAssertEqual(body, testStringData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } - - // | client | set | request body | text | string-convertible | required | setRequiredRequestBodyAsText | - @available(*, deprecated) - func test_setRequiredRequestBodyAsText_stringConvertible() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setRequiredRequestBodyAsText( - testString, - headerFields: &headerFields, - contentType: "text/plain" - ) - XCTAssertEqual(body, testStringData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } - - // | client | set | request body | text | date | optional | setOptionalRequestBodyAsText | - @available(*, deprecated) - func test_setOptionalRequestBodyAsText_date() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setOptionalRequestBodyAsText( - testDate, - headerFields: &headerFields, - contentType: "text/plain" - ) - XCTAssertEqual(body, testDateStringData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } - - // | client | set | request body | text | date | required | setRequiredRequestBodyAsText | - @available(*, deprecated) - func test_setRequiredRequestBodyAsText_date() throws { - var headerFields: [HeaderField] = [] - let body = try converter.setRequiredRequestBodyAsText( - testDate, - headerFields: &headerFields, - contentType: "text/plain" - ) - XCTAssertEqual(body, testDateStringData) - XCTAssertEqual( - headerFields, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } - - // | client | get | response body | text | string-convertible | required | getResponseBodyAsText | - @available(*, deprecated) - func test_getResponseBodyAsText_stringConvertible() throws { - let value: String = try converter.getResponseBodyAsText( - String.self, - from: testStringData, - transforming: { $0 } - ) - XCTAssertEqual(value, testString) - } - - // | client | get | response body | text | date | required | getResponseBodyAsText | - @available(*, deprecated) - func test_getResponseBodyAsText_date() throws { - let value: Date = try converter.getResponseBodyAsText( - Date.self, - from: testDateStringData, - transforming: { $0 } - ) - XCTAssertEqual(value, testDate) - } - - // | server | get | request path | text | string-convertible | required | getPathParameterAsText | - @available(*, deprecated) - func test_getPathParameterAsText_stringConvertible() throws { - let path: [String: String] = [ - "foo": "bar" - ] - let value: String = try converter.getPathParameterAsText( - in: path, - name: "foo", - as: String.self - ) - XCTAssertEqual(value, "bar") - } - - // | server | get | request query | text | string-convertible | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - func test_getOptionalQueryItemAsText_stringConvertible() throws { - let query: [URLQueryItem] = [ - .init(name: "search", value: "foo") - ] - let value: String? = try converter.getOptionalQueryItemAsText( - in: query, - style: nil, - explode: nil, - name: "search", - as: String.self - ) - XCTAssertEqual(value, "foo") - } - - // | server | get | request query | text | string-convertible | required | getRequiredQueryItemAsText | - @available(*, deprecated) - func test_getRequiredQueryItemAsText_stringConvertible() throws { - let query: [URLQueryItem] = [ - .init(name: "search", value: "foo") - ] - let value: String = try converter.getRequiredQueryItemAsText( - in: query, - style: nil, - explode: nil, - name: "search", - as: String.self - ) - XCTAssertEqual(value, "foo") - } - - // | server | get | request query | text | array of string-convertibles | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - func test_getOptionalQueryItemAsText_arrayOfStringConvertibles() throws { - let query: [URLQueryItem] = [ - .init(name: "search", value: "foo"), - .init(name: "search", value: "bar"), - ] - let value: [String]? = try converter.getOptionalQueryItemAsText( - in: query, - style: nil, - explode: nil, - name: "search", - as: [String].self - ) - XCTAssertEqual(value, ["foo", "bar"]) - } - - // | server | get | request query | text | array of string-convertibles | required | getRequiredQueryItemAsText | - @available(*, deprecated) - func test_getRequiredQueryItemAsText_arrayOfStringConvertibles() throws { - let query: [URLQueryItem] = [ - .init(name: "search", value: "foo"), - .init(name: "search", value: "bar"), - ] - let value: [String] = try converter.getRequiredQueryItemAsText( - in: query, - style: nil, - explode: nil, - name: "search", - as: [String].self - ) - XCTAssertEqual(value, ["foo", "bar"]) - } - - // | server | get | request query | text | array of string-convertibles | required | getRequiredQueryItemAsText | - @available(*, deprecated) - func test_getRequiredQueryItemAsText_arrayOfStringConvertibles_unexploded() throws { - let query: [URLQueryItem] = [ - .init(name: "search", value: "foo,bar") - ] - let value: [String] = try converter.getRequiredQueryItemAsText( - in: query, - style: nil, - explode: false, - name: "search", - as: [String].self - ) - XCTAssertEqual(value, ["foo", "bar"]) - } - - // | server | get | request query | text | date | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - func test_getOptionalQueryItemAsText_date() throws { - let query: [URLQueryItem] = [ - .init(name: "search", value: testDateString) - ] - let value: Date? = try converter.getOptionalQueryItemAsText( - in: query, - style: nil, - explode: nil, - name: "search", - as: Date.self - ) - XCTAssertEqual(value, testDate) - } - - // | server | get | request query | text | date | required | getRequiredQueryItemAsText | - @available(*, deprecated) - func test_getRequiredQueryItemAsText_date() throws { - let query: [URLQueryItem] = [ - .init(name: "search", value: testDateString) - ] - let value: Date = try converter.getRequiredQueryItemAsText( - in: query, - style: nil, - explode: nil, - name: "search", - as: Date.self - ) - XCTAssertEqual(value, testDate) - } - - // | server | get | request query | text | array of dates | optional | getOptionalQueryItemAsText | - @available(*, deprecated) - func test_getOptionalQueryItemAsText_arrayOfDates() throws { - let query: [URLQueryItem] = [ - .init(name: "search", value: testDateString), - .init(name: "search", value: testDateString), - ] - let value: [Date]? = try converter.getOptionalQueryItemAsText( - in: query, - style: nil, - explode: nil, - name: "search", - as: [Date].self - ) - XCTAssertEqual(value, [testDate, testDate]) - } - - // | server | get | request query | text | array of dates | required | getRequiredQueryItemAsText | - @available(*, deprecated) - func test_getRequiredQueryItemAsText_arrayOfDates() throws { - let query: [URLQueryItem] = [ - .init(name: "search", value: testDateString), - .init(name: "search", value: testDateString), - ] - let value: [Date] = try converter.getRequiredQueryItemAsText( - in: query, - style: nil, - explode: nil, - name: "search", - as: [Date].self - ) - XCTAssertEqual(value, [testDate, testDate]) - } - - // | server | get | request body | text | string-convertible | optional | getOptionalRequestBodyAsText | - @available(*, deprecated) - func test_getOptionalRequestBodyAsText_stringConvertible() throws { - let body: String? = try converter.getOptionalRequestBodyAsText( - String.self, - from: testStringData, - transforming: { $0 } - ) - XCTAssertEqual(body, testString) - } - - // | server | get | request body | text | string-convertible | required | getRequiredRequestBodyAsText | - @available(*, deprecated) - func test_getRequiredRequestBodyAsText_stringConvertible() throws { - let body: String = try converter.getRequiredRequestBodyAsText( - String.self, - from: testStringData, - transforming: { $0 } - ) - XCTAssertEqual(body, testString) - } - - // | server | get | request body | text | date | optional | getOptionalRequestBodyAsText | - @available(*, deprecated) - func test_getOptionalRequestBodyAsText_date() throws { - let body: Date? = try converter.getOptionalRequestBodyAsText( - Date.self, - from: testDateStringData, - transforming: { $0 } - ) - XCTAssertEqual(body, testDate) - } - - // | server | get | request body | text | date | required | getRequiredRequestBodyAsText | - @available(*, deprecated) - func test_getRequiredRequestBodyAsText_date() throws { - let body: Date = try converter.getRequiredRequestBodyAsText( - Date.self, - from: testDateStringData, - transforming: { $0 } - ) - XCTAssertEqual(body, testDate) - } - - // | server | set | response body | text | string-convertible | required | setResponseBodyAsText | - @available(*, deprecated) - func test_setResponseBodyAsText_stringConvertible() throws { - var headers: [HeaderField] = [] - let data = try converter.setResponseBodyAsText( - testString, - headerFields: &headers, - contentType: "text/plain" - ) - XCTAssertEqual(data, testStringData) - XCTAssertEqual( - headers, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } - - // | server | set | response body | text | date | required | setResponseBodyAsText | - @available(*, deprecated) - func test_setResponseBodyAsText_date() throws { - var headers: [HeaderField] = [] - let data = try converter.setResponseBodyAsText( - testDate, - headerFields: &headers, - contentType: "text/plain" - ) - XCTAssertEqual(data, testDateStringData) - XCTAssertEqual( - headers, - [ - .init(name: "content-type", value: "text/plain") - ] - ) - } + // Tests for deprecated code goes here. } diff --git a/Tests/OpenAPIRuntimeTests/Deprecated/Test_Deprecated_RedactedHeaderFields.swift b/Tests/OpenAPIRuntimeTests/Deprecated/Test_Deprecated_RedactedHeaderFields.swift deleted file mode 100644 index 1f7bd70a..00000000 --- a/Tests/OpenAPIRuntimeTests/Deprecated/Test_Deprecated_RedactedHeaderFields.swift +++ /dev/null @@ -1,86 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftOpenAPIGenerator open source project -// -// Copyright (c) 2023 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 -@_spi(Generated)@testable import OpenAPIRuntime - -final class Test_Deprecated_RedactedHeaderFields: Test_Runtime { - - @available(*, deprecated) - func _resetRedactedHeaderFields() { - HeaderField.redactedHeaderFields = HeaderField.defaultRedactedHeaderFields - } - - func _test(unredactedNames: Set, redactedNames: Set) { - for name in unredactedNames.sorted() { - XCTAssertEqual( - HeaderField(name: name, value: "cleartext").description, - "\(name): cleartext" - ) - } - for name in redactedNames.sorted() { - XCTAssertEqual( - HeaderField(name: name, value: "cleartext").description, - "\(name): " - ) - } - } - - @available(*, deprecated) - override func tearDown() async throws { - _resetRedactedHeaderFields() - try await super.tearDown() - } - - @available(*, deprecated) - func testDefaultRedactedHeaderFields() { - XCTAssertEqual( - HeaderField.redactedHeaderFields, - ["authorization", "cookie", "set-cookie"] - ) - _test( - unredactedNames: ["x-secret", "x-not-secret"], - redactedNames: ["authorization", "cookie", "set-cookie"] - ) - } - - @available(*, deprecated) - func testCustomizeExtraRedactedHeaderField() { - XCTAssertEqual( - HeaderField.redactedHeaderFields, - ["authorization", "cookie", "set-cookie"] - ) - _test( - unredactedNames: ["x-secret", "x-not-secret"], - redactedNames: ["authorization", "cookie", "set-cookie"] - ) - HeaderField.redactedHeaderFields.insert("x-secret") - XCTAssertEqual( - HeaderField.redactedHeaderFields, - ["authorization", "cookie", "set-cookie", "x-secret"] - ) - _test( - unredactedNames: ["x-not-secret"], - redactedNames: ["authorization", "cookie", "set-cookie", "x-secret"] - ) - HeaderField.redactedHeaderFields.remove("authorization") - XCTAssertEqual( - HeaderField.redactedHeaderFields, - ["cookie", "set-cookie", "x-secret"] - ) - _test( - unredactedNames: ["x-not-secret", "authorization"], - redactedNames: ["cookie", "set-cookie", "x-secret"] - ) - } -} diff --git a/Tests/OpenAPIRuntimeTests/Deprecated/Test_Deprecated_StringConvertible.swift b/Tests/OpenAPIRuntimeTests/Deprecated/Test_Deprecated_StringConvertible.swift deleted file mode 100644 index 5e30740c..00000000 --- a/Tests/OpenAPIRuntimeTests/Deprecated/Test_Deprecated_StringConvertible.swift +++ /dev/null @@ -1,34 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftOpenAPIGenerator open source project -// -// Copyright (c) 2023 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 -@_spi(Generated)@testable import OpenAPIRuntime - -final class Test_Deprecated_StringConvertible: XCTestCase { - - @available(*, deprecated) - func testConformances() throws { - let values: [(any _StringConvertible, String)] = [ - ("hello" as String, "hello"), - (1 as Int, "1"), - (1 as Int32, "1"), - (1 as Int64, "1"), - (0.5 as Float, "0.5"), - (0.5 as Double, "0.5"), - (true as Bool, "true"), - ] - for (value, stringified) in values { - XCTAssertEqual(value.description, stringified) - } - } -}