diff --git a/Sources/SwaggerSwiftCore/Models/Model.swift b/Sources/SwaggerSwiftCore/Models/Model.swift index c58a097..18d4e85 100644 --- a/Sources/SwaggerSwiftCore/Models/Model.swift +++ b/Sources/SwaggerSwiftCore/Models/Model.swift @@ -155,7 +155,7 @@ let container = try decoder.container(keyedBy: StringCodingKey.self) """ return """ -\(accessControl.rawValue) init(from decoder: Decoder) throws { +\(accessControl.rawValue) init(from decoder: Decoder) throws { \(functionBody.indentLines(1)) } """ diff --git a/Tests/SwaggerSwiftCoreTests/SwaggerSwiftCoreTests.swift b/Tests/SwaggerSwiftCoreTests/SwaggerSwiftCoreTests.swift index 78db376..7e9b487 100644 --- a/Tests/SwaggerSwiftCoreTests/SwaggerSwiftCoreTests.swift +++ b/Tests/SwaggerSwiftCoreTests/SwaggerSwiftCoreTests.swift @@ -22,14 +22,19 @@ public struct Test: Codable, Sendable { } public init(from decoder: Decoder) throws { - let container = try decoder.container(keyedBy: CodingKeys.self) + let container = try decoder.container(keyedBy: StringCodingKey.self) // Allows the backend to return badly formatted urls - if let urlString = try container.decodeIfPresent(String.self, forKey: .url) { + if let urlString = try container.decodeIfPresent(String.self, forKey: "url") { self.url = URL(string: urlString) } else { self.url = nil } } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: StringCodingKey.self) + try container.encodeIfPresent(url, forKey: "url") + } } """) } @@ -52,6 +57,16 @@ public struct Test: Codable, Sendable { public init(url: URL) { self.url = url } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: StringCodingKey.self) + self.url = try container.decode(URL.self, forKey: "url") + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: StringCodingKey.self) + try container.encode(url, forKey: "url") + } } """) }