swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.89k stars 6.03k forks source link

[SWIFT 4 ] generated swift4 models are not building. #8289

Open VMSureshKumar opened 6 years ago

VMSureshKumar commented 6 years ago

I create the swift 4 models but the model is not building in latest Xcode.

my yaml file is attached.

swagger_1.yaml.zip

command to generate the swift4 files

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

generated swift4 models attached below.

Models.zip

I took ONLY the models and put it in my project file. I dont need the rest of the files. My main objective is to generate only model classes.

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")
}

The models are not building.... lines inside encode and decode "String.self" is not building. .. also explicit encoder and decoders are not required in swift4 unless we have different names in json response..

The error is

In argument type 'String.Type', 'String' does not conform to expected type 'CodingKey'

VMSureshKumar commented 6 years ago

@ehyche @wing328 can you please help.

wing328 commented 6 years ago

@VMSureshKumar Thanks for tagging me but I'm no longer involved in this project. I hope others will be able to help you out. Good luck.

waveChand commented 5 years ago

Also a got not building issue. swagger- codegen -2.4.0-SNAPSHOT - code generated for -swift4 using in xcode10 with swift4.2 Reason - In ApiResponse class Any? is not confirm to decodable. Have someone encounter with this issue

ehyche commented 5 years ago

This usually happens when you have an undefined object in your swagger schema: something that just has type=object but no schema or ref.

So it is most likely an issue with your schema.

Eric

On Fri, Nov 2, 2018 at 7:28 AM waveChand notifications@github.com wrote:

Also a got not building issue. swagger- codegen -2.4.0-SNAPSHOT - swift4 - xcode10 Reason - In ApiResponse class Any? is not confirm to decodable. Have someone encounter with this issue

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/swagger-api/swagger-codegen/issues/8289#issuecomment-435349808, or mute the thread https://github.com/notifications/unsubscribe-auth/AD8xdFRGxvv9p6j2ZdSW1fcKO1LQxXWmks5urCxagaJpZM4UddIV .

-- Eric Hyche ehyche@gmail.com