question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[REQ] [Swift5] POP proposal

See original GitHub issue

Hi guys,

I’ve been using the OpenAPI Generator for Swift 4/5 for a while now, quite happy with your work, thanks!

But after a while I struggled a bit whenever I needed to add some customisation on the generated code. Mainly the issues I found were related to the fact that the methods are defined as class func. That makes it hard to:

  • override: you would need to define a class SomethingSubclassAPI that inherits from the generated SomethingAPI class and override the method. Then you would need to use the subclass around. Easy to do if that happens at the beginning of the project, much more complicated if you already used a lot the generated class around
  • it forces you to pass through the same classes for all the calls, hence the problem above

I then modified the templates to achieve something different.

From this:

open class AlertAPI {
    /**
     Returns the alert
     
     - parameter ifNoneMatch: (header) The value of the Etag header returned in the previous response (optional)
     - parameter completion: completion handler to receive the data and the error objects
     */
    open class func getAlert(ifNoneMatch: String? = nil, completion: @escaping ((_ data: [Alert]?,_ error: Error?) -> Void)) {
        getAlertWithRequestBuilder(ifNoneMatch: ifNoneMatch).execute { (response, error) -> Void in
            completion(response?.body, error)
        }
    }
}

To this:

public protocol AlertAPIProtocol {
  /**
   Returns the alert
   
   - parameter ifNoneMatch: (header) The value of the Etag header returned in the previous response (optional)
   - parameter completion: completion handler to receive the data and the error objects
   */
   static func getAlert(ifNoneMatch: String?, completion: @escaping ((_ data: [Alert]?, _ headers:[String:String]?, _ error: Error?) -> Void))
}

public extension AlertAPIProtocol {

    static func getAlert(ifNoneMatch: String? = nil, completion: @escaping ((_ data: [Alert]?, _ headers:[String:String]?, _ error: Error?) -> Void)) {
        getAlertWithRequestBuilder(ifNoneMatch: ifNoneMatch).execute { (response, error) -> Void in
            completion(response?.body, response?.header, error)
        }
    }
}

This approach works quite well for me and it allows me to:

  • conform to the protocol in each class/struct/enum where I need to perform such API request
  • override the methods whenever and wherever I need to. I can override in a single case, if needed, or in a subprotocol, or even make conditional override based on conditions through the where operator
  • plug different protocols to the same class/struct/enum, composing different APIs by needs

Additionally I have an autogenerated Mock version of the same protocols, that inherits from the original ones, that load content from a json in the bundle. This approach opens to the possibility of conforming to AlertAPIPrototocol in a target and to AlertAPIMockPrototocol in another and being one a subprotocol to the other you can easily use polymorphism. This is just an example of what this approach can lead to. 😃

I’d love to see this approach implemented in the official OpenAPI Generator for swift, I think it could be useful for a lot of people. Are you interested on discussing this further?

Thanks, Alessandro

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
4brunucommented, Jun 6, 2021

Now that https://github.com/OpenAPITools/openapi-generator/pull/9625 is merged, I think it’s safe to start working on POP implementation 🙂

1reaction
frabarbocommented, Feb 9, 2021

Follow. it could help me too!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I pop specific View Controller in Swift - Stack Overflow
Swift 5. To pop to the latest instance of a specific class, ... Find your view controller from navigation stack and pop to...
Read more >
Protocol-Oriented Programming in Swift 5: Familiarize yourself ...
Understanding the Protocol-Oriented Programming (POP) paradigm is imperative if you plan on designing and implementing software using Swift 5. In this book, you ......
Read more >
Protocol-Oriented Programming in Swift 5: Familiarize yourself ...
Understanding the Protocol-Oriented Programming (POP) paradigm is imperative if you plan on designing and implementing software using Swift ...
Read more >
Build a Bluetooth App using Swift 5 | Adafruit Learning System
You'll need a basic understanding of Swift, but no prior experience with Bluetooth Low Energy is required. The example code provided in this...
Read more >
An Introduction to Protocol-oriented Programming in Swift
Most modern programming languages, in the hopes of enhanced maintainability and reusability of code, offer some constructs that help the developer keep the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found