mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-08 21:06:11 +00:00
Swift4: Add additional initializer for initializing model object with properties. (#6642)
* Add addiitional files from upstream * Remove mis-added files * Add additional swift4 initializer for initializing model object with properties. This change fixes this issue: https://github.com/swagger-api/swagger-codegen/issues/6641 It adds an additional initializer which allows model objects to be initialized using the properties. For exxample, if we had this model: "ErrorInfo": { "type": "object", "properties": { "code": { "type": "integer", "format": "int32" }, "message": { "type": "string" }, "details": { "type": "array", "items": { "type": "string" } } }, "description": "Example Error object" }, This we generate an initializer for this model object like this: public init(code: Int?, message: String?, details: [String]?) { self.code = code self.message = message self.details = details } * Add hasVars checks around initializers and re-run all scripts to re-generate
This commit is contained in:
@@ -142,6 +142,56 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
nil
|
||||
)
|
||||
})
|
||||
case is URL.Type:
|
||||
validatedRequest.responseData(completionHandler: { (dataResponse) in
|
||||
cleanupRequest()
|
||||
|
||||
do {
|
||||
|
||||
guard !dataResponse.result.isFailure else {
|
||||
throw DownloadException.responseFailed
|
||||
}
|
||||
|
||||
guard let data = dataResponse.data else {
|
||||
throw DownloadException.responseDataMissing
|
||||
}
|
||||
|
||||
guard let request = request.request else {
|
||||
throw DownloadException.requestMissing
|
||||
}
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let urlRequest = try request.asURLRequest()
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: dataResponse.response?.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
let filePath = documentsDirectory.appendingPathComponent(requestPath)
|
||||
let directoryPath = filePath.deletingLastPathComponent().path
|
||||
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(
|
||||
Response(
|
||||
response: dataResponse.response!,
|
||||
body: (filePath as! T)
|
||||
),
|
||||
nil
|
||||
)
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(nil, ErrorResponse.Error(400, dataResponse.data, requestParserError))
|
||||
} catch let error {
|
||||
completion(nil, ErrorResponse.Error(400, dataResponse.data, error))
|
||||
}
|
||||
return
|
||||
})
|
||||
case is Void.Type:
|
||||
validatedRequest.responseData(completionHandler: { (voidResponse) in
|
||||
cleanupRequest()
|
||||
@@ -191,6 +241,66 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
}
|
||||
return httpHeaders
|
||||
}
|
||||
|
||||
fileprivate func getFileName(fromContentDisposition contentDisposition : String?) -> String? {
|
||||
|
||||
guard let contentDisposition = contentDisposition else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let items = contentDisposition.components(separatedBy: ";")
|
||||
|
||||
var filename : String? = nil
|
||||
|
||||
for contentItem in items {
|
||||
|
||||
let filenameKey = "filename="
|
||||
guard let range = contentItem.range(of: filenameKey) else {
|
||||
break
|
||||
}
|
||||
|
||||
filename = contentItem
|
||||
return filename?
|
||||
.replacingCharacters(in: range, with:"")
|
||||
.replacingOccurrences(of: "\"", with: "")
|
||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
|
||||
return filename
|
||||
|
||||
}
|
||||
|
||||
fileprivate func getPath(from url : URL) throws -> String {
|
||||
|
||||
guard var path = NSURLComponents(url: url, resolvingAgainstBaseURL: true)?.path else {
|
||||
throw DownloadException.requestMissingPath
|
||||
}
|
||||
|
||||
if path.hasPrefix("/") {
|
||||
path.remove(at: path.startIndex)
|
||||
}
|
||||
|
||||
return path
|
||||
|
||||
}
|
||||
|
||||
fileprivate func getURL(from urlRequest : URLRequest) throws -> URL {
|
||||
|
||||
guard let url = urlRequest.url else {
|
||||
throw DownloadException.requestMissingURL
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fileprivate enum DownloadException : Error {
|
||||
case responseDataMissing
|
||||
case responseFailed
|
||||
case requestMissing
|
||||
case requestMissingPath
|
||||
case requestMissingURL
|
||||
}
|
||||
|
||||
public enum AlamofireDecodableRequestBuilderError: Error {
|
||||
|
||||
@@ -44,6 +44,33 @@ open class AllPrimitives: Codable {
|
||||
public var myInlineStringEnum: MyInlineStringEnum?
|
||||
|
||||
|
||||
public init(myInteger: Int?, myIntegerArray: [Int]?, myLong: Int64?, myLongArray: [Int64]?, myFloat: Float?, myFloatArray: [Float]?, myDouble: Double?, myDoubleArray: [Double]?, myString: String?, myStringArray: [String]?, myBytes: Data?, myBytesArray: [Data]?, myBoolean: Bool?, myBooleanArray: [Bool]?, myDate: Date?, myDateArray: [Date]?, myDateTime: Date?, myDateTimeArray: [Date]?, myFile: URL?, myFileArray: [URL]?, myUUID: UUID?, myUUIDArray: [UUID]?, myStringEnum: StringEnum?, myStringEnumArray: [StringEnum]?) {
|
||||
self.myInteger = myInteger
|
||||
self.myIntegerArray = myIntegerArray
|
||||
self.myLong = myLong
|
||||
self.myLongArray = myLongArray
|
||||
self.myFloat = myFloat
|
||||
self.myFloatArray = myFloatArray
|
||||
self.myDouble = myDouble
|
||||
self.myDoubleArray = myDoubleArray
|
||||
self.myString = myString
|
||||
self.myStringArray = myStringArray
|
||||
self.myBytes = myBytes
|
||||
self.myBytesArray = myBytesArray
|
||||
self.myBoolean = myBoolean
|
||||
self.myBooleanArray = myBooleanArray
|
||||
self.myDate = myDate
|
||||
self.myDateArray = myDateArray
|
||||
self.myDateTime = myDateTime
|
||||
self.myDateTimeArray = myDateTimeArray
|
||||
self.myFile = myFile
|
||||
self.myFileArray = myFileArray
|
||||
self.myUUID = myUUID
|
||||
self.myUUIDArray = myUUIDArray
|
||||
self.myStringEnum = myStringEnum
|
||||
self.myStringEnumArray = myStringEnumArray
|
||||
}
|
||||
|
||||
// Encodable protocol methods
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
|
||||
@@ -17,6 +17,12 @@ open class ErrorInfo: Codable {
|
||||
public var details: [String]?
|
||||
|
||||
|
||||
public init(code: Int?, message: String?, details: [String]?) {
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.details = details
|
||||
}
|
||||
|
||||
// Encodable protocol methods
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
|
||||
@@ -17,6 +17,12 @@ open class GetAllModelsResult: Codable {
|
||||
public var myVariableNameTest: VariableNameTest?
|
||||
|
||||
|
||||
public init(myPrimitiveArray: [AllPrimitives]?, myPrimitive: AllPrimitives?, myVariableNameTest: VariableNameTest?) {
|
||||
self.myPrimitiveArray = myPrimitiveArray
|
||||
self.myPrimitive = myPrimitive
|
||||
self.myVariableNameTest = myVariableNameTest
|
||||
}
|
||||
|
||||
// Encodable protocol methods
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
|
||||
@@ -28,6 +28,7 @@ open class ModelWithIntAdditionalPropertiesOnly: Codable {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Encodable protocol methods
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
|
||||
@@ -36,6 +36,17 @@ open class ModelWithPropertiesAndAdditionalProperties: Codable {
|
||||
}
|
||||
}
|
||||
|
||||
public init(myIntegerReq: Int, myIntegerOpt: Int?, myPrimitiveReq: AllPrimitives, myPrimitiveOpt: AllPrimitives?, myStringArrayReq: [String], myStringArrayOpt: [String]?, myPrimitiveArrayReq: [AllPrimitives], myPrimitiveArrayOpt: [AllPrimitives]?) {
|
||||
self.myIntegerReq = myIntegerReq
|
||||
self.myIntegerOpt = myIntegerOpt
|
||||
self.myPrimitiveReq = myPrimitiveReq
|
||||
self.myPrimitiveOpt = myPrimitiveOpt
|
||||
self.myStringArrayReq = myStringArrayReq
|
||||
self.myStringArrayOpt = myStringArrayOpt
|
||||
self.myPrimitiveArrayReq = myPrimitiveArrayReq
|
||||
self.myPrimitiveArrayOpt = myPrimitiveArrayOpt
|
||||
}
|
||||
|
||||
// Encodable protocol methods
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
|
||||
@@ -28,6 +28,7 @@ open class ModelWithStringAdditionalPropertiesOnly: Codable {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Encodable protocol methods
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
|
||||
@@ -18,6 +18,11 @@ open class VariableNameTest: Codable {
|
||||
public var _for: String?
|
||||
|
||||
|
||||
public init(exampleName: String?, _for: String?) {
|
||||
self.exampleName = exampleName
|
||||
self._for = _for
|
||||
}
|
||||
|
||||
// Encodable protocol methods
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
|
||||
Reference in New Issue
Block a user