mirror of
				https://github.com/OpenAPITools/openapi-generator.git
				synced 2025-11-04 02:33:54 +00:00 
			
		
		
		
	Previously, we had implemented the Codable protocol by simply claiming conformance, and making sure that each of our internal classes also implemented the Codable protocol. So our model classes looked like:
class MyModel: Codable {
   var propInt: Int
   var propString: String
}
class MyOtherModel: Codable {
   var propModel: MyModel
}
Previously, our additionalProperties implementation would have meant an object schema with an additionalProperties of Int type would have looked like:
class MyModelWithAdditionalProperties: Codable {
   var additionalProperties: [String: Int]
}
But the default implementation of Codable would have serialized MyModelWithAdditionalProperties like this:
{
  "additionalProperties": {
    "myInt1": 1,
    "myInt2": 2,
    "myInt3": 3
  }
}
The default implementation would put the additionalProperties in its own dictionary (which would be incorrect), as opposed to the desired serialization of:
{
  "myInt1": 1,
  "myInt2": 2,
  "myInt3": 3
}
So therefore, the only way to support this was to do our own implementation of the Codable protocol. The Codable protocol is actually two protocols: Encodable and Decodable.
So therefore, this change generates implementations of Encodable and Decodable for each generated model class. So the new generated classes look like:
class MyModel: Codable {
   var propInt: Int
   var propString: String
   // Encodable protocol methods
   public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: String.self)
        try container.encode(propInt, forKey: "propInt")
        try container.encode(propString, forKey: "propString")
    }
    // Decodable protocol methods
    public required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: String.self)
        propInt = try container.decode(Int.self, forKey: "propInt")
        propString = try container.decode(String.self, forKey: "propString")
    }
}
class MyOtherModel: Codable {
   var propModel: MyModel
   // Encodable protocol methods
   public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: String.self)
        try container.encode(propModel, forKey: "propModel")
    }
    // Decodable protocol methods
    public required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: String.self)
        propModel = try container.decode(MyModel.self, forKey: "propModel")
    }
}