API improvements (#5494)

* hide logo in docs UI
* always return a user in api base controller
* added example for duration fields
This commit is contained in:
Kevin Papst
2025-05-24 22:09:48 +02:00
committed by GitHub
parent 1e0fbf0b73
commit 7b158d57b5
5 changed files with 37 additions and 30 deletions

View File

@@ -20,15 +20,22 @@ use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormTypeInterface;
/**
* @method null|User getUser()
*/
abstract class BaseApiController extends AbstractController
{
public const DATE_ONLY_FORMAT = 'yyyy-MM-dd';
public const DATE_FORMAT = DateTimeType::HTML5_FORMAT;
public const DATE_FORMAT_PHP = 'Y-m-d\TH:i:s';
protected function getUser(): User
{
$user = parent::getUser();
if (!$user instanceof User) {
throw $this->createAccessDeniedException('Need a user for API access');
}
return $user;
}
/**
* @template TFormType of FormTypeInterface<TData>
* @template TData of BaseQuery

View File

@@ -33,6 +33,11 @@ final class DurationType extends AbstractType
'toggle' => false,
'max_hours' => 24,
'icon' => 'clock',
'documentation' => [
'type' => 'string',
'description' => 'Duration - supports various formats: https://www.kimai.org/documentation/duration-format.html',
'example' => '01:30',
]
]);
$resolver->setAllowedTypes('max_hours', 'int');
}

View File

@@ -70,11 +70,20 @@ final class UserType extends AbstractType
// includes the current user if it is a system-account, which is especially useful for forms pages,
// which have a user switcher and display the logged-in user by default
'include_current_user_if_system_account' => false,
'documentation' => [
]);
$resolver->setDefault('documentation', function (Options $options) {
$example = 0;
if ($options['user'] instanceof User) {
$example = $options['user']->getId();
}
return [
'type' => 'integer',
'description' => 'User ID',
],
]);
'example' => $example
];
});
$resolver->setDefault('choices', function (Options $options) {
$query = new UserFormTypeQuery();