openapi-generator
openapi-generator copied to clipboard
[BUG] [Swift] Generating/Calling NSDecimalString function crashes the Swift compiler
Description
NSDecimalString generation causing a complier error after xCode beta 5 and 6.
The following code that calls NSDecimalString crashes the compiler with the message:
SILFunction type mismatch for 'NSDecimalString': '$@convention(c) (UnsafePointer, Optional) -> @autoreleased Optional' != '$@convention(c) (UnsafePointer, Optional) -> @autoreleased NSString'
https://github.com/swiftlang/swift/issues/75752
See the issue in swift language repo. If the new xCode 16 stays that way, openapi should change is generation. There is a workaround on the issue which works. It is currently a problem regardless of the beta, since developers have to adapt the new xCode beta to prepare for iOS 18 which is coming out soon.
openapi-generator version
7.8
OpenAPI declaration file content or url
Generation Details
openapi-generator generate
-i etc/xxxyaml
-g swift5
-o Sources/Packages/XXX
--global-property models,supportingFiles,modelDocs=false
--model-name-suffix=DTO
--additional-properties=projectName= XXX,swiftPackagePath=Sources,removeMigrationProjectNameClass=true
Steps to reproduce
Related issues/PRs
Suggest a fix
Problematic generation:
extension KeyedEncodingContainerProtocol { public 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) } }
Workaround:
extension KeyedEncodingContainerProtocol { public mutating func encode(_ value: Decimal, forKey key: Self.Key) throws { let decimalNumber = NSDecimalNumber(decimal: value) let numberFormatter = NumberFormatter() numberFormatter.numberStyle = .decimal numberFormatter.locale = Locale(identifier: "en_US") let formattedString = numberFormatter.string(from: decimalNumber) ?? "\(value)" try encode(formattedString, forKey: key) } }