Skip to content

Commit

Permalink
Add correct response for OCS*Exceptions
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Dec 7, 2023
1 parent 113bf16 commit fd01672
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ControllerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static function parse(string $context, array $definitions, ClassMethod $method,
} else {
$responseDescriptions[$statusCode] = $docNode->value->description;
}
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "text/plain", new OpenApiType(type: "string"), null);
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "application/json", new OpenApiType(type: "array", maxLength: 0), null);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ static function mergeSchemas(array $schemas) {
}

static function wrapOCSResponse(Route $route, ControllerMethodResponse $response, array|stdClass $schema): array|stdClass {
if ($route->isOCS && $response->className == "DataResponse") {
if ($route->isOCS
&& ($response->className === 'DataResponse'
|| (str_starts_with($response->className, 'OCS') && str_ends_with($response->className, 'Exception')))) {
return [
"type" => "object",
"required" => [
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#throwing', 'url' => '/api/{apiVersion}/throwing', '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
16 changes: 16 additions & 0 deletions tests/lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;

/**
Expand Down Expand Up @@ -118,4 +119,19 @@ protected function createNotificationsPushDevice(): array {
public function doubleScope(): DataResponse {
return new DataResponse();
}

/**
* @NoAdminRequired
*
* Route is in admin and default scope
*
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
* @throws OCSNotFoundException Throwing OCS exception
*
* 200: Admin settings updated
* 404: Throwing all the time
*/
public function throwing(): DataResponse {
throw new OCSNotFoundException();
}
}
99 changes: 99 additions & 0 deletions tests/openapi-federation.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,105 @@
}
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/throwing": {
"post": {
"operationId": "settings-throwing",
"summary": "Route is in admin and default scope",
"tags": [
"settings"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"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": {}
}
}
}
}
}
}
},
"404": {
"description": "Throwing OCS exception",
"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

0 comments on commit fd01672

Please sign in to comment.