OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.72k stars 6.56k forks source link

[SWIFT4] Value of type XX has no member 'map' #1456

Open Sal0g opened 5 years ago

Sal0g commented 5 years ago
Description

When using using Objc compatible code, the produced code is not valid in Swift 3/4.

Using openapi-generator Version 3.3.2

with the command:

openapi-generator generate \
    -i ./openAPI-3.yaml \
    --config ./api-generation-config.json \
    -g swift4 \

and the config file api-generation-config.json:

{
...
"objcCompatible":"true",
"podswift_version":"4.0"
}

and the yaml:

definitions:
  ModelA:
    required:
      - intA
    properties:
      intA:
        type: integer
        format: int64
      name:
        type: string

produces this code:

public struct ModelA: Codable {

    public var intA: Int64
    public var intANum: NSNumber? {
        get {
            return intA.map({ return NSNumber(value: $0) })
        }
    }
    public var name: String?

    public init(intA: Int64, name: String?) {
        self.intA = intA
        self.name = name
    }
}

resulting in the error: - ERROR | xcodebuild: ModelA.swift: error: value of type 'Int64' has no member 'map'

This occurs for type Int64 and Bool.

wing328 commented 5 years ago

@Sal0g thanks for reporting the issue. Is it correct to say that it works fine with Int32 but not Int64 for Integer type?

Sal0g commented 5 years ago

Yes, it seems that Int32 in the yaml definition is getting translated into Int/Int? whereas Int64 gets translated into Int64 in the generated Swift code.