swagger-api / swagger-api/swagger-codegen

[SWIFT] Generator does not export containers of enums correctly

Open
#3,814 0 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

Description

When using a swagger.json with containers of enums, the exporter creates swift code that does not compile.

In User.swift

public enum [String:String]: String { 
   case Read = "Read"
   case Write = "Write"
}
public enum [RolesIsASetOfEnums]: String { 
    case User = "User"
    case Admin = "Admin"
}

In Models.swift

instance.permissionsIsAStringToEnumMap = User.[String:String](rawValue: (sourceDictionary["permissions_IsAStringToEnumMap"] as? String) ?? "")
instance.rolesIsASetOfEnums = User.[RolesIsASetOfEnums](rawValue: (sourceDictionary["roles_IsASetOfEnums"] as? String) ?? "")
Swagger-codegen version

2.3.0

Swagger declaration file content or url

User.json.zip

Command line used for generation

generate -i User.json -l swift -o UserModel --additional-properties unwrapRequired=true,responseAs=PromiseKit

Steps to reproduce

Just run the generator

Related issues

https://github.com/swagger-api/swagger-codegen/issues/2600
This bug describes the root of the issue, and has a work around in java, which will build and compile. However, while that issue has a workaround, the swift version does not.

Suggest a Fix

I was able to change model.mustache to use this line:

public enum {{enumName}}: String { {{#allowableValues}}{{#values}}

Which fixed the model declaration, but there is code generated in the Models.swift decoders that I was not able to figure out.

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 generator command using the attached User.json.zip and inspect the generated User.swift and Models.swift files. Start with model.mustache, then trace the generated decoder expressions for container-of-enum properties. Done means the generated Swift declarations and decoders compile correctly for the reported enum containers.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.