# Open API Tools & Swift - Personal Experience

**URL:** <https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962>\
**Category:** Pitches\
**Created:** [December 21, 2019, 5:09pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962 "2019-12-21T17:09:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![spprichard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spprichard/32/22828_2.png) [@spprichard](https://forums.swift.org/u/spprichard)\
**Post date:** [December 21, 2019, 5:09pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/1 "2019-12-21T17:09:12Z")

</div>

This post is meant to illustrate the current state of tooling for Server Side Swift projects written from the perspective of someone who is still very new to Swift, and is probably making some assumptions that may prove to be incorrect.

I recently began a personal project in which I wanted to use Kubernetes (K8s) & Swift. To get started I created a library that was going to house all the K8s types, and the HTTP Client for communicating with the K8s API. With the goal of importing this library into all the other projects I had planned. Lucky enough for me, K8s uses the OpenAPI Spec, so generating the client code should be easy. I was mistaken, for Swift that is not the case. Specifically for any Swift client that you wanted to run on Linux. At the time of writing this, there did not exist a Swift Client that the Open-API code generation tool could generate that could be ran on Linux. I had concluded, with the help of others, that if I wanted a Pure-Swift Kubernetes library that could be ran on Linux, I would have to write the HTTP Client code myself.

Project: [https://github.com/spprichard/Swifternetes](https://github.com/spprichard/Swifternetes)  
This project is still in the very early stages, and I am still learning Swift.

All is not lost however, the Open-Api generator could generate the model code for the entire K8s API, which is a big win! What I mean by model code is, that the Request/Response types, all the Resource types (Pod, Deployment, Service, etc). There is a bit of a down side here too, the latest version of the Open-Api Generator for Swift is 4.2. At face value this seems like a minor issue to me because the library I was building was targeted for Swift 5.0 and later. I don’t think there would be any breaking changes in 4.2 to 5.0 in this case, but something to note.

I also noticed a lack of Codable support. It was not clear to me how I could get Codable support fo the the types the generator would generate without having basically write the types myself. Which defeats the purpose of the Open-Api Code Generator all together.

As it stands today, the Swifternetes project is off the ground, if even by an inch! Along the way I have learned a lot, and hopefully will consider to do so. The reason for this post is to discuss my experiences trying to build this library, and possible areas of focus for the Server Working Group in 2020

**Tools I used:**

- The Kubernetes Specific generator

- Open-API Generator

**Areas of Potential Interest**

- Give the Open-Api Code Generator for Swift that ability to produce clients that can be compiled and ran on Linux.
- Add support for the new AsyncHTTPClient as one of the client the Open-Api Code generator can generate.
- Update the Open-Api Code Generator for Swift to 5.0 and later
- Give the Open-Api Code Generator for Swift the ability to generate models that conform to Codable

I look forward to the discussion to follow!

---

<div class="post-metadata">

**Author:** ![thecheatah](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thecheatah/32/7276_2.png) [@thecheatah](https://forums.swift.org/u/thecheatah)\
**Post date:** [December 23, 2019, 5:50am UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/2 "2019-12-23T05:50:27Z")

</div>

@spprichard I started an [oas project for a vapor 4 server](https://github.com/thecheatah/vapor-server-codegen). It uses Codables. As I ramp up my vapor project, I’ll put together a Codegen for the AsyncHTTPClient as well.

I would love to get your opinion on the vapor 4 Codegen if you get a chance. I am actively working on it and addressing issues as I run into them.

---

<div class="post-metadata">

**Author:** ![spprichard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spprichard/32/22828_2.png) [@spprichard](https://forums.swift.org/u/spprichard)\
**Post date:** [December 23, 2019, 2:49pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/3 "2019-12-23T14:49:51Z")

</div>

@thecheatah Yea I looked at that this weekend. I'm for sure interested in it.

So for my specific case, I used this project [GitHub - kubernetes-client/gen: Common generator scripts for all client libraries](https://github.com/kubernetes-client/gen) to generate the Swift code for my Switernetes project. However, it looks to use a swagger.json? I'm really not sure how swagger and Open-Api play together. If any one has knowledge on this I'm all ears. Would your tool be able to take a swagger.json? The examples I see look to use a .yaml file.

---

<div class="post-metadata">

**Author:** ![Joseph\_Heck](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joseph_heck/32/22369_2.png) [@Joseph\_Heck](https://forums.swift.org/u/Joseph_Heck)\
**Post date:** [December 23, 2019, 3:33pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/4 "2019-12-23T15:33:53Z")

</div>

openAPI is the evolution of what started as swagger, so there might be some tweaking of some specifics, but it should handle it as long as it can read and understand earlier versions of the openAPI spec.

---

<div class="post-metadata">

**Author:** ![thecheatah](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thecheatah/32/7276_2.png) [@thecheatah](https://forums.swift.org/u/thecheatah)\
**Post date:** [December 23, 2019, 3:40pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/5 "2019-12-23T15:40:22Z")

</div>

@spprichard I can try to reduce the confusion for you. Swagger went through [rebranding exercise](https://swagger.io/blog/api-strategy/difference-between-swagger-and-openapi/) and version "3" of swagger was named OpenAPI Spec 3 (oas3). So it's swagger 1 (no-one really uses), swagger 2 and openapi spec 3.

The contents of the swagger or openapi spec can either be yaml or json. [This online editor](http://editor.swagger.io) can convert between the two for you.

---

<div class="post-metadata">

**Author:** ![spprichard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spprichard/32/22828_2.png) [@spprichard](https://forums.swift.org/u/spprichard)\
**Post date:** [December 23, 2019, 3:52pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/6 "2019-12-23T15:52:59Z")

</div>

@Joseph_Heck & @thecheatah thanks for clearing that up!

@thecheatah I will for sure try your tool for my Swifternetes project. Having the AsyncHTTPClient option is exactly what I need for this project to take off. Can you keep me posted on the progress? Also, let me know if I can help in anyway

---

<div class="post-metadata">

**Author:** ![spprichard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spprichard/32/22828_2.png) [@spprichard](https://forums.swift.org/u/spprichard)\
**Post date:** [December 23, 2019, 4:10pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/7 "2019-12-23T16:10:29Z")

</div>

@thecheatah Do you know of a tool beside the online editor for converting JSON definitions to YAML? The online one not like the size of the Kubernetes spec.....Web page freezes

---

<div class="post-metadata">

**Author:** ![thecheatah](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thecheatah/32/7276_2.png) [@thecheatah](https://forums.swift.org/u/thecheatah)\
**Post date:** [December 23, 2019, 4:14pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/8 "2019-12-23T16:14:10Z")

</div>

Take a look at [this post](https://stackoverflow.com/questions/50846431/converting-a-yaml-file-to-python-json-object). Looks like you can do it using Python on command line.

Google chrome might be your best bet with the online tool if you want to give that a go.

Also, I will let you know when I get started on the AsyncHTTPClient codegen.

---

<div class="post-metadata">

**Author:** ![spprichard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spprichard/32/22828_2.png) [@spprichard](https://forums.swift.org/u/spprichard)\
**Post date:** [December 23, 2019, 4:16pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/9 "2019-12-23T16:16:39Z")

</div>

Fantastic! Will do

---

<div class="post-metadata">

**Author:** ![wing328](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wing328/32/10129_2.png) [@wing328](https://forums.swift.org/u/wing328)\
**Post date:** [January 9, 2020, 8:18am UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/10 "2020-01-09T08:18:43Z")

</div>

> [@spprichard](#):
>
> - Update the Open-Api Code Generator for Swift to 5.0 and later

We've added a Swift 5 client generator (beta) to OpenAPI Generator. Please check it out and let us know if you've any feedback:

Ref: [Reddit - Dive into anything](https://www.reddit.com/r/swift/comments/elbq9n/autogenerate_swift_5_api_client_for_rest_apis/)

---

<div class="post-metadata">

**Author:** ![tanner0101](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tanner0101/32/675_2.png) [@tanner0101](https://forums.swift.org/u/tanner0101)\
**Post date:** [January 9, 2020, 4:02pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/11 "2020-01-09T16:02:43Z")

</div>

> [@spprichard](#):
>
> In order to use this tool you must create a swift.sh, and a swift.xml. I can post my version of this file upon request.

@spprichard would you consider contributing those files to the [GitHub - kubernetes-client/gen: Common generator scripts for all client libraries](https://github.com/kubernetes-client/gen) repo itself?

---

<div class="post-metadata">

**Author:** ![spprichard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spprichard/32/22828_2.png) [@spprichard](https://forums.swift.org/u/spprichard)\
**Post date:** [January 9, 2020, 4:15pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/12 "2020-01-09T16:15:21Z")

</div>

Yea, I don't see why not. I'll try to do that this weekend

---

<div class="post-metadata">

**Author:** ![spprichard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spprichard/32/22828_2.png) [@spprichard](https://forums.swift.org/u/spprichard)\
**Post date:** [January 9, 2020, 4:16pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/13 "2020-01-09T16:16:18Z")

</div>

@wing328 That's awesome!

Correct me if I am wrong here, but URLSession doesn't compile for Linux?

---

<div class="post-metadata">

**Author:** ![tanner0101](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tanner0101/32/675_2.png) [@tanner0101](https://forums.swift.org/u/tanner0101)\
**Post date:** [January 9, 2020, 5:01pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/14 "2020-01-09T17:01:37Z")

</div>

> [@spprichard](#):
>
> Correct me if I am wrong here, but URLSession doesn't compile for Linux?

URLSession used to have some difficulties on Linux in the very early days, but it works great now. The only concern with using URLSession is for non-blocking server apps where the different concurrency models could create performance bottlenecks. URLSession has also been moved to the `FoundationNetworking` module on Linux only (to avoid linking cURL everywhere) which can be a sharp edge for people.

---

<div class="post-metadata">

**Author:** ![spprichard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spprichard/32/22828_2.png) [@spprichard](https://forums.swift.org/u/spprichard)\
**Post date:** [January 9, 2020, 5:18pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/15 "2020-01-09T17:18:46Z")

</div>

@tanner0101 Cool, that makes sense. Thank you.

So there is still a need to have a non-blocking library option for the OpenAPI Code Gen project?

---

<div class="post-metadata">

**Author:** ![tanner0101](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tanner0101/32/675_2.png) [@tanner0101](https://forums.swift.org/u/tanner0101)\
**Post date:** [January 9, 2020, 7:40pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/16 "2020-01-09T19:40:27Z")

</div>

> [@spprichard](#):
>
> So there is still a need to have a non-blocking library option for the OpenAPI Code Gen project?

Yeah I definitely think so. I think it should be available in addition to URLSession. For iOS use cases URLSession (or Alamofire) make a lot of sense. For server, especially in cases where non-blocking performance is critical, you will want AsyncHTTPClient.

It looks like openapi-generator has different files for Alamofire, URLSession, RxSwift, etc ([https://github.com/OpenAPITools/openapi-generator/blob/master/bin/swift5-petstore-urlsession.json](https://github.com/OpenAPITools/openapi-generator/blob/master/bin/swift5-petstore-urlsession.json)). Could we add AsyncHTTPClient support next to those?

---

<div class="post-metadata">

**Author:** ![wing328](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wing328/32/10129_2.png) [@wing328](https://forums.swift.org/u/wing328)\
**Post date:** [January 10, 2020, 2:29am UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/17 "2020-01-10T02:29:25Z")

</div>

> [@tanner0101](#):
>
> Could we add AsyncHTTPClient support next to those?

Definitely. We welcome contribution on that. Can you please open an issue/ticket via [Sign in to GitHub · GitHub](http://github.com/OpenAPITools/openapi-generator/issues/new) to track this?

---

<div class="post-metadata">

**Author:** ![tanner0101](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tanner0101/32/675_2.png) [@tanner0101](https://forums.swift.org/u/tanner0101)\
**Post date:** [January 10, 2020, 2:49am UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/18 "2020-01-10T02:49:57Z")

</div>

> [@wing328](#):
>
> Can you please open an issue/ticket

Here it is: [Add Support for Swift's AsyncHTTPClient · Issue #4957 · OpenAPITools/openapi-generator · GitHub](https://github.com/OpenAPITools/openapi-generator/issues/4957)

Thanks!

---

<div class="post-metadata">

**Author:** ![Yonas\_Kolb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/yonas_kolb/32/10222_2.png) [@Yonas\_Kolb](https://forums.swift.org/u/Yonas_Kolb)\
**Post date:** [January 16, 2020, 9:19pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/19 "2020-01-16T21:19:42Z")

</div>

SwagGen might be another tool worth exploring, which is itself written in Swift and uses Stencil templates [GitHub - yonaskolb/SwagGen: OpenAPI/Swagger 3.0 Parser and Swift code generator](https://github.com/yonaskolb/SwagGen)

By default it generates client side code, but the templates are very customizable. The models also have built in Codable support

---

<div class="post-metadata">

**Author:** ![wing328](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wing328/32/10129_2.png) [@wing328](https://forums.swift.org/u/wing328)\
**Post date:** [February 1, 2020, 2:23pm UTC](https://forums.swift.org/t/open-api-tools-swift-personal-experience/31962/20 "2020-02-01T14:23:44Z")

</div>

UPDATE: We've released OpenAPI Generator v4.2.3, which includes the Swift 5 client generator and many enhancements, bug fixes: [https://twitter.com/oas\_generator/status/1223289771813785601](https://twitter.com/oas_generator/status/1223289771813785601)
