Skip to content

Commit

Permalink
Handle all possible cases of other Schemas when importing used schemas
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Dec 7, 2023
1 parent f464078 commit e0274d7
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 5 deletions.
11 changes: 10 additions & 1 deletion generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ foreach ($scopePaths as $scope => $paths) {
}

// Queue potential sub-refs for exporting as well
foreach (['allOf', 'oneOf', 'anyOf', 'properties'] as $group) {
foreach (['allOf', 'oneOf', 'anyOf', 'properties', 'additionalProperties'] as $group) {
if (isset($schemas[$schemaName][$group])) {
foreach ($schemas[$schemaName][$group] as $subType) {
$newRefs = Helpers::collectUsedRefs($subType);
Expand All @@ -777,6 +777,15 @@ foreach ($scopePaths as $scope => $paths) {
}
}

if (isset($schemas[$schemaName]['items'])) {
$newRefs = Helpers::collectUsedRefs($schemas[$schemaName]['items']);
foreach ($newRefs as $newRef) {
if (!isset($scopedSchemas[substr($newRef, strlen('#/components/schemas/'))])) {
$usedSchemas[] = $newRef;
}
}
}

$scopedSchemas[$schemaName] = $schemas[$schemaName];
}

Expand Down
10 changes: 6 additions & 4 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,12 @@ static function collectUsedRefs(array $data): array {
if (isset($data['$ref'])) {
$refs[] = [$data['$ref']];
}
if (isset($data['properties'])) {
foreach ($data['properties'] as $property) {
if (is_array($property)) {
$refs[] = self::collectUsedRefs($property);
foreach (['allOf', 'oneOf', 'anyOf', 'properties', 'additionalProperties'] as $group) {
if (isset($data[$group]) && is_array($data[$group])) {
foreach ($data[$group] as $property) {
if (is_array($property)) {
$refs[] = self::collectUsedRefs($property);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
['name' => 'Settings#doubleScope', 'url' => '/api/{apiVersion}/double', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#throwing', 'url' => '/api/{apiVersion}/throwing', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#nestedSchemas', 'url' => '/api/{apiVersion}/nested-schemas', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#listSchemas', 'url' => '/api/{apiVersion}/list-schemas', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],

['name' => 'Settings2#defaultAdminScopeOverwritten', 'url' => '/api/{apiVersion}/default-admin-overwritten', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings2#defaultAdminScope', 'url' => '/api/{apiVersion}/default-admin', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
/**
* @psalm-import-type NotificationsPushDevice from ResponseDefinitions
* @psalm-import-type NotificationsNotification from ResponseDefinitions
* @psalm-import-type NotificationsCollection from ResponseDefinitions
*/
#[OpenAPI(scope: OpenAPI::SCOPE_FEDERATION)]
class SettingsController extends OCSController {
Expand Down Expand Up @@ -149,4 +150,18 @@ public function throwing(): DataResponse {
public function nestedSchemas(): DataResponse {
return new DataResponse();
}

/**
* @NoAdminRequired
*
* Route is ignored because of scope on the controller
*
* @return DataResponse<Http::STATUS_OK, NotificationsCollection, array{}>
*
* 200: OK
*/
#[OpenAPI]
public function listSchemas(): DataResponse {
return new DataResponse();
}
}
9 changes: 9 additions & 0 deletions tests/lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
namespace OCA\Notifications;

/**
* @psalm-type NotificationsItem = array{
* label: string,
* link: string,
* type: string,
* primary: bool,
* }
*
* @psalm-type NotificationsCollection = list<NotificationsItem>
*
* @psalm-type NotificationsNotificationAction = array{
* label: string,
* link: string,
Expand Down
102 changes: 102 additions & 0 deletions tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@
}
}
},
"Collection": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Item"
}
},
"NotificationAction": {
"type": "object",
"required": [
Expand All @@ -141,6 +147,29 @@
"type": "boolean"
}
}
},
"Item": {
"type": "object",
"required": [
"label",
"link",
"type",
"primary"
],
"properties": {
"label": {
"type": "string"
},
"link": {
"type": "string"
},
"type": {
"type": "string"
},
"primary": {
"type": "boolean"
}
}
}
}
},
Expand Down Expand Up @@ -363,6 +392,79 @@
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/list-schemas": {
"post": {
"operationId": "settings-list-schemas",
"summary": "Route is ignored because of scope on the controller",
"tags": [
"settings"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "apiVersion",
"in": "path",
"required": true,
"schema": {
"type": "string",
"enum": [
"v2"
],
"default": "v2"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"$ref": "#/components/schemas/Collection"
}
}
}
}
}
}
}
}
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/default-admin-overwritten": {
"post": {
"operationId": "settings2-default-admin-scope-overwritten",
Expand Down

0 comments on commit e0274d7

Please sign in to comment.