Skip to content

Commit 7c6ed51

Browse files
committed
bug #718 [Platform] Support boolean string conversion in AbstractModelCatalog::parseModelName() (sonnymilton)
This PR was squashed before being merged into the main branch. Discussion ---------- [Platform] Support boolean string conversion in `AbstractModelCatalog::parseModelName()` | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Docs? | no <!-- required for new features --> | Issues | Fix #713 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT This PR extends the query parameter normalization logic in `AbstractModelCatalog` to handle boolean-like strings (`"true"` / `"false"`) in addition to numeric strings: - Query parameters such as `?think=true` or `?think=false` are now correctly converted to PHP booleans instead of remaining plain strings. - Conversion is applied recursively, so nested arrays produced by `parse_str()` also normalize their boolean values. - Numeric string handling is preserved as before. ### Tests New test cases were added to ensure: - Boolean string parameters are converted properly (`true`/`false`). - Recursive conversion inside nested query arrays works as expected. - Mixed parameters with numbers, booleans, and plain strings remain consistent. Commits ------- f57c279 [Platform] Support boolean string conversion in `AbstractModelCatalog::parseModelName()`
2 parents e21e99f + f57c279 commit 7c6ed51

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/platform/src/ModelCatalog/AbstractModelCatalog.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function parseModelName(string $modelName): array
8686

8787
parse_str($queryString, $options);
8888

89-
$options = self::convertNumericStrings($options);
89+
$options = self::convertScalarStrings($options);
9090
}
9191

9292
// Determine catalog key: try exact match first, then fall back to base model
@@ -110,13 +110,17 @@ protected function parseModelName(string $modelName): array
110110
*
111111
* @param array<string, mixed> $data The array to process
112112
*
113-
* @return array<string, mixed> The array with numeric strings converted to appropriate numeric types
113+
* @return array<string, mixed> The array with numeric and boolean-like strings converted to appropriate numeric/boolean types
114114
*/
115-
private static function convertNumericStrings(array $data): array
115+
private static function convertScalarStrings(array $data): array
116116
{
117117
foreach ($data as $key => $value) {
118118
if (\is_array($value)) {
119-
$data[$key] = self::convertNumericStrings($value);
119+
$data[$key] = self::convertScalarStrings($value);
120+
} elseif ('true' === $value) {
121+
$data[$key] = true;
122+
} elseif ('false' === $value) {
123+
$data[$key] = false;
120124
} elseif (is_numeric($value) && \is_string($value)) {
121125
// Convert to int if it's a whole number, otherwise to float
122126
$data[$key] = str_contains($value, '.') ? (float) $value : (int) $value;

src/platform/tests/ModelCatalog/AbstractModelCatalogTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ public function testGetModelWithIntegerQueryParameter()
4949
$this->assertSame(500, $options['max_tokens']);
5050
}
5151

52+
public function testGetModelWithBooleanQueryParameters()
53+
{
54+
$catalog = $this->createTestCatalog();
55+
$model = $catalog->getModel('test-model?think=true&stream=false');
56+
57+
$this->assertSame('test-model', $model->getName());
58+
$options = $model->getOptions();
59+
$this->assertArrayHasKey('think', $options);
60+
$this->assertIsBool($options['think']);
61+
$this->assertTrue($options['think']);
62+
$this->assertArrayHasKey('stream', $options);
63+
$this->assertIsBool($options['stream']);
64+
$this->assertFalse($options['stream']);
65+
}
66+
5267
public function testGetModelWithMultipleQueryParameters()
5368
{
5469
$catalog = $this->createTestCatalog();
@@ -66,7 +81,8 @@ public function testGetModelWithMultipleQueryParameters()
6681
$this->assertSame(0.7, $options['temperature']);
6782

6883
$this->assertArrayHasKey('stream', $options);
69-
$this->assertSame('true', $options['stream']);
84+
$this->assertIsBool($options['stream']);
85+
$this->assertTrue($options['stream']);
7086
}
7187

7288
public function testGetModelWithNestedArrayQueryParameters()
@@ -125,6 +141,23 @@ public function testNumericStringsAreConvertedRecursively()
125141
$this->assertIsInt($options['a']['e']);
126142
}
127143

144+
public function testBooleanStringsAreConvertedRecursively()
145+
{
146+
$catalog = $this->createTestCatalog();
147+
$model = $catalog->getModel('test-model?a[b][c]=true&a[b][d]=text&a[e]=false');
148+
149+
$options = $model->getOptions();
150+
151+
$this->assertIsArray($options['a']);
152+
$this->assertIsArray($options['a']['b']);
153+
$this->assertIsBool($options['a']['b']['c']);
154+
$this->assertTrue($options['a']['b']['c']);
155+
$this->assertIsString($options['a']['b']['d']);
156+
$this->assertSame('text', $options['a']['b']['d']);
157+
$this->assertIsBool($options['a']['e']);
158+
$this->assertFalse($options['a']['e']);
159+
}
160+
128161
private function createTestCatalog(): AbstractModelCatalog
129162
{
130163
return new class extends AbstractModelCatalog {

0 commit comments

Comments
 (0)