diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 8163b34d3..231131f56 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -472,7 +472,7 @@ public function getQuestion(int $formId, int $questionId): DataResponse { #[CORS()] #[NoAdminRequired()] #[ApiRoute(verb: 'POST', url: '/api/v3/forms/{formId}/questions')] - public function newQuestion(int $formId, string $type, string $text = '', ?int $fromId = null): DataResponse { + public function newQuestion(int $formId, ?string $type = null, string $text = '', ?int $fromId = null): DataResponse { $form = $this->getFormIfAllowed($formId); if ($this->formsService->isFormArchived($form)) { $this->logger->debug('This form is archived and can not be modified'); diff --git a/lib/Db/Question.php b/lib/Db/Question.php index e83edb8be..4ad4b8a0a 100644 --- a/lib/Db/Question.php +++ b/lib/Db/Question.php @@ -36,14 +36,16 @@ /** * @psalm-import-type FormsQuestionExtraSettings from ResponseDefinitions + * @psalm-import-type FormsQuestionType from ResponseDefinitions * @method int getFormId() * @method void setFormId(integer $value) * @method int getOrder() * @method void setOrder(integer $value) + * @psalm-method FormsQuestionType getType() * @method string getType() - * @psalm-method 'date'|'dropdown'|'file'|'long'|'multiple'|'multiple_unique'|'short'|'time' getType() + * @psalm-method 'date'|'datetime'|'dropdown'|'file'|'long'|'multiple'|'multiple_unique'|'short'|'time' getType() * @method void setType(string $value) - * @psalm-method void setType('date'|'dropdown'|'file'|'long'|'multiple'|'multiple_unique'|'short'|'time' $value) + * @psalm-method void setType('date'|'datetime'|'dropdown'|'file'|'long'|'multiple'|'multiple_unique'|'short'|'time' $value) * @method bool getIsRequired() * @method void setIsRequired(bool $value) * @method string getText() @@ -77,7 +79,7 @@ public function __construct() { * @return FormsQuestionExtraSettings */ public function getExtraSettings(): array { - return json_decode($this->getExtraSettingsJson() ?: '{}', false, 512, JSON_THROW_ON_ERROR); + return json_decode($this->getExtraSettingsJson() ?: '{}', true, 512, JSON_THROW_ON_ERROR); } /** @@ -99,7 +101,7 @@ public function setExtraSettings(array $extraSettings) { * id: int, * formId: int, * order: int, - * type: "dropdown"|"multiple"|"multiple_unique"|"date"|"time"|"short"|"long"|"file", + * type: FormsQuestionType, * isRequired: bool, * text: string, * name: string, diff --git a/lib/ResponseDefinitions.php b/lib/ResponseDefinitions.php index 3c40c777a..79a4946aa 100644 --- a/lib/ResponseDefinitions.php +++ b/lib/ResponseDefinitions.php @@ -48,7 +48,7 @@ * validationType?: ?string * } * - * @psalm-type FormsQuestionType = "dropdown"|"multiple"|"multiple_unique"|"date"|"time"|"short"|"long"|"file" + * @psalm-type FormsQuestionType = "dropdown"|"multiple"|"multiple_unique"|"date"|"time"|"short"|"long"|"file"|"datetime" * * @psalm-type FormsQuestion = array{ * id: int, diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php index 8cf534415..d45e5e9ad 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -143,6 +143,10 @@ public function getQuestions(int $formId): array { } } + if (empty($question['extraSettings'])) { + $question['extraSettings'] = new \stdClass(); + } + $questionList[] = $question; } } catch (DoesNotExistException $e) { diff --git a/openapi.json b/openapi.json index dda8f55d0..d9b3c4745 100644 --- a/openapi.json +++ b/openapi.json @@ -465,7 +465,8 @@ "time", "short", "long", - "file" + "file", + "datetime" ] }, "Share": { @@ -1347,14 +1348,11 @@ } ], "requestBody": { - "required": true, + "required": false, "content": { "application/json": { "schema": { "type": "object", - "required": [ - "type" - ], "properties": { "type": { "$ref": "#/components/schemas/QuestionType", diff --git a/tests/Unit/Service/FormsServiceTest.php b/tests/Unit/Service/FormsServiceTest.php index 349331179..066921bba 100644 --- a/tests/Unit/Service/FormsServiceTest.php +++ b/tests/Unit/Service/FormsServiceTest.php @@ -246,7 +246,7 @@ public function dataGetForm() { 'order' => 2, 'type' => 'short', 'isRequired' => true, - 'extraSettings' => [], + 'extraSettings' => new \stdClass, 'text' => 'Question 2', 'description' => '', 'name' => 'city',