Skip to content

[FrameworkBundle] Fix config for array of base_uri in http_client #53131

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

Open
wants to merge 6 commits into
base: 6.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@
return $v;
})
->end()
->children()

Check failure on line 1041 in src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php:1041:15: UndefinedMethod: Method Symfony\Component\Config\Definition\Builder\NodeDefinition::children does not exist (see https://psalm.dev/022)
->arrayNode('validation')
->beforeNormalization()
->ifTrue(fn ($v) => isset($v['enable_annotations']))
Expand Down Expand Up @@ -1347,7 +1347,7 @@
return $v;
})
->end()
->children()

Check failure on line 1350 in src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php:1350:15: UndefinedMethod: Method Symfony\Component\Config\Definition\Builder\NodeDefinition::children does not exist (see https://psalm.dev/022)
->arrayNode('php_errors')
->info('PHP errors handling configuration')
->addDefaultsIfNotSet()
Expand Down Expand Up @@ -1979,13 +1979,23 @@
->ifTrue(fn ($v) => !empty($v['query']) && !isset($v['base_uri']))
->thenInvalid('"query" applies to "base_uri" but no base URI is defined.')
->end()
->validate()
->ifTrue(fn ($v) => (
(isset($v['base_uri']) && \is_array($v['base_uri']))
&& (
(!isset($v['retry_failed']) || false === $v['retry_failed']['enabled'])
|| \count($v['base_uri']) !== \count(array_filter($v['base_uri'], 'is_string'))
)
))
->thenInvalid('"base_uri" can only be an array if "retry_failed" is defined.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defined or enabled for retry_failed?
also, should we validate the array contains only strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say that more precautions are always better, so indeed checking if there are strings only!
And yeah, retries should be enabled, imo.

Going to make changes in that direction.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this code we're merging all validations but the error message is the same for all. If you define an array and one of the values is no a string by mistake, you'll see this and will be confusing:

"base_uri" can only be an array if "retry_failed" is defined

Maybe we could extract this validation and create a dedicated error message for it?

\count($v['base_uri']) !== \count(array_filter($v['base_uri'], 'is_string'))

Thanks!

->end()
->children()
->scalarNode('scope')
->info('The regular expression that the request URL must match before adding the other options. When none is provided, the base URI is used instead.')
->cannotBeEmpty()
->end()
->scalarNode('base_uri')
->info('The URI to resolve relative URLs, following rules in RFC 3985, section 2.')
->variableNode('base_uri')
->info('The URI(s) to resolve relative URLs, following rules in RFC 3985, section 2.')
->cannotBeEmpty()
->end()
->scalarNode('auth_basic')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2546,12 +2546,12 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
unset($scopeConfig['retry_failed']);

if (null === $scope) {
$baseUri = $scopeConfig['base_uri'];
unset($scopeConfig['base_uri']);
$baseUri = \is_array($scopeConfig['base_uri']) ? $scopeConfig['base_uri'][0] : $scopeConfig['base_uri'];
$config = array_filter($scopeConfig, fn ($k) => 'base_uri' !== $k, \ARRAY_FILTER_USE_KEY);

$container->register($name, ScopingHttpClient::class)
->setFactory([ScopingHttpClient::class, 'forBaseUri'])
->setArguments([new Reference('http_client.transport'), $baseUri, $scopeConfig])
->setArguments([new Reference('http_client.transport'), $baseUri, $config])
->addTag('http_client.client')
;
} else {
Expand All @@ -2562,7 +2562,12 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
}

if ($this->readConfigEnabled('http_client.scoped_clients.'.$name.'.retry_failed', $container, $retryOptions)) {
$this->registerRetryableHttpClient($retryOptions, $name, $container);
$baseUris = [];
if (isset($scopeConfig['base_uri']) && \is_array($scopeConfig['base_uri'])) {
$baseUris = $scopeConfig['base_uri'];
unset($scopeConfig['base_uri']);
}
$this->registerRetryableHttpClient($retryOptions, $name, $container, $baseUris);
}

$container
Expand Down Expand Up @@ -2598,7 +2603,7 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
}
}

