Skip to content

Commit 2587f18

Browse files
Merge pull request #182 from nextcloud/bugfix/noid/allow-more-specific-object-keys
fix: Allow more specific object keys
2 parents b8b55cc + ce0c021 commit 2587f18

File tree

5 files changed

+684
-2
lines changed

5 files changed

+684
-2
lines changed

src/OpenApiType.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,16 @@ public static function resolve(string $context, array $definitions, ParamTagValu
225225
}
226226

227227
if ($node instanceof GenericTypeNode && $node->type->name === 'array' && count($node->genericTypes) === 2 && $node->genericTypes[0] instanceof IdentifierTypeNode) {
228-
if ($node->genericTypes[0]->name === 'string') {
228+
$allowedTypes = ['string', 'lowercase-string', 'non-empty-string', 'non-empty-lowercase-string'];
229+
if (in_array($node->genericTypes[0]->name, $allowedTypes, true)) {
229230
return new OpenApiType(
230231
context: $context,
231232
type: 'object',
232233
additionalProperties: self::resolve($context . ': additionalProperties', $definitions, $node->genericTypes[1]),
233234
);
234235
}
235236

236-
Logger::panic($context, "JSON objects can only be indexed by 'string' but got '" . $node->genericTypes[0]->name . "'");
237+
Logger::panic($context, "JSON objects can only be indexed by '" . implode("', '", $allowedTypes) . "' but got '" . $node->genericTypes[0]->name . "'");
237238
}
238239

239240
if ($node instanceof GenericTypeNode && $node->type->name == 'int' && count($node->genericTypes) == 2) {

tests/appinfo/routes.php

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
['name' => 'Federation#federationByController', 'url' => '/api/{apiVersion}/controller-scope', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
2222
['name' => 'Federation#movedToDefaultScope', 'url' => '/api/{apiVersion}/default-scope', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
2323

24+
['name' => 'ReturnArrays#stringArray', 'url' => '/api/{apiVersion}/return-arrays/string', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v2)']],
25+
['name' => 'ReturnArrays#nonEmptyStringArray', 'url' => '/api/{apiVersion}/return-arrays/non-empty-string', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v2)']],
26+
['name' => 'ReturnArrays#lowercaseStringArray', 'url' => '/api/{apiVersion}/return-arrays/lowercase-string', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v2)']],
27+
['name' => 'ReturnArrays#nonEmptyLowercaseStringArray', 'url' => '/api/{apiVersion}/return-arrays/non-empty-lowercase-string', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v2)']],
28+
2429
['name' => 'Settings#ignoreByDeprecatedAttributeOnMethod', 'url' => '/api/{apiVersion}/ignore-openapi-attribute', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
2530
['name' => 'Settings#ignoreByScopeOnMethod', 'url' => '/api/{apiVersion}/ignore-method-scope', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
2631
['name' => 'Settings#ignoreByUnnamedScopeOnMethod', 'url' => '/api/{apiVersion}/ignore-method-scope-unnamed', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Notifications\Controller;
11+
12+
use OCP\AppFramework\Http;
13+
use OCP\AppFramework\Http\DataResponse;
14+
use OCP\AppFramework\OCSController;
15+
16+
class ReturnArraysController extends OCSController {
17+
/**
18+
* Route with array using string keys
19+
*
20+
* @return DataResponse<Http::STATUS_OK, array<string, mixed>, array{}>
21+
*
22+
* 200: OK
23+
*/
24+
public function stringArray(): DataResponse {
25+
return new DataResponse();
26+
}
27+
28+
/**
29+
* Route with array using non-empty-string keys
30+
*
31+
* @return DataResponse<Http::STATUS_OK, array<non-empty-string, mixed>, array{}>
32+
*
33+
* 200: OK
34+
*/
35+
public function nonEmptyStringArray(): DataResponse {
36+
return new DataResponse();
37+
}
38+
39+
/**
40+
* Route with array using lowercase-string keys
41+
*
42+
* @return DataResponse<Http::STATUS_OK, array<lowercase-string, mixed>, array{}>
43+
*
44+
* 200: OK
45+
*/
46+
public function lowercaseStringArray(): DataResponse {
47+
return new DataResponse();
48+
}
49+
50+
/**
51+
* Route with array using non-empty-lowercase-string keys
52+
*
53+
* @return DataResponse<Http::STATUS_OK, array<non-empty-lowercase-string, mixed>, array{}>
54+
*
55+
* 200: OK
56+
*/
57+
public function nonEmptyLowercaseStringArray(): DataResponse {
58+
return new DataResponse();
59+
}
60+
}

0 commit comments

Comments
 (0)