Skip to content

Commit

Permalink
Correctly add schemas references by nested 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 fd01672 commit d3bd5c3
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 6 deletions.
14 changes: 8 additions & 6 deletions generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -761,12 +761,14 @@ foreach ($scopePaths as $scope => $paths) {
}

// Queue potential sub-refs for exporting as well
if (isset($schemas[$schemaName]['allOf'])) {
foreach ($schemas[$schemaName]['allOf'] as $subType) {
$newRefs = Helpers::collectUsedRefs($subType);
foreach ($newRefs as $newRef) {
if (!isset($scopedSchemas[substr($newRef, strlen('#/components/schemas/'))])) {
$usedSchemas[] = $newRef;
foreach (['allOf', 'oneOf', 'anyOf', 'properties'] as $group) {
if (isset($schemas[$schemaName][$group])) {
foreach ($schemas[$schemaName][$group] as $subType) {
$newRefs = Helpers::collectUsedRefs($subType);
foreach ($newRefs as $newRef) {
if (!isset($scopedSchemas[substr($newRef, strlen('#/components/schemas/'))])) {
$usedSchemas[] = $newRef;
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ static function collectUsedRefs(array $data): array {
}
}
}
if (isset($data['items'])) {
$refs[] = self::collectUsedRefs($data['items']);
}
return array_merge(...$refs);
}
}
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#throwing', 'url' => '/api/{apiVersion}/throwing', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#nestedSchemas', 'url' => '/api/{apiVersion}/nested-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
*/
#[OpenAPI(scope: OpenAPI::SCOPE_FEDERATION)]
class SettingsController extends OCSController {
Expand Down Expand Up @@ -134,4 +135,18 @@ public function doubleScope(): DataResponse {
public function throwing(): DataResponse {
throw new OCSNotFoundException();
}

/**
* @NoAdminRequired
*
* Route is ignored because of scope on the controller
*
* @return DataResponse<Http::STATUS_OK, list<NotificationsNotification>, array{}>
*
* 200: OK
*/
#[OpenAPI]
public function nestedSchemas(): DataResponse {
return new DataResponse();
}
}
174 changes: 174 additions & 0 deletions tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,104 @@
"type": "string"
}
}
},
"Notification": {
"type": "object",
"required": [
"notification_id",
"app",
"user",
"datetime",
"object_type",
"object_id",
"subject",
"message",
"link",
"actions"
],
"properties": {
"notification_id": {
"type": "integer",
"format": "int64"
},
"app": {
"type": "string"
},
"user": {
"type": "string"
},
"datetime": {
"type": "string"
},
"object_type": {
"type": "string"
},
"object_id": {
"type": "string"
},
"subject": {
"type": "string"
},
"message": {
"type": "string"
},
"link": {
"type": "string"
},
"actions": {
"type": "array",
"items": {
"$ref": "#/components/schemas/NotificationAction"
}
},
"subjectRich": {
"type": "string"
},
"subjectRichParameters": {
"type": "object",
"additionalProperties": {
"type": "object"
}
},
"messageRich": {
"type": "string"
},
"messageRichParameters": {
"type": "object",
"additionalProperties": {
"type": "object"
}
},
"icon": {
"type": "string"
},
"shouldNotify": {
"type": "boolean"
}
}
},
"NotificationAction": {
"type": "object",
"required": [
"label",
"link",
"type",
"primary"
],
"properties": {
"label": {
"type": "string"
},
"link": {
"type": "string"
},
"type": {
"type": "string"
},
"primary": {
"type": "boolean"
}
}
}
}
},
Expand Down Expand Up @@ -189,6 +287,82 @@
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/nested-schemas": {
"post": {
"operationId": "settings-nested-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": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Notification"
}
}
}
}
}
}
}
}
}
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/default-admin-overwritten": {
"post": {
"operationId": "settings2-default-admin-scope-overwritten",
Expand Down

0 comments on commit d3bd5c3

Please sign in to comment.