Skip to content

Create a swift4 code generator and templates #6002

@ehyche

Description

@ehyche
Description

Swift 4 (announced at WWDC 2017 in June 2017) introduces some features which make serialization and deserialization in Swift much simpler:

  • The Codable protocol, which is actually two protocols (Decodable and Encodable)
  • Because of protocol extensions, for most cases, all the model classes have to do is simply declare their conformance to the Codable protocol.

I have written an Xcode playground which explores lots of details about the Codable protocol here:

https://github.com/ehyche/xcode-playgrounds

in the LearningAboutCodable.playground file.

So I have created a "swift4" language for swagger-codegen which greatly simplifies the mustache templates, and thus the generated models. Now, most models simply have the properties and that's it. For example, here is the Pet class:

open class Pet: Codable {

    public enum Status: String, Codable { 
        case available = "available"
        case pending = "pending"
        case sold = "sold"
    }
    public var id: Int64?
    public var category: Category?
    public var name: String?
    public var photoUrls: [String]?
    public var tags: [Tag]?
    /** pet status in the store */
    public var status: Status?

    public init() {}

}
Swagger-codegen version

I am proposing adding this to master. I have created the swift4 language on a branch, and will be creating a PR for it.

Command line used for generation
./bin/swift4-petstore-all.sh
Steps to reproduce

New language.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions