swagger-api / swagger-api/swagger-codegen

[Swift 4] Property with type 'Any' does not conforms with codable protocol

Open
#6,483 15 comments 17 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

Current code generator for Swift 4 generates invalid code for JSON dictionaries or properties with object value type.

Property generated code:

open class TaskParameter: Codable {

    public var id: String?
    public var value: Any?

    public init() {}

}

Dictionary generated code:

open class EntityKey: Codable {
    public var entity: String?
    public var alias: String?
    public var fields: [String:Any]?

    public init() {}
} 
Swagger-codegen version

2.2.3

Swagger declaration file content or url
"definitions": {
"TaskParameter": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                },
                "value": {
                    "type": "object"
                }
            }
        },
"EntityKey": {
            "type": "object",
            "properties": {
                "entity": {
                    "type": "string"
                },
                "alias": {
                    "type": "string"
                },
                "fields": {
                    "type": "object",
                    "additionalProperties": {
                        "type": "object"
                    }
                }
            }
        }
}
Command line used for generation

swagger-codegen generate -i ./swagger.json -l swift4 -o ./api

Steps to reproduce

Generate the code with swagger-codegen 2.2.3 then try to compile the generated classes on Xcode 9 GM or any beta version.

Related issues/PRs
Suggest a fix/enhancement

Seems like this problem relies on the fact that Any is not (and will be not) Codable, thus a generic "AnyCodable" that conforms to this protocol should be used instead.

StackOverflow Discussion
Apple Forum Discussion

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 supplied Swagger definition and the swagger-codegen generate -i ./swagger.json -l swift4 -o ./api command, then inspect the Swift 4 generator output for Codable properties and dictionaries typed as Any. Done means generated code for object-valued properties compiles under Xcode 9 and supports the reported JSON shapes.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
mobile-dev, 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.