Swagger/OpenAPI Generation for Swift (iOS/macOS)
Developing mobile applications for iOS and macOS often requires integration with REST APIs. Manually writing the networking layer is a time-consuming and error-prone process. Code generation tools based on OpenAPI (Swagger) specifications allow you to automatically create typed Swift models, network requests, and even full-fledged API clients. This speeds up development, reduces the number of bugs, and simplifies code maintenance when the API changes.
In this article, we will review the main tools for generating Swift code from OpenAPI specifications, show examples of their use, and provide recommendations for integration into iOS/macOS projects.
Main Generation Tools
Today, there are several popular solutions that support Swift as a target language:
- OpenAPI Generator — the most popular open-source tool. Supports many languages, including Swift 5 and Combine.
- Swagger Codegen — the predecessor of OpenAPI Generator, still supported but less flexible.
- Create API — a generator from Apple, built into Xcode 15+, for creating clients from OpenAPI specifications.
Let's look at each of them in more detail.
OpenAPI Generator for Swift
OpenAPI Generator is a universal tool that can be used as a CLI, Docker image, or plugin for Gradle/Maven. Several generators are available for Swift:
swift5— generation in Swift 5 with support for Codable, URLSession, and Combine.swift5-deprecated— an older version for backward compatibility.
Installation and Usage
The easiest way is to use Docker:
docker run --rm -v "${PWD}:/local" \ openapitools/openapi-generator-cli generate \ -i /local/spec.yaml \ -g swift5 \ -o /local/SwiftClientAfter execution, a generated Xcode project with models, API requests, and tests will appear in the SwiftClient folder.
Example of Generated Code
Suppose we have a specification with an endpoint for getting a user:
openapi: 3.0.0info: title: User API version: 1.0.0paths: /users/{id}: get: operationId: getUserById parameters: - name: id in: path required: true schema: type: integer responses: '200': description: OK content: application/json: schema: $ref: '#/components/schemas/User'components: schemas: User: type: object properties: id: type: integer name: type: string email: type: string required: - id - name - emailOpenAPI Generator will create the following Swift model:
import Foundation
public struct User: Codable { public let id: Int public let name: String public let email: String
enum CodingKeys: String, CodingKey { case id case name case email }}And the corresponding API method:
import Foundationimport Combine
public class UserAPI { public static func getUserById(id: Int) -> AnyPublisher<User, Error> { var request = URLRequest(url: URL(string: "https://api.example.com/users/\\(id)")!) request.httpMethod = "GET" return URLSession.shared.dataTaskPublisher(for: request) .map(\\.data) .decode(type: User.self, decoder: JSONDecoder()) .eraseToAnyPublisher() }}Create API from Apple (Xcode 15+)
Starting with Xcode 15