swift-openapi-generator
by
apple

Description: Generate Swift client and server code from an OpenAPI document.

View apple/swift-openapi-generator on GitHub ↗

Summary Information

Updated 2 hours ago
Added to GitGenius on June 21st, 2025
Created on April 20th, 2023
Open Issues/Pull Requests: 129 (+0)
Number of forks: 157
Total Stargazers: 1,848 (+0)
Total Subscribers: 114 (+0)
Detailed Description

The Apple `swift-openapi-generator` repository provides a command-line tool for generating Swift code from OpenAPI (formerly Swagger) specifications. It’s designed to streamline the process of integrating with REST APIs in Swift projects, reducing boilerplate and improving developer productivity. The core functionality revolves around taking a YAML or JSON OpenAPI document as input and producing type-safe Swift structures representing the API’s resources, operations, and data models. This generated code can then be used directly within Swift applications to interact with the API.

The generator supports OpenAPI versions 2.0 and 3.0/3.1, covering a wide range of API definitions. It focuses on generating client-side code, specifically models (structs conforming to `Codable`) for request and response bodies, and client objects that encapsulate the API’s endpoints and their associated methods. A key feature is its ability to handle complex OpenAPI features like polymorphism, enums, and nested objects, translating them into equivalent Swift representations. The generated code aims to be clean, readable, and maintainable, adhering to Swift best practices. It doesn't attempt to generate full-fledged networking layers (like using `URLSession` directly); instead, it provides the data models and API interface, allowing developers to choose their preferred networking library (e.g., Alamofire, Moya) for actual API calls.

The tool is built using Swift itself and leverages the `Yams` library for parsing YAML files and the `ArgumentParser` framework for handling command-line arguments. This means it can be easily integrated into CI/CD pipelines and automated build processes. Configuration is primarily done through command-line options, allowing users to customize the output directory, the name of the generated module, and various code generation settings. Furthermore, the generator supports customization through templates. Users can modify the default templates (Handlebars-based) to tailor the generated code to their specific project requirements, such as adding custom documentation, modifying naming conventions, or integrating with specific dependency injection frameworks.

The repository includes comprehensive documentation, including a detailed guide on installation, usage, and customization. It also provides example OpenAPI specifications and generated code to help users get started quickly. The project is actively maintained by Apple, with regular updates and bug fixes. The `Examples` directory within the repository is particularly useful, showcasing how to generate code from different OpenAPI specifications and demonstrating the customization options available.

Beyond basic code generation, the tool offers features like generating documentation comments based on the OpenAPI description, handling date and time formats, and supporting different data types. It also includes mechanisms for handling API key authentication and other common security schemes defined in the OpenAPI specification. The project’s goal is to be a robust and reliable solution for Swift developers needing to interact with REST APIs, significantly reducing the manual effort involved in creating and maintaining API client code.

swift-openapi-generator
by
appleapple/swift-openapi-generator

Repository Details

Fetching additional details & charts...