From 06960aa5ea613ada50c260f6144a0c4aea884c79 Mon Sep 17 00:00:00 2001 From: Peter Ringelmann Date: Mon, 16 Nov 2026 13:19:17 +0100 Subject: [PATCH] fix(settings): store declarative setting values as strings Numbers from switches and number fields caused a 500 with internal storage. Lists are stored as JSON, like the frontend already sends them. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Peter Ringelmann --- .../DeclarativeSettingsController.php | 2 ++ lib/private/Settings/DeclarativeManager.php | 1 + tests/lib/Settings/DeclarativeManagerTest.php | 23 +++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/apps/settings/lib/Controller/DeclarativeSettingsController.php b/apps/settings/lib/Controller/DeclarativeSettingsController.php index 4e4bee4043ca0..3680910451f99 100644 --- a/apps/settings/lib/Controller/DeclarativeSettingsController.php +++ b/apps/settings/lib/Controller/DeclarativeSettingsController.php @@ -49,6 +49,7 @@ public function __construct( * @throws NotLoggedInException Not logged in or not an admin user * @throws NotAdminException Not logged in or not an admin user * @throws OCSBadRequestException Invalid arguments to save value + * @NoSubAdminRequired * * 200: Value set successfully */ @@ -69,6 +70,7 @@ public function setValue(string $app, string $formId, string $fieldId, mixed $va * @throws NotLoggedInException Not logged in or not an admin user * @throws NotAdminException Not logged in or not an admin user * @throws OCSBadRequestException Invalid arguments to save value + * @NoSubAdminRequired * * 200: Value set successfully */ diff --git a/lib/private/Settings/DeclarativeManager.php b/lib/private/Settings/DeclarativeManager.php index cc9f479597636..855eee19f369c 100644 --- a/lib/private/Settings/DeclarativeManager.php +++ b/lib/private/Settings/DeclarativeManager.php @@ -324,6 +324,7 @@ private function getInternalValue(IUser $user, string $app, string $formId, stri } private function saveInternalValue(IUser $user, string $app, string $formId, string $fieldId, mixed $value): void { + $value = is_array($value) ? json_encode($value, JSON_THROW_ON_ERROR) : (string)$value; $sectionType = $this->getSectionType($app, $fieldId); $field = $this->getSchemaField($app, $formId, $fieldId); diff --git a/tests/lib/Settings/DeclarativeManagerTest.php b/tests/lib/Settings/DeclarativeManagerTest.php index d3ff46818b72a..56aab8f0d2f2a 100644 --- a/tests/lib/Settings/DeclarativeManagerTest.php +++ b/tests/lib/Settings/DeclarativeManagerTest.php @@ -24,6 +24,7 @@ use OCP\Settings\IDeclarativeManager; use OCP\Settings\IDeclarativeSettingsForm; use OCP\Settings\IDeclarativeSettingsFormWithHandlers; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -528,6 +529,28 @@ public function testSetInternalValue(): void { $this->assertEquals('120m', $someRealSettingField['value']); } + public static function dataSetInternalValue(): array { + return [ + 'switch' => [1, '1'], + 'float' => [1.5, '1.5'], + 'bool' => [true, '1'], + 'list' => [['foo', 'bar'], '["foo","bar"]'], + ]; + } + + #[DataProvider('dataSetInternalValue')] + public function testSetInternalValueIsStoredAsString(mixed $value, string $stored): void { + $app = 'testing'; + $schema = self::validSchemaAllFields; + $this->declarativeManager->registerSchema($app, $schema); + + $this->appConfig->expects($this->once()) + ->method('setValueString') + ->with($app, 'test_field_4', $stored); + + $this->declarativeManager->setValue($this->adminUser, $app, $schema['id'], 'test_field_4', $value); + } + public function testSetExternalValue(): void { $app = 'testing'; $schema = self::validSchemaAllFields;