Skip to content

Conversation

@x-sheep
Copy link
Contributor

@x-sheep x-sheep commented Nov 3, 2024

Swift 6 mandates types to conform to the Sendable protocol to be used in async contexts. This PR will only add conformance when it can be proved by the compiler, or if a locking mechanism is in place.

@jgavris (2017/07) @ehyche (2017/08) @Edubits (2017/09) @jaz-ah (2017/09) @4brunu (2019/11) @dydus0x14 (2023/06)

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package ./bin/generate-samples.sh ./bin/configs/*.yaml ./bin/utils/export_docs_generators.sh 
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
@4brunu
Copy link
Contributor

4brunu commented Nov 3, 2024

Hi @x-sheep, thanks for creating this PR.
So basically we can add sendable conformance to structs and enums, but not classes, am I right?

@x-sheep
Copy link
Contributor Author

x-sheep commented Nov 3, 2024

A class can only be safely marked Sendable if it's immutable, or if it handles concurrent access itself. While the generated code won't cause data races, it's possible that consumers of this code do. So they might need to add an extension with @unchecked Sendable themselves, and make sure that classes aren't concurrently modified.

I could add a case where Sendable is added to classes if the readonlyProperties config is set to true.

@4brunu
Copy link
Contributor

4brunu commented Nov 3, 2024

That's a good idea.
Could you please add that?
By the way, what's your opinion on the best default value for read only properties?
Currently it's disabled by default, although I think it would be a good idea by default to enable it.
What are your thoughts on this?

@x-sheep
Copy link
Contributor Author

x-sheep commented Nov 3, 2024

In some of my projects, I do extensively mutate the structs retrieved from an external API.

I believe Swift's handling of mutating structs is robust enough. The compiler will still stop possible race conditions, even when the data is marked as Sendable. So I think the current defaults (read-only off, classes off) are suitable for the majority of cases.

@x-sheep
Copy link
Contributor Author

x-sheep commented Nov 4, 2024

I've now added Sendable conformance to classes with read-only properties.

Maybe it's a good idea to add a sample project to the repo using these settings?

@4brunu
Copy link
Contributor

4brunu commented Nov 4, 2024

Maybe it's a good idea to add a sample project to the repo using these settings?

That's a good idea, but right now we have a problem that we have some many samples that sometimes CI failes due to timeout.
Could you add those configurations to some existing samples please? This way we would increate the CI times.
For example, here we test a lot of configurations.
https://github.com/OpenAPITools/openapi-generator/blob/master/bin/configs/swift6-urlsessionLibrary.yaml

@x-sheep
Copy link
Contributor Author

x-sheep commented Nov 4, 2024

I've added readonlyProperties to the urlsessionLibrary sample. The vapor sample already uses classes without readonly.

@4brunu 4brunu merged commit 00dbe59 into OpenAPITools:master Nov 4, 2024
17 checks passed
@4brunu
Copy link
Contributor

4brunu commented Nov 4, 2024

Thanks for the contribution 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants