Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix(parameters): Support int numerals
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Nov 10, 2023
commit d2c2487732311e9683b0673d67b89c3aff1fd116
22 changes: 22 additions & 0 deletions src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ static function resolve(string $context, array $definitions, ParamTagValueNode|N

return new OpenApiType(type: "string", enum: $values);
}
if ($isUnion && count($node->types) == count(array_filter($node->types, fn($type) => $type instanceof ConstTypeNode && $type->constExpr instanceof ConstExprIntegerNode))) {
$values = [];
/** @var ConstTypeNode $type */
foreach ($node->types as $type) {
$values[] = (int) $type->constExpr->value;
}

if (count(array_filter($values, fn(string $value) => $value == '')) > 0) {
// Not a valid enum
return new OpenApiType(
type: "integer",
format: "int64",
);
}

return new OpenApiType(
type: "integer",
format: "int64",
enum: $values,
);
}

if ($isUnion || $isIntersection) {
$nullable = false;
Expand Down Expand Up @@ -205,6 +226,7 @@ enum: [$node->constExpr->value],
return new OpenApiType(
type: "integer",
format: "int64",
enum: [(int) $node->constExpr->value],
);
}

Expand Down
1 change: 1 addition & 0 deletions tests/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
['name' => 'Settings#defaultScope', 'url' => '/api/{apiVersion}/settings', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#adminScope', 'url' => '/api/{apiVersion}/admin', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#doubleScope', 'url' => '/api/{apiVersion}/double', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#listOfIntParameters', 'url' => '/api/{apiVersion}/list-of-int', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],

['name' => 'Settings2#defaultAdminScopeOverwritten', 'url' => '/api/{apiVersion}/default-admin-overwritten', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings2#defaultAdminScope', 'url' => '/api/{apiVersion}/default-admin', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,17 @@ protected function createNotificationsPushDevice(): array {
public function doubleScope(): DataResponse {
return new DataResponse();
}

/**
* A route with a limited set of possible integers
*
* @param 1|2|3|4|5|6|7|8|9|10 $limit Maximum number of objects
* @psalm-param int<1, 10> $limit
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
*
* 200: Admin settings updated
*/
public function listOfIntParameters(int $limit): DataResponse {
return new DataResponse();
}
}
94 changes: 94 additions & 0 deletions tests/openapi-federation.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,100 @@
}
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/list-of-int": {
"post": {
"operationId": "settings-list-of-int-parameters",
"summary": "A route with a limited set of possible integers",
"description": "This endpoint requires admin access",
"tags": [
"settings"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "limit",
"in": "query",
"description": "Maximum number of objects",
"required": true,
"schema": {
"type": "integer",
"format": "int64",
"enum": [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
]
}
},
{
"name": "apiVersion",
"in": "path",
"required": true,
"schema": {
"type": "string",
"enum": [
"v2"
],
"default": "v2"
}
},
{
"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": "Admin settings updated",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
}
}
}
},
"tags": []
Expand Down