Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow more specific object keys #182

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading