Skip to content

Commit cd26ad0

Browse files
Merge pull request #11271 from nextcloud/feat/noid/dev-openapi-scopes
feat(developer): Document OpenAPI scopes
2 parents a09ca0d + e89849d commit cd26ad0

1 file changed

Lines changed: 53 additions & 3 deletions

File tree

developer_manual/client_apis/OCS/ocs-openapi.rst

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Psalm will catch these problems for you if you configured the issue handlers men
101101

102102
.. code-block:: php
103103
:caption: Bad
104+
:emphasize-lines: 2
104105
105106
/**
106107
* @return array
@@ -115,6 +116,7 @@ Psalm will catch these problems for you if you configured the issue handlers men
115116
116117
.. code-block:: php
117118
:caption: Good
119+
:emphasize-lines: 2
118120
119121
/**
120122
* @return array{id: int, name: string}
@@ -222,6 +224,7 @@ All 2xx responses should return the same data structure and all 4xx should also
222224

223225
.. code-block:: php
224226
:caption: Bad
227+
:emphasize-lines: 2,7,9
225228
226229
/**
227230
* @return DataResponse<Http::STATUS_OK, array{name: string}, array{}>|DataResponse<Http::STATUS_CREATED, array{id: int, name: string}, array{}>
@@ -249,6 +252,7 @@ All 2xx responses should return the same data structure and all 4xx should also
249252
250253
.. code-block:: php
251254
:caption: Good
255+
:emphasize-lines: 2,7,9
252256
253257
/**
254258
* @return DataResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{id: int, name: string}, array{}>
@@ -284,13 +288,15 @@ Use the ``setHeaders`` method instead.
284288

285289
.. code-block:: php
286290
:caption: Bad
291+
:emphasize-lines: 2
287292
288293
$response = new DataResponse();
289294
$response->addHeader("X-My-Header", "some value");
290295
return $response;
291296
292297
.. code-block:: php
293298
:caption: Good
299+
:emphasize-lines: 2
294300
295301
$response = new DataResponse();
296302
$response->setHeaders(["X-My-Header" => "some value"]);
@@ -622,6 +628,7 @@ The name of every type definition has to start with the app ID.
622628
To import and use the type definition you have to import it in your controller:
623629

624630
.. code-block:: php
631+
:emphasize-lines: 2
625632
626633
/**
627634
* @psalm-import-type TodoItem from ResponseDefinitions
@@ -653,6 +660,7 @@ For this example our ``update`` will throw an exception when the ETag does not m
653660
Adding the correct annotation works like this:
654661

655662
.. code-block:: php
663+
:emphasize-lines: 4
656664
657665
/**
658666
* ...
@@ -672,16 +680,17 @@ How to ignore certain endpoints
672680
-------------------------------
673681

674682
The tool already ignores all the endpoints that are not reachable from the outside, but some apps have reachable endpoints that are not APIs (e.g. serving some HTML).
675-
To ignore those you can add the ``#[IgnoreOpenAPI]`` attribute or if you still support PHP 7 the ``@IgnoreOpenAPI`` annotation to the controller method or the controller class:
683+
To ignore those you can add the ``#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]`` attribute or if you still support PHP 7 the ``@IgnoreOpenAPI`` annotation to the controller method or the controller class:
676684

677685
.. code-block:: php
686+
:emphasize-lines: 4,6
678687
679688
/**
680689
* ...
681690
*
682691
* @IgnoreOpenAPI
683692
*/
684-
#[IgnoreOpenAPI]
693+
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
685694
#[NoAdminRequired]
686695
public function show(): TemplateResponse {
687696
...
@@ -708,6 +717,7 @@ Imagine we take the same Todo app of the previous example and want to expose som
708717
Now you have to add the correct return type annotation:
709718

710719
.. code-block:: php
720+
:emphasize-lines: 3
711721
712722
class Capabilities implements ICapability {
713723
/**
@@ -725,9 +735,49 @@ Now you have to add the correct return type annotation:
725735
726736
The capabilities will automatically appear in the generated specification.
727737

738+
Scopes
739+
------
740+
741+
In some cases a consumer of the API might not want or need to implement all APIs your app offers.
742+
Examples are federation between apps on different servers, administration related endpoints, and more.
743+
The default client which should implement the main functionality is called ``OpenAPI::SCOPE_DEFAULT``.
744+
Constants are available in ``OCP\AppFramework\Http\Attribute\OpenAPI::SCOPE_*`` for better cross-app experience.
745+
A controller and methods can have multiple scopes, however when a method has the attribute set,
746+
all scopes from the controller are ignored.
747+
748+
Methods that require admin permissions due to missing ``#[NoAdminRequired]`` or ``#[PublicPage]`` attribute or the
749+
matching annotation, default to the ``OpenAPI::SCOPE_ADMINISTRATION`` scope.
750+
751+
.. code-block:: php
752+
753+
#[OpenAPI(scope: OpenAPI::SCOPE_ADMINISTRATION)]
754+
#[OpenAPI(scope: OpenAPI::SCOPE_FEDERATION)]
755+
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
756+
#[OpenAPI(scope: 'myscope')]
757+
public function show(): TemplateResponse {
758+
...
759+
}
760+
761+
The different scopes will be saved as ``openapi.json`` for the default scope and ``openapi-{scope}.json`` for the others.
762+
763+
Tags
764+
^^^^
765+
766+
To organize the API endpoints within a scope, tags can be used to group them. By default the controller name is used.
767+
Tags can also differ between different scopes.
768+
769+
.. code-block:: php
770+
771+
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT, tags: ['mytag1'])]
772+
#[OpenAPI(scope: OpenAPI::SCOPE_ADMINISTRATION, tags: ['settings', 'custom2'])]
773+
public function saveSettings(): TemplateResponse {
774+
...
775+
}
776+
728777
How to generate the specification
729778
---------------------------------
730779

731-
If you followed the installation instructions for openapi-extractor you can run ``composer exec generate-spec`` in your apps root folder and you will have a new file called ``openapi.json``.
780+
If you followed the installation instructions for openapi-extractor you can run ``composer exec generate-spec`` in your
781+
apps root folder and you will have a new file called ``openapi.json`` (depending on the used scopes).
732782
If the tool fails somewhere it will tell you what is wrong and often times also how to fix the problem.
733783
Additionally you should run psalm to check for any problems.

0 commit comments

Comments
 (0)