private function registerRetryableHttpClient(array $options, string $name, ContainerBuilder $container): void
private function registerRetryableHttpClient(array $options, string $name, ContainerBuilder $container, array $baseUris = []): void
{
if (null !== $options['retry_strategy']) {
$retryStrategy = new Reference($options['retry_strategy']);
Expand Down Expand Up @@ -2628,7 +2633,8 @@ private function registerRetryableHttpClient(array $options, string $name, Conta
->register($name.'.retryable', RetryableHttpClient::class)
->setDecoratedService($name, null, 10) // higher priority than TraceableHttpClient (5)
->setArguments([new Reference($name.'.retryable.inner'), $retryStrategy, $options['max_retries'], new Reference('logger')])
->addTag('monolog.logger', ['channel' => 'http_client']);
->addTag('monolog.logger', ['channel' => 'http_client'])
->addMethodCall('withOptions', [['base_uri' => $baseUris]], true);
}

private function registerMailerConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader, bool $webhookEnabled): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@
<xsd:element name="resolve" type="http_resolve" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="header" type="http_header" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="peer-fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="base-uri" type="http_client_base_uri" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="retry-failed" type="http_client_retry_failed" minOccurs="0" maxOccurs="1" />
<xsd:element name="extra" type="xsd:anyType" minOccurs="0" maxOccurs="unbounded" />
</xsd:choice>
Expand All @@ -694,6 +695,7 @@
<xsd:element name="resolve" type="http_resolve" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="header" type="http_header" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="peer-fingerprint" type="fingerprint" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="base-uri" type="http_client_base_uri" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="retry-failed" type="http_client_retry_failed" minOccurs="0" maxOccurs="1" />
<xsd:element name="extra" type="xsd:anyType" minOccurs="0" maxOccurs="unbounded" />
</xsd:choice>
Expand Down Expand Up @@ -747,6 +749,10 @@
<xsd:attribute name="code" type="xsd:integer" />
</xsd:complexType>

<xsd:complexType name="http_client_base_uri" mixed="true">
<xsd:attribute name="key" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="http_query" mixed="true">
<xsd:attribute name="key" type="xsd:string" />
</xsd:complexType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
'base_uri' => 'http://example.com',
'retry_failed' => ['multiplier' => 4],
],
'bar' => [
'base_uri' => ['http://a.example.com', 'http://b.example.com'],
'retry_failed' => ['max_retries' => 4],
],
],
],
]);
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
<framework:scoped-client name="foo" base-uri="http://example.com">
<framework:retry-failed multiplier="4"/>
</framework:scoped-client>
<framework:scoped-client name="bar">
<framework:base-uri>http://a.example.com</framework:base-uri>
<framework:base-uri>http://a.example.com</framework:base-uri>
<framework:retry-failed max-retries="3"/>
</framework:scoped-client>
</framework:http-client>
</framework:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ framework:
base_uri: http://example.com
retry_failed:
multiplier: 4
bar:
base_uri:
- http://a.example.com
- http://b.example.com
retry_failed:
max_retries: 3
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpClient/RetryableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ class RetryableHttpClient implements HttpClientInterface, ResetInterface
/**
* @param int $maxRetries The maximum number of times to retry
*/
public function __construct(HttpClientInterface $client, ?RetryStrategyInterface $strategy = null, int $maxRetries = 3, ?LoggerInterface $logger = null)
public function __construct(HttpClientInterface $client, ?RetryStrategyInterface $strategy = null, int $maxRetries = 3, ?LoggerInterface $logger = null, array $baseUris = [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is not needed nor used

{
$this->client = $client;
$this->strategy = $strategy ?? new GenericRetryStrategy();
$this->maxRetries = $maxRetries;
$this->logger = $logger;
$this->baseUris = $baseUris;
}

public function withOptions(array $options): static
Expand Down
Loading