Skip to content

Commit

Permalink
fix(type): Only allow specified boolean when given
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Jan 8, 2024
1 parent a6dc75d commit 2d51049
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ private static function resolveIdentifier(string $context, array $definitions, s
"positive-int" => new OpenApiType(type: "integer", format: "int64", minimum: 1),
"negative-int" => new OpenApiType(type: "integer", format: "int64", maximum: -1),
"non-positive-int" => new OpenApiType(type: "integer", format: "int64", maximum: 0),
"bool", "boolean", "true", "false" => new OpenApiType(type: "boolean"),
"bool", "boolean" => new OpenApiType(type: "boolean"),
"true" => new OpenApiType(type: "boolean", enum: [true]),
"false" => new OpenApiType(type: "boolean", enum: [false]),
"double" => new OpenApiType(type: "number", format: "double"),
"float" => new OpenApiType(type: "number", format: "float"),
"mixed", "empty", "array" => new OpenApiType(type: "object"),
Expand Down
3 changes: 2 additions & 1 deletion tests/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
['name' => 'Settings#intParameterWithMinAndMax', 'url' => '/api/{apiVersion}/min-max', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#intParameterWithMin', 'url' => '/api/{apiVersion}/min', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#intParameterWithMax', 'url' => '/api/{apiVersion}/max', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#listOfIntStringAndBool', 'url' => '/api/{apiVersion}/mixed-list', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#listOfIntStringAndOneBool', 'url' => '/api/{apiVersion}/mixed-list-one', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#listOfIntStringAndAllBools', 'url' => '/api/{apiVersion}/mixed-list-all', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#booleanParameterRequired', 'url' => '/api/{apiVersion}/boolean', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#booleanParameterDefaultFalse', 'url' => '/api/{apiVersion}/boolean-false', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#booleanParameterDefaultTrue', 'url' => '/api/{apiVersion}/boolean-true', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
Expand Down
18 changes: 15 additions & 3 deletions tests/lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,19 @@ public function intParameterWithMax(int $limit): DataResponse {
*
* 200: Admin settings updated
*/
public function listOfIntStringAndBool($weird): DataResponse {
public function listOfIntStringAndOneBool($weird): DataResponse {
return new DataResponse();
}

/**
* A route with a list of 2 integers, 2 strings and 1 boolean
*
* @param 0|1|'yes'|'no'|true|false $weird Weird list
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
*
* 200: Admin settings updated
*/
public function listOfIntStringAndAllBools($weird): DataResponse {
return new DataResponse();
}

Expand All @@ -237,7 +249,7 @@ public function booleanParameterRequired(bool $yesOrNo): DataResponse {
/**
* A route with boolean defaulting to false
*
* @param bool $yesOrNo Booleandefaulting to false
* @param bool $yesOrNo Boolean defaulting to false
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
*
* 200: Admin settings updated
Expand All @@ -249,7 +261,7 @@ public function booleanParameterDefaultFalse(bool $yesOrNo = false): DataRespons
/**
* A route with boolean defaulting to true
*
* @param bool $yesOrNo Booleandefaulting to true
* @param bool $yesOrNo Boolean defaulting to true
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
*
* 200: Admin settings updated
Expand Down
116 changes: 111 additions & 5 deletions tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1273,9 +1273,9 @@
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/mixed-list": {
"/ocs/v2.php/apps/notifications/api/{apiVersion}/mixed-list-one": {
"post": {
"operationId": "settings-list-of-int-string-and-bool",
"operationId": "settings-list-of-int-string-and-one-bool",
"summary": "A route with a list of 2 integers, 2 strings and 1 boolean",
"description": "This endpoint requires admin access",
"tags": [
Expand All @@ -1298,8 +1298,107 @@
"schema": {
"oneOf": [
{
"type": "boolean"
"type": "integer",
"enum": [
0,
1
]
},
{
"type": "string",
"enum": [
"yes",
"no"
]
},
{
"type": "boolean",
"enum": [
true
]
}
]
}
},
{
"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": {}
}
}
}
}
}
}
}
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/mixed-list-all": {
"post": {
"operationId": "settings-list-of-int-string-and-all-bools",
"summary": "A route with a list of 2 integers, 2 strings and 1 boolean",
"description": "This endpoint requires admin access",
"tags": [
"settings"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "weird",
"in": "query",
"description": "Weird list",
"required": true,
"schema": {
"oneOf": [
{
"type": "integer",
"enum": [
Expand All @@ -1313,6 +1412,13 @@
"yes",
"no"
]
},
{
"type": "boolean",
"enum": [
true,
false
]
}
]
}
Expand Down Expand Up @@ -1477,7 +1583,7 @@
{
"name": "yesOrNo",
"in": "query",
"description": "Booleandefaulting to false",
"description": "Boolean defaulting to false",
"schema": {
"type": "integer",
"default": 0,
Expand Down Expand Up @@ -1562,7 +1668,7 @@
{
"name": "yesOrNo",
"in": "query",
"description": "Booleandefaulting to true",
"description": "Boolean defaulting to true",
"schema": {
"type": "integer",
"default": 1,
Expand Down

0 comments on commit 2d51049

Please sign in to comment.