yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator
MIT License
625 stars 147 forks source link

add ability to encode encodable objects #113

Closed wqz-leo closed 5 years ago

wqz-leo commented 5 years ago

This PR will add ability to encode objects which are assigned to dictionary value. For example, both Event and Item are generated by SwagGen:

struct Event: Codable {
    var detail: [String: Any]?
}

struct Item: Codable {
    var id: String
    var title: String
}

let item = Item(id: "123", title: "Game of Thrones")
let event = Event(detail: ["item": item])

Then calling JSONEncoder().encode(event) will throw invalidValue error.

To get above example work, the Event can only be constructed like this:

let item: [String: Any] = ["id": "123", "title": "Game of Thrones"]
let event = Event(detail: item)
yonaskolb commented 5 years ago

Thanks Leo! Sorry for the delay. Gonna be working on this repo again, as I'm updating it to swagger 3.0. I'll do a final release with this in it