Skip to content

Commit

Permalink
[swift5] fixes bug OpenAPITools#13410
Browse files Browse the repository at this point in the history
  • Loading branch information
Feuerwerk committed Oct 4, 2022
1 parent 085e1e5 commit cb5aeb9
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ extension KeyedEncodingContainerProtocol {
}
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} mutating func encode(_ value: Decimal, forKey key: Self.Key) throws {
var mutableValue = value
let stringValue = NSDecimalString(&mutableValue, Locale(identifier: "en_US"))
try encode(stringValue, forKey: key)
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} mutating func encodeIfPresent(_ value: Decimal?, forKey key: Self.Key) throws {
if let value = value {
try encode(value, forKey: key)
}
}
}

extension KeyedDecodingContainerProtocol {
Expand Down Expand Up @@ -186,6 +197,28 @@ extension KeyedDecodingContainerProtocol {
return map
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func decode(_ type: Decimal.Type, forKey key: Self.Key) throws -> Decimal {
let stringValue = try decode(String.self, forKey: key)
guard let decimalValue = Decimal(string: stringValue) else {
let context = DecodingError.Context(codingPath: [key], debugDescription: "The key \(key) couldn't be converted to a Decimal value")
throw DecodingError.typeMismatch(type, context)
}

return decimalValue
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func decodeIfPresent(_ type: Decimal.Type, forKey key: Self.Key) throws -> Decimal? {
guard let stringValue = try decodeIfPresent(String.self, forKey: key) else {
return nil
}
guard let decimalValue = Decimal(string: stringValue) else {
let context = DecodingError.Context(codingPath: [key], debugDescription: "The key \(key) couldn't be converted to a Decimal value")
throw DecodingError.typeMismatch(type, context)
}

return decimalValue
}

}{{/generateModelAdditionalProperties}}{{^useVapor}}

extension HTTPURLResponse {
Expand Down

0 comments on commit cb5aeb9

Please sign in to comment.