Skip to content

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

@vntguca

Description

@vntguca
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions