Skip to content

Commit 30e2d3c

Browse files
committed
creating_new_rest_resource.md: Detail OpenAPI doc
1 parent 21aaea3 commit 30e2d3c

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

code_samples/api/rest_api/src/Rest/Controller/DefaultController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
],
9898
parameters: [],
9999
requestBody: new Model\RequestBody(
100+
description: 'Set salutation or recipient.',
100101
required: false,
101102
content: new \ArrayObject([
102103
'application/vnd.ibexa.api.GreetingInput+xml' => [
@@ -118,6 +119,7 @@
118119
],
119120
'example' => [
120121
'salutation' => 'Good morning',
122+
'recipient' => 'Planet',
121123
],
122124
],
123125
'application/vnd.ibexa.api.GreetingInput+json' => [
@@ -232,8 +234,6 @@ public function greet(Request $request): Response|Greeting
232234
$greeting = new Greeting();
233235
}
234236

235-
//return $greeting;
236-
237237
$accept = $request->headers->get('Accept', 'application/' . self::DEFAULT_FORMAT);
238238
preg_match('@.*[/+](?P<format>[^/+]+)@', $accept, $matches);
239239
$format = empty($matches['format']) ? self::DEFAULT_FORMAT : $matches['format'];

docs/api/rest_api/extending_rest_api/creating_new_rest_resource.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ A REST controller should:
6565

6666
``` php
6767
[[= include_file('code_samples/api/rest_api/src/Rest/Controller/DefaultController.php', 0, 14) =]]
68-
[[= include_file('code_samples/api/rest_api/src/Rest/Controller/DefaultController.php', 212) =]]
68+
[[= include_file('code_samples/api/rest_api/src/Rest/Controller/DefaultController.php', 214) =]]
6969
```
7070

7171
<details>
@@ -167,10 +167,23 @@ The resource is added to the OpenAPI Description dumped with `ibexa:openapi` com
167167
In `dev` mode, the resource appears in the live documentation at `<dev-domain>/api/ibexa/v2/doc#/App/api_greet_get`.
168168

169169
``` php hl_lines="5 6 16 89"
170-
[[= include_file('code_samples/api/rest_api/src/Rest/Controller/DefaultController.php', 0, 212) =]]
171-
//…
170+
[[= include_file('code_samples/api/rest_api/src/Rest/Controller/DefaultController.php', 0, 215) =]]//…
172171
```
173172

173+
The resource can be tested from this live documentation.
174+
175+
For example, the `POST /greet` at `<dev-domain>/api/ibexa/v2/doc#/App/api_greet_post` can be tested this way:
176+
177+
- Click the **Try it out** button
178+
- In **Request body** section, choose the Content-Type from the drop-down menu. For example, the JSON format `application/vnd.ibexa.api.GreetingInput+json`
179+
- In the text area, edit the JSON to make your own test
180+
- In the **Responses** section, choose the desired response format for successful `200`
181+
- Click the **Execute** button
182+
183+
![Example of test from live doc](../img/rest-api-greeting-form.png)
184+
185+
Eventually, remove properties to see if default values work, inject parse errors, or empty the whole text area to see error handling.
186+
174187
## Registering resources in REST root
175188

176189
You can add the new resource to the [root resource](rest_api_usage.md#rest-root) through a configuration with the following pattern:
107 KB
Loading

0 commit comments

Comments
 (0)