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.86k stars 6.59k forks source link

[BUG][swift6] Non-sendable types cannot cross actor boundary #20057

Closed lilidotshi closed 1 week ago

lilidotshi commented 1 week ago

Bug Report Checklist

Description

With Swift 6, there's more strict concurrency checks, including sendable objects being sent across actor boundaries. This is a fairly basic view model for async calls that feed up to a SwiftUI View.

@MainActor
final class ViewModel: ObservableObject {
    @Published var title: String

    func fetchTitle() {
        Task {
            let response = await FooAPI.fetchTitle()
            self.title = response.title
        }
}

The OpenAPI generated model looks something like this:

public struct TitleResponse: Codable, JSONEncodable, Hashable {
    public let title: String
    // all the other openapi generated functions
}

In Swift 5, let response = await FooAPI.fetchTitle() generates an warning Non-sendable type 'TitleResponse' returned by implicitly asynchronous call to nonisolated function cannot cross actor boundary; this is an error in the Swift 6 language mode

Since the structs generated by OpenAPI are public structs, they don't get the implicit conformance to Sendable, which leaves us with that error. I am curious what your recommendation is moving forward for Swift 6, if the objects should conform by default to Sendable or not.

openapi-generator version

7.9.0

4brunu commented 1 week ago

Hi, this should be fixed, by those two PR https://github.com/OpenAPITools/openapi-generator/pull/20013 https://github.com/OpenAPITools/openapi-generator/pull/20023, in the swift 6 generator when the openapi-generator 7.10.0 is released. Until then, you can build openapi-generator from source, which will create a jar, and use the jar directly. https://github.com/OpenAPITools/openapi-generator?tab=readme-ov-file#14---build-projects

lilidotshi commented 1 week ago

Hi, this should be fixed, by those two PR #20013 #20023, in the swift 6 generator when the openapi-generator 7.10.0 is released. Until then, you can build openapi-generator from source, which will create a jar, and use the jar directly. https://github.com/OpenAPITools/openapi-generator?tab=readme-ov-file#14---build-projects

Ah thank you!