Skip to content

Commit

Permalink
fix: Allow more specific object keys
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Nov 12, 2024
1 parent b8b55cc commit ce0c021
Show file tree
Hide file tree
Showing 5 changed files with 684 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,16 @@ public static function resolve(string $context, array $definitions, ParamTagValu
}

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

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

if ($node instanceof GenericTypeNode && $node->type->name == 'int' && count($node->genericTypes) == 2) {
Expand Down
5 changes: 5 additions & 0 deletions tests/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
['name' => 'Federation#federationByController', 'url' => '/api/{apiVersion}/controller-scope', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Federation#movedToDefaultScope', 'url' => '/api/{apiVersion}/default-scope', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],

['name' => 'ReturnArrays#stringArray', 'url' => '/api/{apiVersion}/return-arrays/string', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'ReturnArrays#nonEmptyStringArray', 'url' => '/api/{apiVersion}/return-arrays/non-empty-string', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'ReturnArrays#lowercaseStringArray', 'url' => '/api/{apiVersion}/return-arrays/lowercase-string', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'ReturnArrays#nonEmptyLowercaseStringArray', 'url' => '/api/{apiVersion}/return-arrays/non-empty-lowercase-string', 'verb' => 'GET', 'requirements' => ['apiVersion' => '(v2)']],

['name' => 'Settings#ignoreByDeprecatedAttributeOnMethod', 'url' => '/api/{apiVersion}/ignore-openapi-attribute', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#ignoreByScopeOnMethod', 'url' => '/api/{apiVersion}/ignore-method-scope', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#ignoreByUnnamedScopeOnMethod', 'url' => '/api/{apiVersion}/ignore-method-scope-unnamed', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
Expand Down
60 changes: 60 additions & 0 deletions tests/lib/Controller/ReturnArraysController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Notifications\Controller;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;

class ReturnArraysController extends OCSController {
/**
* Route with array using string keys
*
* @return DataResponse<Http::STATUS_OK, array<string, mixed>, array{}>
*
* 200: OK
*/
public function stringArray(): DataResponse {
return new DataResponse();
}

/**
* Route with array using non-empty-string keys
*
* @return DataResponse<Http::STATUS_OK, array<non-empty-string, mixed>, array{}>
*
* 200: OK
*/
public function nonEmptyStringArray(): DataResponse {
return new DataResponse();
}

/**
* Route with array using lowercase-string keys
*
* @return DataResponse<Http::STATUS_OK, array<lowercase-string, mixed>, array{}>
*
* 200: OK
*/
public function lowercaseStringArray(): DataResponse {
return new DataResponse();
}

/**
* Route with array using non-empty-lowercase-string keys
*
* @return DataResponse<Http::STATUS_OK, array<non-empty-lowercase-string, mixed>, array{}>
*
* 200: OK
*/
public function nonEmptyLowercaseStringArray(): DataResponse {
return new DataResponse();
}
}
Loading

0 comments on commit ce0c021

Please sign in to comment.