swagger-api / swagger-api/swagger-codegen

[SWIFT 4 ] generated swift4 models are not building.

Open
#8,289 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the attached swagger_1.yaml and the command swagger-codegen generate -i swagger_1.yaml -l swift4. Inspect the generated model files in Models.zip, especially their encode and decode methods, and build them in the reported Xcode setup. Done means the generated models compile without the CodingKey error while remaining usable without the other generated files.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.