diff --git a/src/ControllerMethod.php b/src/ControllerMethod.php index 81d7a67..c9c45e3 100644 --- a/src/ControllerMethod.php +++ b/src/ControllerMethod.php @@ -545,6 +545,8 @@ public static function parse(string $context, $type->defaultValue = Helpers::exprToValue($context, $methodParameter->default); $type->hasDefaultValue = true; } catch (UnsupportedExprException $e) { + $type->hasDefaultValue = true; + $type->hasUnknownDefaultValue = true; Logger::debug($context, $e); } } diff --git a/src/OpenApiType.php b/src/OpenApiType.php index 24c5a5f..6de07d9 100644 --- a/src/OpenApiType.php +++ b/src/OpenApiType.php @@ -42,6 +42,7 @@ public function __construct( public ?string $format = null, public bool $nullable = false, public bool $hasDefaultValue = false, + public bool $hasUnknownDefaultValue = false, public bool $deprecated = false, public mixed $defaultValue = null, public ?OpenApiType $items = null, @@ -92,7 +93,7 @@ public function toArray(bool $isParameter = false): array|stdClass { if ($this->deprecated) { $values['deprecated'] = true; } - if ($this->hasDefaultValue) { + if ($this->hasDefaultValue && !$this->hasUnknownDefaultValue) { $values['default'] = $this->type === 'object' && is_array($this->defaultValue) && count($this->defaultValue) === 0 ? new stdClass() : $this->defaultValue; } if ($this->enum !== null) { diff --git a/tests/lib/Controller/AdminSettingsController.php b/tests/lib/Controller/AdminSettingsController.php index 8af8ea7..6fc7d50 100644 --- a/tests/lib/Controller/AdminSettingsController.php +++ b/tests/lib/Controller/AdminSettingsController.php @@ -61,4 +61,24 @@ public function movedToSettingsTag(): DataResponse { public function movedToSettingsTagUnnamed(): DataResponse { return new DataResponse(); } + + public const ALSO_OPTIONAL = 1; + + /** + * OCS Route with attribute + * + * @param int $option1 This is optional with magic number + * @param int $option2 This is optional with constant + * @return DataResponse, array{}> + * + * 200: Success + */ + #[ApiRoute(verb: 'POST', url: '/optional-parameters')] + public function optionalParameters( + int $option1 = 0, + int $option2 = self::ALSO_OPTIONAL, + ) { + return DataResponse(); + } + } diff --git a/tests/openapi-administration.json b/tests/openapi-administration.json index c1b8450..4bc3659 100644 --- a/tests/openapi-administration.json +++ b/tests/openapi-administration.json @@ -9259,6 +9259,145 @@ } } }, + "/ocs/v2.php/apps/notifications/optional-parameters": { + "post": { + "operationId": "admin_settings-optional-parameters", + "summary": "OCS Route with attribute", + "description": "This endpoint requires admin access", + "tags": [ + "admin_settings" + ], + "security": [ + { + "bearer_auth": [] + }, + { + "basic_auth": [] + } + ], + "requestBody": { + "required": false, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "option1": { + "type": "integer", + "format": "int64", + "default": 0, + "description": "This is optional with magic number" + }, + "option2": { + "type": "integer", + "format": "int64", + "description": "This is optional with constant" + } + } + } + } + } + }, + "parameters": [ + { + "name": "OCS-APIRequest", + "in": "header", + "description": "Required to be true for the API request to pass", + "required": true, + "schema": { + "type": "boolean", + "default": true + } + } + ], + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + }, + "401": { + "description": "Current user is not logged in", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + }, + "403": { + "description": "Logged in account must be an admin", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + } + } + } + }, "/ocs/v2.php/tests/attribute-ocs/{param}": { "get": { "operationId": "routing-attributeocs-route", diff --git a/tests/openapi-full.json b/tests/openapi-full.json index c56a39c..afb019c 100644 --- a/tests/openapi-full.json +++ b/tests/openapi-full.json @@ -9459,6 +9459,145 @@ } } }, + "/ocs/v2.php/apps/notifications/optional-parameters": { + "post": { + "operationId": "admin_settings-optional-parameters", + "summary": "OCS Route with attribute", + "description": "This endpoint requires admin access", + "tags": [ + "admin_settings" + ], + "security": [ + { + "bearer_auth": [] + }, + { + "basic_auth": [] + } + ], + "requestBody": { + "required": false, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "option1": { + "type": "integer", + "format": "int64", + "default": 0, + "description": "This is optional with magic number" + }, + "option2": { + "type": "integer", + "format": "int64", + "description": "This is optional with constant" + } + } + } + } + } + }, + "parameters": [ + { + "name": "OCS-APIRequest", + "in": "header", + "description": "Required to be true for the API request to pass", + "required": true, + "schema": { + "type": "boolean", + "default": true + } + } + ], + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + }, + "401": { + "description": "Current user is not logged in", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + }, + "403": { + "description": "Logged in account must be an admin", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": {} + } + } + } + } + } + } + } + } + } + }, "/ocs/v2.php/tests/attribute-ocs/{param}": { "get": { "operationId": "routing-attributeocs-route",