[swift] refactor global fields to local fields in API classes#20082
[swift] refactor global fields to local fields in API classes#20082kalinjul wants to merge 2 commits intoOpenAPITools:masterfrom
Conversation
5c15fd8 to
574acc9
Compare
055e157 to
5620a94
Compare
|
Hi @kalinjul. |
|
Sorry to perform thread/issue necromancy here but I believe, from the Swift 6 generated code I’ve seen, that this is still a problem. These properties need to be protected by some synchronisation mechanism – either making the |
|
@rhysm94 would you be willing to submit a PR? I can help with the review |
|
Hi @rhysm94 I have been thinking about your comment, at the time I didn't had much time to respond, but I think I can add more detail. |
Hi @4brunu! Sorry, also not had much time to respond. The extra important thing would be to expose the fields or the entire state as a |
|
I should say - I'm happy to look at raising a PR into this soon. |
|
I raised the minimum iOS version to iOS 13 so that we can use the swift concurrent features without availability checks. I think we could raise it even more, to something like iOS 15, but I'm not sure that we would gain something with that.
Yes, I agree we should create something locally in the generated code.
Could you please expand on this?
Thanks for your help. |
Yeah, understandable. I'll double check the licensing for
Yeah, so I'll link to the LockIsolated documentation that explains this particularly well, but the gist is this: If I have field I could write code that does the following: let previousCount = counter
counter = previousCount + 1then we have the risk of a data race, between these two lines of code. After I capture the previous value of The way around this is to make sure access and mutation happens transactionally, using a counter.withValue { $0 += 1 }This The documentation for ConcurrencyExtras has a really good explanation for this problem, any why using the
I'll attempt this, maybe if we can use deprecated properties or something? Not sure. We'll try and work something out, even if it's a clear migration path to the new pattern! |
Copy the Apple's Mutex is a good ideia, because it will be easy to migrate in the future, when we can use Apple's Mutex.
I see, it's similar to NSLock. Looks good to me.
Thanks |
Generated Swift code used global static variables for important state like
customHeadersandrequestBuilderFactory(see APIs.mustache).This likely introduces race conditions if one needs to set a custom requestBuilderFactory or header for a single request, as it is not guaranteed that no other Thread/Task re-sets this global var concurrently.
This PR adds local fields in generated Api classes that are initialized with the existing static fields by default.
Api calls are no longer class funcs.
You can now create two instances of an API using different RequestBuilders, headers, and so on. In other platforms such as kotlin or java, this was always possible using different ApiClients.
For backwards compatibility, you may just use the constructor with it's default values, which pulls in the static values just as before.
fixes #17377
TODO: This currently only adresses swift5, swift6 in progress.
PR checklist
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.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)