Home

Awesome

⚠️ This project is no longer under development. Please see alternatives including a project maintained by Apple

SwagGen

Platforms SPM Git Version license

SwagGen is a library and command line tool for parsing and generating code for OpenAPI/Swagger 3.0 specs, completely written in Swift.

Swagger 2 support has been removed. For Swagger 2 use version 3.0.2 or the swagger_2 branch

Swagger parser

It contains a Swagger library that can be used in Swift to load and parse Swagger specs.

Swagger code generator

SwagGen is command line tool that generates code from a OpenAPI/Swagger 3.0 spec. Templates for any language can be written that leverage this generator.

Swift template

SwagGen includes a bundled template for generating a client side Swift library for interfacing with the Swagger spec. It includes support for model inheritance, shared enums, discrete and mutable request objects, inline schemas, Codable and Equatable models, configurable options, generic networking stack, and many other niceties.

Installing

Make sure Xcode 11+ is installed first.

Mint

$ mint install yonaskolb/SwagGen

Homebrew

$ brew tap yonaskolb/SwagGen https://github.com/yonaskolb/SwagGen.git
$ brew install SwagGen

Make

$ git clone https://github.com/yonaskolb/SwagGen.git
$ cd SwagGen
$ make install

Swift Package Manager

Use as CLI

$ git clone https://github.com/yonaskolb/SwagGen.git
$ cd swaggen
$ swift run

Use as dependency

Add the following to your Package.swift file's dependencies:

.package(url: "https://github.com/yonaskolb/SwagGen.git", from: "4.4.0"),

And then import wherever needed:

import SwagGenKit
import Swagger

Usage

Use --help to see usage information

swaggen --help
Usage: swaggen <command> [options]

Commands:
  generate        Generates a template from a Swagger spec
  help            Prints this help information
  version         Prints the current version of this app

generate

swaggen generate path_to_spec

Use swaggen generate --help to see the list of generation options.

Example:

swaggen generate http://myapi.com/spec --template Templates/Swift  --destination generated --option name:MyAPI --option "customProperty: custom value --clean leave.files"

For the Swift template, a handy option is name, which changes the name of the generated framework from the default of API. This can be set in the template or by passing in --option name:MyCoolAPI.

Swift Template

List of all available options:

nameactionexpected valuesdefault value
namename of the APIStringAPI
authorsauthors in podspecStringYonas Kolb
baseURLbaseURL in APIClientStringfirst scheme, host, and base path of spec
fixedWidthIntegerswhether to use types like Int32 and Int64Boolfalse
homepagehomepage in podspecStringhttps://github.com/yonaskolb/SwagGen
modelPrefixmodel by adding a prefix and model file nameStringnull
modelSuffixmodel by adding a suffix and model file nameStringnull
mutableModelswhether model properties are mutableBooltrue
modelTypewhether each model is a struct or classStringclass
modelInheritancewhether models use inheritance. Must be false for structsBooltrue
modelNamesoverride model names[String: String][:]
modelProtocolcustomize protocol name that all models conform toStringAPIModel
enumNamesoverride enum names[String: String][:]
enumUndecodableCasewhether to add undecodable case to enumsBoolfalse
propertyNamesoverride property names[String: String][:]
safeArrayDecodingfilter out invalid items in array instead of throwingBoolfalse
safeOptionalDecodingset invalid optionals to nil instead of throwingBoolfalse
tagPrefixprefix for all tagsStringnull
tagSuffixsuffix for all tagsStringnull
codableResponsesconstrains all responses to be CodableBoolfalse
anyTypeoverride Any with custom typeStringAny
numberTypeoverride default number type when format not specifiedStringDouble

If writing your own Swift template there are a few types that are generated that you will need to provide typealias's for:

Development

If you want to pass any required arguments when running in Xcode, you can edit the scheme to include launch arguments.

Templates

Templates are made up of a template config file, a bunch of Stencil files, and other files that will be copied over during generation

Template config

This is the configuration and manifest file for the template in YAML or JSON format. It can contain:

An example template for Swift can be found here

Template Files

These files follow the Stencil file format outlined here https://stencil.fuller.li

Formatters

Formatters change what information is available to the templates and how it's formatted. They can be specified via the formatter property in the template config. Usually these would map to a specific target language, but can be customized for different purposes.

Output Languages

SwagGen can be used to generate code for any language. At the moment there is only a formatter and template for Swift

Swift API usage

Usage documentation can be found in the Readme that is generated with your template.


Attributions

This tool is powered by:

Thanks also to Logan Shire and his initial work on Swagger Parser

Alternatives

Contributions

Pull requests and issues are welcome

License

SwagGen is licensed under the MIT license. See LICENSE for more info.