-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[PHP] By default should use static Configuration class instance #19775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…new instance is created every time
|
https://github.com/OpenAPITools/openapi-generator/actions/runs/11168507264/job/31063629502?pr=19775 please update the sample when you've time |
|
@wing328 could you please do this for me? |
|
I'm OK with this change, using static methods and properties is a code smell in 99% of the cases, but here the assumption is you should get the (supposedly pre-configured) default config instead of just an empty config object. Not because of performance but because of the behavior, the solution is still the same. 👍 |
Yes exactly. Normally because Configuration is static object, then it's meant to be pre-configured and then can be automatically be reused in all API calls done later in code. |
|
no worry i'll update the sample over the weekend and get this PR merged have a nice weekend |
|
tested updated samples locally and the result is good will update the samples after merging this PR into master thanks again for the PR |
|
Just my 2 cts to this closed issue: This change of behavior is a severe BC break! In a dynamic environment (with underlying caching of API instances), I explicitly relied on receiving a new config instance every time: $apiClass = 'Some/Dynamically/Determined/GeneratedApi';
$api = new ($apiClass)(/* no configuration passed */);
// Fetch correct config instance from $api
// - don't want to do the 'Some/Dynamically/Determined/...' stuff for configuration again
$apiConfig = $api->getConfig();
$apiConfig->setHost($specificHost);
return $api->callDaStuff(); // API call uses $specificHostWith this PR in place, changing $gugl = new FinderApi();
$gugl->getConfig()->setHost('https://www.gugl.com');
$guglEaster = $gugle->find('easteregg'); // "GUGL's easteregg
$yehu = new FinderApi();
$yehu->getConfig()->setHost('https://www.yehu.com');
$yehuEaster = $yehu->find('easteregg'); // "YEHU's easteregg
// Now, use $gugl again:
$guglXmas = $gugle->find('santa'); // "YEHU's santa (!!!!!!)Lazy workaround: Fix: API class ( --- $this->config = $config ?: Configuration::getDefaultConfiguration();
+++ $this->config = $config ?: clone Configuration::getDefaultConfiguration();However, not sure if Any opinions on this? @wing328 |
For PHP.
By default should use static Configuration class instance, otherwise new instance is created every time you make an API call.
Before this PR every time you create new API object and do not pass Configuration object as 2nd argument to API class constructor it creates new Configuration instance. This is wrong, because Configuration class is meant to be Singleton - so
Configuration::getDefaultConfiguration()should be used instead.Please review: @jebentier @dkarlovi @mandrean @jfastnacht @ybelenko @renepardon