Merge pull request #54335 from nextcloud/feat/noid/lexicon-appconfig-controller

feat(lexicon): get value type from lexicon
pull/54579/head
Maxence Lange 2025-08-22 12:27:48 +07:00 committed by GitHub
commit 2975a99848
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 9 deletions

@ -10,12 +10,14 @@ namespace OCA\Provisioning_API\Controller;
use OC\AppConfig;
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
use OC\Config\ConfigManager;
use OCP\App\IAppManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\Config\ValueType;
use OCP\Exceptions\AppConfigUnknownKeyException;
use OCP\IAppConfig;
use OCP\IGroupManager;
@ -37,6 +39,7 @@ class AppConfigController extends OCSController {
private IGroupManager $groupManager,
private IManager $settingManager,
private IAppManager $appManager,
private readonly ConfigManager $configManager,
) {
parent::__construct($appName, $request);
}
@ -130,19 +133,27 @@ class AppConfigController extends OCSController {
}
$type = null;
try {
$configDetails = $this->appConfig->getDetails($app, $key);
$type = $configDetails['type'];
} catch (AppConfigUnknownKeyException) {
// checking expected type from lexicon
$keyDetails = $this->appConfig->getKeyDetails($app, $key);
if (array_key_exists('valueType', $keyDetails)) {
$type = $keyDetails['valueType'];
} else {
// if no details from lexicon, get from eventual current value in database
try {
$configDetails = $this->appConfig->getDetails($app, $key);
$type = $configDetails['type'];
} catch (AppConfigUnknownKeyException) {
}
}
/** @psalm-suppress InternalMethod */
match ($type) {
IAppConfig::VALUE_BOOL => $this->appConfig->setValueBool($app, $key, (bool)$value),
IAppConfig::VALUE_FLOAT => $this->appConfig->setValueFloat($app, $key, (float)$value),
IAppConfig::VALUE_INT => $this->appConfig->setValueInt($app, $key, (int)$value),
IAppConfig::VALUE_STRING => $this->appConfig->setValueString($app, $key, $value),
IAppConfig::VALUE_ARRAY => $this->appConfig->setValueArray($app, $key, \json_decode($value, true)),
IAppConfig::VALUE_BOOL, ValueType::BOOL => $this->appConfig->setValueBool($app, $key, $this->configManager->convertToBool($value)),
IAppConfig::VALUE_FLOAT, ValueType::FLOAT => $this->appConfig->setValueFloat($app, $key, $this->configManager->convertToFloat($value)),
IAppConfig::VALUE_INT, ValueType::INT => $this->appConfig->setValueInt($app, $key, $this->configManager->convertToInt($value)),
IAppConfig::VALUE_STRING, ValueType::STRING => $this->appConfig->setValueString($app, $key, $value),
IAppConfig::VALUE_ARRAY, ValueType::ARRAY => $this->appConfig->setValueArray($app, $key, $this->configManager->convertToArray($value)),
default => $this->appConfig->setValueMixed($app, $key, $value),
};

@ -8,6 +8,7 @@ declare(strict_types=1);
namespace OCA\Provisioning_API\Tests\Controller;
use OC\AppConfig;
use OC\Config\ConfigManager;
use OCA\Provisioning_API\Controller\AppConfigController;
use OCP\App\IAppManager;
use OCP\AppFramework\Http;
@ -38,6 +39,7 @@ class AppConfigControllerTest extends TestCase {
private IManager&MockObject $settingManager;
private IGroupManager&MockObject $groupManager;
private IAppManager $appManager;
private ConfigManager $configManager;
protected function setUp(): void {
parent::setUp();
@ -48,6 +50,7 @@ class AppConfigControllerTest extends TestCase {
$this->settingManager = $this->createMock(IManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->appManager = Server::get(IAppManager::class);
$this->configManager = Server::get(ConfigManager::class);
}
/**
@ -67,6 +70,7 @@ class AppConfigControllerTest extends TestCase {
$this->groupManager,
$this->settingManager,
$this->appManager,
$this->configManager,
);
} else {
return $this->getMockBuilder(AppConfigController::class)
@ -79,6 +83,7 @@ class AppConfigControllerTest extends TestCase {
$this->groupManager,
$this->settingManager,
$this->appManager,
$this->configManager,
])
->onlyMethods($methods)
->getMock();