From 12fd115af3d72ff5cef59dacc6fab3f9d4c47b7e Mon Sep 17 00:00:00 2001 From: Ladd Van Tol Date: Wed, 30 Nov 2022 01:35:07 -0800 Subject: [PATCH] Support RawRepresentable enums (#14144) * Support raw representable enums * Update samples --- .../main/resources/swift5/APIHelper.mustache | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- .../Sources/PetstoreClient/APIHelper.swift | 35 +++++++++---------- .../Classes/OpenAPIs/APIHelper.swift | 35 +++++++++---------- 16 files changed, 256 insertions(+), 304 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/swift5/APIHelper.mustache b/modules/openapi-generator/src/main/resources/swift5/APIHelper.mustache index 95cf790b280..f7c727964c9 100644 --- a/modules/openapi-generator/src/main/resources/swift5/APIHelper.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/APIHelper.mustache @@ -25,13 +25,10 @@ import Vapor{{/useVapor}} return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -51,13 +48,19 @@ import Vapor{{/useVapor}} } } + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -70,10 +73,7 @@ import Vapor{{/useVapor}} let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -85,7 +85,7 @@ import Vapor{{/useVapor}} } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -102,16 +102,13 @@ import Vapor{{/useVapor}} let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index b265c06f0f9..84d22f4133a 100644 --- a/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ internal struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ internal struct APIHelper { } } + internal static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + internal static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ internal struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ internal struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ internal struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/APIHelper.swift b/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/APIHelper.swift +++ b/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } diff --git a/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/APIHelper.swift b/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/APIHelper.swift index 03f3b1259b0..93c5c762602 100644 --- a/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/APIHelper.swift +++ b/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/APIHelper.swift @@ -24,13 +24,10 @@ public struct APIHelper { return source.reduce(into: [String: String]()) { result, item in if let collection = item.value as? [Any?] { result[item.key] = collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } else if let value: Any = item.value { - result[item.key] = "\(value)" + result[item.key] = convertAnyToString(value) } } } @@ -50,13 +47,19 @@ public struct APIHelper { } } + public static func convertAnyToString(_ value: Any?) -> String? { + guard let value = value else { return nil } + if let value = value as? any RawRepresentable { + return "\(value.rawValue)" + } else { + return "\(value)" + } + } + public static func mapValueToPathItem(_ source: Any) -> Any { if let collection = source as? [Any?] { return collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .joined(separator: ",") } return source @@ -69,10 +72,7 @@ public struct APIHelper { let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value.wrappedValue as? [Any?] { - let collectionValues: [String] = collection.compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } if !item.value.isExplode { result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) @@ -84,7 +84,7 @@ public struct APIHelper { } } else if let value = item.value.wrappedValue { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } } @@ -101,16 +101,13 @@ public struct APIHelper { let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in if let collection = item.value as? [Any?] { collection - .compactMap { value in - guard let value = value else { return nil } - return "\(value)" - } + .compactMap { value in convertAnyToString(value) } .forEach { value in result.append(URLQueryItem(name: item.key, value: value)) } } else if let value = item.value { - result.append(URLQueryItem(name: item.key, value: "\(value)")) + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) } }