From 5e0bc6313c19149709cd29c5f8e73930059f2d07 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 7 Dec 2023 13:11:38 +0100 Subject: [PATCH] Add correct response for OCS*Exceptions Signed-off-by: Joas Schilling --- src/ControllerMethod.php | 2 +- src/Helpers.php | 4 +- tests/appinfo/routes.php | 1 + tests/lib/Controller/SettingsController.php | 16 ++++ tests/openapi-federation.json | 99 +++++++++++++++++++++ 5 files changed, 120 insertions(+), 2 deletions(-) diff --git a/src/ControllerMethod.php b/src/ControllerMethod.php index e817d4a..3dd363f 100644 --- a/src/ControllerMethod.php +++ b/src/ControllerMethod.php @@ -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); } } } diff --git a/src/Helpers.php b/src/Helpers.php index d5b1df1..78b8dad 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -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" => [ diff --git a/tests/appinfo/routes.php b/tests/appinfo/routes.php index 6827908..e7fd325 100644 --- a/tests/appinfo/routes.php +++ b/tests/appinfo/routes.php @@ -33,6 +33,7 @@ ['name' => 'Settings#doubleScope', 'url' => '/api/{apiVersion}/double', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], ['name' => 'Settings#nestedSchemas', 'url' => '/api/{apiVersion}/nested-schemas', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']], ['name' => 'Settings#listSchemas', 'url' => '/api/{apiVersion}/list-schemas', '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)']], diff --git a/tests/lib/Controller/SettingsController.php b/tests/lib/Controller/SettingsController.php index 3e79b45..21fa755 100644 --- a/tests/lib/Controller/SettingsController.php +++ b/tests/lib/Controller/SettingsController.php @@ -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; /** @@ -148,4 +149,19 @@ public function nestedSchemas(): DataResponse { public function listSchemas(): DataResponse { return new DataResponse(); } + + /** + * @NoAdminRequired + * + * Route throws an OCS exception + * + * @return DataResponse, array{}> + * @throws OCSNotFoundException Throwing OCS exception + * + * 200: Admin settings updated + * 404: Throwing all the time + */ + public function throwing(): DataResponse { + throw new OCSNotFoundException(); + } } diff --git a/tests/openapi-federation.json b/tests/openapi-federation.json index fc38cea..c9b7493 100644 --- a/tests/openapi-federation.json +++ b/tests/openapi-federation.json @@ -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": []