title: Alamofire 5 - DataRequest with SwiftyJSON as response
body: <p>Before you tell me to use <code>Decodable</code> for all the models, and you would be right, I am trying to fix a project in the time the client has allocated. Changing all models to <code>Decodable</code>, and all the places where the models get used, would take a number of days.</p>
  <p>The project intercepts calls, turns them to <code>SwiftyJSON</code> and returns custom error messages from the API.</p>
  <p>The issue is that <strong>Alamofire 5</strong> doesn't accept a closure as a <code>DataRequest</code> anymore. So I need another way of intercepting the response, checking for these custom error messages, and then returning <code>SwiftyJSON</code> to the caller. All models have an init that creates a <code>SwiftyJSON</code> model.</p>
  <p>What models look like - simplified:</p>
  <pre><code>final public class SomeImage: ResponseCollectionSerializable {
      
      public static func collection(json: JSON) -&gt; [SomeImage] {
          var images = [SomeImage]()
          
          for (_, subJson):(String, JSON) in json[&quot;children&quot;] {
              if let image = SomeImage(json: subJson) {
                  images.append(image)
              }
          }
          
          return images
      }
      
      init?(json: JSON) {
          
          guard let id = json[&quot;attributes&quot;][&quot;id&quot;].string, let name = json[&quot;name&quot;].string else {
              return nil
          }
          
          imageURL = json[&quot;attributes&quot;][&quot;imageURL&quot;].string
          
          super.init(withId: id, name: name)
      }
      
      // Attributes
      let imageURL: String?
  }
  </code></pre>
  <p>Protocol on collection models:</p>
  <pre><code>public protocol ResponseCollectionSerializable {
      static func collection(json: JSON) -&gt; [Self]
  }
  </code></pre>
  <p>Problem area:</p>
  <pre><code>extension Alamofire.DataRequest {
      
      public func responseCollection&lt;T: ResponseCollectionSerializable&gt;(completionHandler: @escaping (DataResponse&lt;[T], AFError&gt;) -&gt; Void) -&gt; Self {
          let responseSerializer = DataResponseSerializer&lt;[T]&gt; { request, response, data, error in
          // error: Trailing closure passed to parameter of type 'DataPreprocessor' that does not accept a closure
  
              guard error == nil else {
                  return .failure(error!)
              }
              
              let JSONSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
              let result = JSONSerializer.serializeResponse(request, response, data, error)
              
              switch result {
              case .success(let value):
                  if response != nil  {
                      let json = JSON(value)
                      return .success(T.collection(json: json))
                  } else {
                      let failureReason = &quot;Response collection could not be serialized&quot;
                      let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
                      let error = NSError(domain: Bundle.main.bundleIdentifier!, code: MyErrorCode.JSONSerializationFailed.rawValue, userInfo: userInfo)
                      return .failure(error) // custom Error
                  }
              case .failure(let error):
                  return .failure(error) // AFError
          }
          
          return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
      }
  }
  </code></pre>
  <p>In simple terms, my question is how in Alamofire 5 can I run this intercepting code on all calls and return SwiftyJSON?</p>