swagger-api / swagger-api/swagger-codegen
[Swift3] Code generated doesn't even compile on macOS
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Regardless of the Swagger spec, the Swift3 code generated by Swagger Codegen doesn't compile on macOS. The problem is in the generic part of Models.swift, more specifically the implementation of the decode function for objects. func decode<T>(clazz: T.Type, source: AnyObject) -> T.
This is the full code:
static func decode<T>(clazz: T.Type, source: AnyObject) -> T {
initialize()
if T.self is Int32.Type && source is NSNumber {
return source.int32Value as! T; //the problem is with this line
}
if T.self is Int64.Type && source is NSNumber {
return source.int64Value as! T;
}
if T.self is UUID.Type && source is String {
return UUID(uuidString: source as! String) as! T
}
if source is T {
return source as! T
}
if T.self is Data.Type && source is String {
return Data(base64Encoded: source as! String) as! T
}
let key = "\(T.self)"
if let decoder = decoders[key] {
return decoder(source) as! T
} else {
fatalError("Source \(source) is not convertible to type \(clazz): Maybe swagger file is insufficient")
}
}
The compiler gives an error: Ambiguous use of 'int32Value' for the line return source.int32Value as! T due to the fact, that on macOS, int32Value is defined in Foundation.NSNumber and Foundation. NSAppleEventDescriptor as well. On other platforms supporting Swift3 the problem is not present, since Foundation.NSAppleEventDescriptor is not available on other platforms, only on macOS.
Swagger-codegen version
2.2.2
Swagger declaration file content or url
Any Swagger spec causes this error.
Command line used for generation
swagger-codegen generate -i /path/spec.yaml -l swift3
Steps to reproduce
Generate Swift3 code from any Swagger spec and try to compile it on macOS
Related issues
Suggest a Fix
When decoding a number, it needs to be casted into NSNumber and then .int32Value can be called on it. Namely, this line: return source.int32Value as! T; should be replaced by these 2 lines:
let numberValue = source as! NSNumber
return numberValue.int32Value as! T;
The .mustache file used to generate Models.swift should be updated accordingly.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Find the Swift3 .mustache template that generates Models.swift and inspect the generic decode function. Generate a client with swagger-codegen generate -i /path/spec.yaml -l swift3, then compile it on macOS to reproduce the ambiguous int32Value error. Done means generated Swift3 code compiles on macOS for a Swagger spec.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100