Skip to content

Commit 503f652

Browse files
committed
bug #1691 Fix scientific notation strings being cast to int instead of float in model option parsing (fabpot)
This PR was merged into the main branch. Discussion ---------- Fix scientific notation strings being cast to int instead of float in model option parsing | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Docs? | no <!-- required for new features --> | Issues | n/a | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Update/add documentation as required (we can help!) - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- accb903 Fix scientific notation strings being cast to int instead of float in model option parsing
2 parents 62a0c07 + accb903 commit 503f652

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/platform/src/ModelCatalog/AbstractModelCatalog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private static function convertScalarStrings(array $data): array
123123
$data[$key] = false;
124124
} elseif (is_numeric($value) && \is_string($value)) {
125125
// Convert to int if it's a whole number, otherwise to float
126-
$data[$key] = str_contains($value, '.') ? (float) $value : (int) $value;
126+
$data[$key] = str_contains($value, '.') || str_contains($value, 'e') || str_contains($value, 'E') ? (float) $value : (int) $value;
127127
}
128128
}
129129

src/platform/tests/ModelCatalog/AbstractModelCatalogTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ public function testBooleanStringsAreConvertedRecursively()
158158
$this->assertFalse($options['a']['e']);
159159
}
160160

161+
public function testScientificNotationIsConvertedToFloat()
162+
{
163+
$catalog = $this->createTestCatalog();
164+
$model = $catalog->getModel('test-model?frequency_penalty=1e-5&presence_penalty=2.5E3');
165+
166+
$options = $model->getOptions();
167+
168+
$this->assertIsFloat($options['frequency_penalty']);
169+
$this->assertSame(1e-5, $options['frequency_penalty']);
170+
$this->assertIsFloat($options['presence_penalty']);
171+
$this->assertSame(2.5E3, $options['presence_penalty']);
172+
}
173+
161174
private function createTestCatalog(): AbstractModelCatalog
162175
{
163176
return new class extends AbstractModelCatalog {

0 commit comments

Comments
 (0)