Swift 4 支持 swagger 代码生成

Swift 4 support for swagger code gen

我在从 YAML 文件生成 swift 模型时遇到问题。

swagger-codegen generate -i swagger_1.yaml -l swift4

下面是示例模型之一。这不是在 swift 4 中构建的。因为没有编码密钥

import Foundation

open class User: Codable {

    public enum Sex: String, Codable { 
        case male = "male"
        case female = "female"
    }
    public var id: Int64?
    public var username: String?
    public var firstName: String?
    public var lastName: String?
    public var sex: Sex?



    public init(id: Int64?, username: String?, firstName: String?, lastName: String?, sex: Sex?) {
        self.id = id
        self.username = username
        self.firstName = firstName
        self.lastName = lastName
        self.sex = sex
    }


    // Encodable protocol methods

    public func encode(to encoder: Encoder) throws {

        var container = encoder.container(keyedBy: String.self)

        try container.encodeIfPresent(id, forKey: "id")
        try container.encodeIfPresent(username, forKey: "username")
        try container.encodeIfPresent(firstName, forKey: "firstName")
        try container.encodeIfPresent(lastName, forKey: "lastName")
        try container.encodeIfPresent(sex, forKey: "sex")
    }

    // Decodable protocol methods

    public required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: String.self)

        id = try container.decodeIfPresent(Int64.self, forKey: "id")
        username = try container.decodeIfPresent(String.self, forKey: "username")
        firstName = try container.decodeIfPresent(String.self, forKey: "firstName")
        lastName = try container.decodeIfPresent(String.self, forKey: "lastName")
        sex = try container.decodeIfPresent(Sex.self, forKey: "sex")
    }
}

也在 GitHub 中发布了这个问题。

https://github.com/swagger-api/swagger-codegen/issues/8289

您需要添加 enum CodingKeys 并重写 initFromDecoder/encode。

此外,在您的情况下,根本不需要单独的 encode/decode/CodingKeys,因为 class 的所有属性都已经 Codable。

open class User: Codable {
    ....
    enum CodingKeys: String, CodingKey {
        case id, username, firstName, lastName, sex
    }

    // Encodable protocol methods

    public func encode(to encoder: Encoder) throws {

        var container = encoder.container(keyedBy: CodingKeys.self)

        try container.encodeIfPresent(id, forKey: .id)
        try container.encodeIfPresent(username, forKey: .username)
        try container.encodeIfPresent(firstName, forKey: .firstName)
        try container.encodeIfPresent(lastName, forKey: .lastName)
        try container.encodeIfPresent(sex, forKey: .sex)
    }

    // Decodable protocol methods

    public required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        id = try container.decodeIfPresent(Int64.self, forKey: .id)
        username = try container.decodeIfPresent(String.self, forKey: .username)
        firstName = try container.decodeIfPresent(String.self, forKey: .firstName)
        lastName = try container.decodeIfPresent(String.self, forKey: .lastName)
        sex = try container.decodeIfPresent(Sex.self, forKey: .sex)
    }
}