Skip to content

Commit fd01672

Browse files
committed
Add correct response for OCS*Exceptions
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent 113bf16 commit fd01672

5 files changed

Lines changed: 120 additions & 2 deletions

File tree

src/ControllerMethod.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static function parse(string $context, array $definitions, ClassMethod $method,
8484
} else {
8585
$responseDescriptions[$statusCode] = $docNode->value->description;
8686
}
87-
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "text/plain", new OpenApiType(type: "string"), null);
87+
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "application/json", new OpenApiType(type: "array", maxLength: 0), null);
8888
}
8989
}
9090
}

src/Helpers.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ static function mergeSchemas(array $schemas) {
103103
}
104104

105105
static function wrapOCSResponse(Route $route, ControllerMethodResponse $response, array|stdClass $schema): array|stdClass {
106-
if ($route->isOCS && $response->className == "DataResponse") {
106+
if ($route->isOCS
107+
&& ($response->className === 'DataResponse'
108+
|| (str_starts_with($response->className, 'OCS') && str_ends_with($response->className, 'Exception')))) {
107109
return [
108110
"type" => "object",
109111
"required" => [

tests/appinfo/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
['name' => 'Settings#defaultScope', 'url' => '/api/{apiVersion}/settings', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
3232
['name' => 'Settings#adminScope', 'url' => '/api/{apiVersion}/admin', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
3333
['name' => 'Settings#doubleScope', 'url' => '/api/{apiVersion}/double', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
34+
['name' => 'Settings#throwing', 'url' => '/api/{apiVersion}/throwing', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
3435

3536
['name' => 'Settings2#defaultAdminScopeOverwritten', 'url' => '/api/{apiVersion}/default-admin-overwritten', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
3637
['name' => 'Settings2#defaultAdminScope', 'url' => '/api/{apiVersion}/default-admin', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],

tests/lib/Controller/SettingsController.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCP\AppFramework\Http;
3131
use OCP\AppFramework\Http\Attribute\OpenAPI;
3232
use OCP\AppFramework\Http\DataResponse;
33+
use OCP\AppFramework\OCS\OCSNotFoundException;
3334
use OCP\AppFramework\OCSController;
3435

3536
/**
@@ -118,4 +119,19 @@ protected function createNotificationsPushDevice(): array {
118119
public function doubleScope(): DataResponse {
119120
return new DataResponse();
120121
}
122+
123+
/**
124+
* @NoAdminRequired
125+
*
126+
* Route is in admin and default scope
127+
*
128+
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
129+
* @throws OCSNotFoundException Throwing OCS exception
130+
*
131+
* 200: Admin settings updated
132+
* 404: Throwing all the time
133+
*/
134+
public function throwing(): DataResponse {
135+
throw new OCSNotFoundException();
136+
}
121137
}

tests/openapi-federation.json

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,105 @@
117117
}
118118
}
119119
}
120+
},
121+
"/ocs/v2.php/apps/notifications/api/{apiVersion}/throwing": {
122+
"post": {
123+
"operationId": "settings-throwing",
124+
"summary": "Route is in admin and default scope",
125+
"tags": [
126+
"settings"
127+
],
128+
"security": [
129+
{
130+
"bearer_auth": []
131+
},
132+
{
133+
"basic_auth": []
134+
}
135+
],
136+
"parameters": [
137+
{
138+
"name": "apiVersion",
139+
"in": "path",
140+
"required": true,
141+
"schema": {
142+
"type": "string",
143+
"enum": [
144+
"v2"
145+
],
146+
"default": "v2"
147+
}
148+
},
149+
{
150+
"name": "OCS-APIRequest",
151+
"in": "header",
152+
"description": "Required to be true for the API request to pass",
153+
"required": true,
154+
"schema": {
155+
"type": "boolean",
156+
"default": true
157+
}
158+
}
159+
],
160+
"responses": {
161+
"200": {
162+
"description": "Admin settings updated",
163+
"content": {
164+
"application/json": {
165+
"schema": {
166+
"type": "object",
167+
"required": [
168+
"ocs"
169+
],
170+
"properties": {
171+
"ocs": {
172+
"type": "object",
173+
"required": [
174+
"meta",
175+
"data"
176+
],
177+
"properties": {
178+
"meta": {
179+
"$ref": "#/components/schemas/OCSMeta"
180+
},
181+
"data": {}
182+
}
183+
}
184+
}
185+
}
186+
}
187+
}
188+
},
189+
"404": {
190+
"description": "Throwing OCS exception",
191+
"content": {
192+
"application/json": {
193+
"schema": {
194+
"type": "object",
195+
"required": [
196+
"ocs"
197+
],
198+
"properties": {
199+
"ocs": {
200+
"type": "object",
201+
"required": [
202+
"meta",
203+
"data"
204+
],
205+
"properties": {
206+
"meta": {
207+
"$ref": "#/components/schemas/OCSMeta"
208+
},
209+
"data": {}
210+
}
211+
}
212+
}
213+
}
214+
}
215+
}
216+
}
217+
}
218+
}
120219
}
121220
},
122221
"tags": []

0 commit comments

Comments
 (0)