Skip to content

Commit 82afbe3

Browse files
Merge #370
370: Use VariableNode on "settings" configuration r=norkunas a=pan93412 # Pull Request ## Related issue Fixes #369 ## What does this PR do? "arrayNode" does not support arbitrary configuration keys. Therefore, I switch to variableNode with dynamic check. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Yi-Jyun Pan <[email protected]>
2 parents a02a467 + acf34e4 commit 82afbe3

File tree

2 files changed

+88
-5
lines changed

2 files changed

+88
-5
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Meilisearch\Bundle\Searchable;
88
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
99
use Symfony\Component\Config\Definition\ConfigurationInterface;
10+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1011

1112
final class Configuration implements ConfigurationInterface
1213
{
@@ -62,11 +63,20 @@ public function getConfigTreeBuilder(): TreeBuilder
6263
->info('Property accessor path (like method or property name) used to decide if an entry should be indexed.')
6364
->defaultNull()
6465
->end()
65-
->arrayNode('settings')
66+
->variableNode('settings')
67+
->defaultValue([])
6668
->info('Configure indices settings, see: https://www.meilisearch.com/docs/reference/api/settings')
6769
->beforeNormalization()
6870
->always()
69-
->then(static function (array $value) {
71+
->then(static function ($value) {
72+
if (null === $value) {
73+
return [];
74+
}
75+
76+
if (!\is_array($value)) {
77+
throw new InvalidConfigurationException('Settings must be an array.');
78+
}
79+
7080
$stringSettings = ['distinctAttribute', 'proximityPrecision', 'searchCutoffMs'];
7181

7282
foreach ($stringSettings as $setting) {
@@ -77,9 +87,7 @@ public function getConfigTreeBuilder(): TreeBuilder
7787

7888
return $value;
7989
})
80-
->end()
81-
->arrayPrototype()
82-
->variablePrototype()->end()
90+
->end()
8391
->end()
8492
->end()
8593
->end()

tests/Unit/ConfigurationTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,46 @@ public function testValidConfig(array $inputConfig, array $expectedConfig): void
2323
$this->assertProcessedConfigurationEquals($inputConfig, $expectedConfig);
2424
}
2525

26+
/**
27+
* @param mixed $value
28+
*
29+
* @dataProvider dataTestSettingsDynamicCheckerInvalid
30+
*/
31+
public function testSettingsDynamicCheckerInvalid($value): void
32+
{
33+
$this->assertConfigurationIsInvalid([
34+
'meilisearch' => [
35+
'indices' => [
36+
[
37+
'name' => 'items',
38+
'class' => 'App\Entity\Post',
39+
'settings' => $value,
40+
],
41+
],
42+
],
43+
], 'Settings must be an array.');
44+
}
45+
46+
/**
47+
* @param mixed $value
48+
*
49+
* @dataProvider dataTestSettingsDynamicCheckerValid
50+
*/
51+
public function testSettingsDynamicCheckerValid($value): void
52+
{
53+
$this->assertConfigurationIsValid([
54+
'meilisearch' => [
55+
'indices' => [
56+
[
57+
'name' => 'items',
58+
'class' => 'App\Entity\Post',
59+
'settings' => $value,
60+
],
61+
],
62+
],
63+
]);
64+
}
65+
2666
/**
2767
* @return iterable<array{inputConfig: array<mixed>, expectedConfig: array<mixed>}>
2868
*/
@@ -294,6 +334,41 @@ public static function dataTestConfigurationTree(): iterable
294334
];
295335
}
296336

337+
/**
338+
* @return iterable<array{value: mixed}>
339+
*/
340+
public static function dataTestSettingsDynamicCheckerInvalid(): iterable
341+
{
342+
yield 'string is not acceptable' => [
343+
'value' => 'hello',
344+
];
345+
yield 'int is not acceptable' => [
346+
'value' => 1,
347+
];
348+
yield 'bool is not acceptable' => [
349+
'value' => true,
350+
];
351+
}
352+
353+
/**
354+
* @return iterable<array{value: mixed}>
355+
*/
356+
public static function dataTestSettingsDynamicCheckerValid(): iterable
357+
{
358+
yield 'array is acceptable' => [
359+
'value' => [],
360+
];
361+
yield 'array with arbitrary key is acceptable' => [
362+
'value' => [
363+
'key' => 'value',
364+
'key2' => 'value2',
365+
],
366+
];
367+
yield 'null is acceptable' => [
368+
'value' => null,
369+
];
370+
}
371+
297372
protected function getConfiguration(): Configuration
298373
{
299374
return new Configuration();

0 commit comments

Comments
 (0)