Swagger-codegen: Problem subclassing RequestBuilderFactory and AlamofireRequestBuilder

Created on 15 Sep 2018  路  2Comments  路  Source: swagger-api/swagger-codegen

Tried with latest HEAD today:

import Foundation
import Alamofire
import BCustomSearch

class BCustomAlamofireRequestBuilderFactory: RequestBuilderFactory {
    func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
        return BCustomAlamofireRequestBuilder<T>.self
    }

    func getBuilder<T:Decodable>() -> RequestBuilder<T>.Type {
        return BCustomAlamofireDecodableRequestBuilder<T>.self
    }
}

class BCustomAlamofireRequestBuilder<T>: AlamofireRequestBuilder<T> {
    override func createSessionManager() -> Alamofire.SessionManager {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = buildHeaders()
        return Alamofire.SessionManager(configuration: configuration)
    }

}

class BCustomAlamofireDecodableRequestBuilder<T:Decodable>: BCustomAlamofireRequestBuilder<T> {
}

in App Delegate:

        BCustomSearchAPI.requestBuilderFactory = BCustomAlamofireRequestBuilderFactory()

I don't understand what's going on because my subclass is not doing anything. I removed custom session manager code and it chokes:

Could not cast value of type 'Foundation.Data' (0x103a5d148) to 'BSearch.SearchProfilesResponse' (0x1018e7270).
2018-09-15 11:45:23.288262+0100 B[22250:672532] Could not cast value of type 'Foundation.Data' (0x103a5d148) to 'BSearch.SearchProfilesResponse' (0x1018e7270).

Most helpful comment

Finally wrapped my head around it!

BCustomAlamofireDecodableRequestBuilder must be a subclass of AlamofireRequestBuilder and I have to override createSessionManager again.

import Foundation
import Alamofire
import BCustomSearch

class BCustomAlamofireRequestBuilderFactory: RequestBuilderFactory {
    func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
        return BCustomAlamofireRequestBuilder<T>.self
    }

    func getBuilder<T:Decodable>() -> RequestBuilder<T>.Type {
        return BCustomAlamofireDecodableRequestBuilder<T>.self
    }
}

class BCustomAlamofireRequestBuilder<T>: AlamofireRequestBuilder<T> {
    override func createSessionManager() -> Alamofire.SessionManager {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = buildHeaders()
        return Alamofire.SessionManager(configuration: configuration)
    }

}

class BCustomAlamofireDecodableRequestBuilder<T:Decodable>: AlamofireRequestBuilder<T> {
    override func createSessionManager() -> Alamofire.SessionManager {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = buildHeaders()
        return Alamofire.SessionManager(configuration: configuration)
    }
}

All 2 comments

Finally wrapped my head around it!

BCustomAlamofireDecodableRequestBuilder must be a subclass of AlamofireRequestBuilder and I have to override createSessionManager again.

import Foundation
import Alamofire
import BCustomSearch

class BCustomAlamofireRequestBuilderFactory: RequestBuilderFactory {
    func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
        return BCustomAlamofireRequestBuilder<T>.self
    }

    func getBuilder<T:Decodable>() -> RequestBuilder<T>.Type {
        return BCustomAlamofireDecodableRequestBuilder<T>.self
    }
}

class BCustomAlamofireRequestBuilder<T>: AlamofireRequestBuilder<T> {
    override func createSessionManager() -> Alamofire.SessionManager {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = buildHeaders()
        return Alamofire.SessionManager(configuration: configuration)
    }

}

class BCustomAlamofireDecodableRequestBuilder<T:Decodable>: AlamofireRequestBuilder<T> {
    override func createSessionManager() -> Alamofire.SessionManager {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = buildHeaders()
        return Alamofire.SessionManager(configuration: configuration)
    }
}

@zboralski There is one typo though.

'BCustomAlamofireDecodableRequestBuilder' must be inherited from 'AlamofireDecodableRequestBuilder'.

I spent several many minutes to figure this out. Hope it saves others!

Was this page helpful?
0 / 5 - 0 ratings