Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ControllerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/Controller/AdminSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Success
*/
#[ApiRoute(verb: 'POST', url: '/optional-parameters')]
public function optionalParameters(
int $option1 = 0,
int $option2 = self::ALSO_OPTIONAL,
) {
return DataResponse();
}

}
139 changes: 139 additions & 0 deletions tests/openapi-administration.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
139 changes: 139 additions & 0 deletions tests/openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down