mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-22 17:40:55 +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") } }
21 lines
397 B
Plaintext
21 lines
397 B
Plaintext
PODS:
|
|
- Alamofire (4.5.1)
|
|
- TestClient (0.0.1):
|
|
- Alamofire (~> 4.5.0)
|
|
|
|
DEPENDENCIES:
|
|
- Alamofire (~> 4.5)
|
|
- TestClient (from `../`)
|
|
|
|
EXTERNAL SOURCES:
|
|
TestClient:
|
|
:path: ../
|
|
|
|
SPEC CHECKSUMS:
|
|
Alamofire: 2d95912bf4c34f164fdfc335872e8c312acaea4a
|
|
TestClient: c12a94972a1128167637d39ea0b072f46c0790de
|
|
|
|
PODFILE CHECKSUM: a4351ac5e001fd96f35ed7e647981803864100b5
|
|
|
|
COCOAPODS: 1.1.1
|