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 8, 2023
1 parent 8a351f6 commit 94962d4
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 @@ -770,7 +770,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 @@ -783,6 +783,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 @@ -253,10 +253,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 @@ -32,6 +32,7 @@
['name' => 'Settings#adminScope', 'url' => '/api/{apiVersion}/admin', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#doubleScope', 'url' => '/api/{apiVersion}/double', '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 @@ -35,6 +35,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 @@ -133,4 +134,18 @@ public function doubleScope(): 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 94962d4

Please sign in to comment.