Skip to content

Commit

Permalink
fix(OpenApiType): Forbid using ambigous array syntaxes
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Oct 28, 2024
1 parent bed64c9 commit dd13b3f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 47 deletions.
6 changes: 6 additions & 0 deletions src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,19 @@ public static function resolve(string $context, array $definitions, ParamTagValu
}

if ($node instanceof ArrayTypeNode) {
Logger::error($context, "The 'TYPE[]' syntax for arrays is forbidden due to ambiguities. Use 'list<TYPE>' for JSON arrays or 'array<string, TYPE>' for JSON objects instead.");

return new OpenApiType(
context: $context,
type: 'array',
items: self::resolve($context . ': items', $definitions, $node->type),
);
}
if ($node instanceof GenericTypeNode && ($node->type->name === 'array' || $node->type->name === 'list' || $node->type->name === 'non-empty-list') && count($node->genericTypes) === 1) {
if ($node->type->name === 'array') {
Logger::error($context, "The 'array<TYPE>' syntax for arrays is forbidden due to ambiguities. Use 'list<TYPE>' for JSON arrays or 'array<string, TYPE>' for JSON objects instead.");
}

if ($node->genericTypes[0] instanceof IdentifierTypeNode && $node->genericTypes[0]->name === 'empty') {
return new OpenApiType(
context: $context,
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/Controller/AdminSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AdminSettingsController extends OCSController {
/**
* Route is only in the admin scope because there is no "NoAdminRequired" annotation or attribute
*
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Personal settings updated
*/
Expand All @@ -29,7 +29,7 @@ public function adminScopeImplicitFromAdminRequired(): DataResponse {
/**
* Route is in the default scope because the method overwrites with the Attribute
*
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Personal settings updated
*/
Expand All @@ -41,7 +41,7 @@ public function movedToDefaultScope(): DataResponse {
/**
* Route in default scope with tags
*
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Personal settings updated
*/
Expand All @@ -53,7 +53,7 @@ public function movedToSettingsTag(): DataResponse {
/**
* Route in default scope with tags but without named parameters on the attribute
*
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Personal settings updated
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/Controller/ExAppSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ExAppSettingsController extends OCSController {
/**
* Route is in ex_app scope because of the attribute
*
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Personal settings updated
*/
Expand All @@ -32,7 +32,7 @@ public function exAppScopeAttribute(): DataResponse {
/**
* Route is in ex_app scope because of the override
*
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Personal settings updated
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/Controller/FederationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FederationController extends OCSController {
*
* Route is in federation scope as per controller scope
*
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: OK
*/
Expand All @@ -43,7 +43,7 @@ public function federationByController(): DataResponse {
* Route is only in the default scope (moved from federation)
*
* @param NotificationsRequestProperty $property Property
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Personal settings updated
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/Controller/RoutingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class RoutingController extends OCSController {
/**
* OCS Route with attribute
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Success
*/
Expand All @@ -30,7 +30,7 @@ public function attributeOCS() {
* @NoCSRFRequired
*
* Index Route with attribute
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Success
*/
Expand Down
Loading

0 comments on commit dd13b3f

Please sign in to comment.