Skip to content

Commit ebb1e24

Browse files
committed
Version OpenAPI files in the repository
1 parent 829fc0d commit ebb1e24

5 files changed

Lines changed: 42 additions & 18 deletions

File tree

.github/workflows/api_refs.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ jobs:
7373
git diff-index --quiet --cached HEAD || git commit -m "PHP API Ref HTML"
7474
git add docs/api/rest_api/rest_api_reference/rest_api_reference.html
7575
git diff-index --quiet --cached HEAD || git commit -m "REST API Ref HTML"
76+
git add docs/api/rest_api/rest_api_reference/openapi.yaml
77+
git add docs/api/rest_api/rest_api_reference/openapi.json
78+
git diff-index --quiet --cached HEAD || git commit -m "REST API OpenAPI spec"
7679
7780
- name: Create Pull Request
7881
uses: peter-evans/create-pull-request@v7

docs/api/rest_api/rest_api_usage/rest_api_usage.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ The REST API uses HTTP methods (such as `GET` and `PUBLISH`), and HTTP headers t
1515

1616
The REST API is built on top of [API Platform](https://api-platform.com/docs/symfony/) and meets the [OpenAPI](https://www.openapis.org/) standard.
1717

18-
You can download the OpenAPI specification from the [REST API Reference](/api/rest_api/rest_api_reference/rest_api_reference.html), or generate it for your project by running one of the commands below:
18+
You can download the OpenAPI specification in:
19+
20+
- [YAML format](openapi.yaml)
21+
- [JSON format](openapi.json)
22+
23+
You can also generate one for your project by running one of the commands below:
1924

2025
``` bash
2126
php bin/console ibexa:openapi --output=openapi.json # JSON output

tools/api_refs/api_refs.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ set +x;
55
AUTH_JSON=${1:-~/.composer/auth.json}; # Path to an auth.json file allowing to install the targeted edition and version
66
PHP_API_OUTPUT_DIR=${2:-./docs/api/php_api/php_api_reference}; # Path to the directory where the built PHP API Reference is hosted
77
REST_API_OUTPUT_FILE=${3:-./docs/api/rest_api/rest_api_reference/rest_api_reference.html}; # Path to the REST API Reference file
8+
REST_API_OPENAPI_FILE_YAML=${4:-./docs/api/rest_api/rest_api_reference/openapi.yaml}; # Path to the REST API OpenAPI spec file
9+
REST_API_OPENAPI_FILE_JSON=${5:-./docs/api/rest_api/rest_api_reference/openapi.json}; # Path to the REST API OpenAPI spec file
810

911
DXP_EDITION='commerce'; # Edition from and for which the Reference is built
1012
DXP_VERSION='5.0.*'; # Version from and for which the Reference is built
@@ -37,6 +39,8 @@ if [ ! -d $PHP_API_OUTPUT_DIR ]; then
3739
fi;
3840
PHP_API_OUTPUT_DIR=$(realpath $PHP_API_OUTPUT_DIR); # Transform into absolute path before changing the working directory
3941
REST_API_OUTPUT_FILE=$(realpath $REST_API_OUTPUT_FILE); # Transform into absolute path before changing the working directory
42+
REST_API_OPENAPI_FILE_YAML=$(realpath $REST_API_OPENAPI_FILE_YAML); # Transform into absolute path before changing the working directory
43+
REST_API_OPENAPI_FILE_JSON=$(realpath $REST_API_OPENAPI_FILE_JSON); # Transform into absolute path before changing the working directory
4044

4145
if [ 1 -eq $FORCE_DXP_INSTALL ]; then
4246
echo 'Remove temporary directory…';
@@ -217,10 +221,16 @@ echo 'Dump REST OpenAPI schema… ';
217221
$PHP_BINARY bin/console ibexa:openapi --yaml \
218222
| sed "s@info:@info:\n x-logo:\n url: 'https://doc.ibexa.co/en/latest/images/ibexa-dxp-logo.png'@" \
219223
> openapi.yaml;
224+
$PHP_BINARY bin/console ibexa:openapi \
225+
| sed 's@"info": {@"info": {\n "x-logo": {\n "url": "https://doc.ibexa.co/en/latest/images/ibexa-dxp-logo.png"\n },@' \
226+
> openapi.json;
220227
echo 'Fix REST OpenAPI schema… ';
221228
$PHP_BINARY $OPENAPI_FIX;
222229
echo 'Build REST Reference… ';
223230
redocly build-docs openapi.yaml --output $REST_API_OUTPUT_FILE --config $REDOCLY_CONFIG --template $REDOCLY_TEMPLATE;
231+
echo 'Copy OpenAPI spec to documentation… ';
232+
cp openapi.yaml $REST_API_OPENAPI_FILE_YAML;
233+
cp openapi.json $REST_API_OPENAPI_FILE_JSON;
224234

225235
if [ 1 -eq $FORCE_DXP_INSTALL ]; then
226236
echo 'Remove temporary directory…';

tools/api_refs/openapi.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
<?php
22

3-
$openApi = yaml_parse_file('openapi.yaml');
4-
5-
foreach ($openApi['paths'] as $path => &$pathMethods) {
6-
foreach ($pathMethods as $method => &$methodDefinition) {
7-
if (array_key_exists('requestBody', $methodDefinition) && array_key_exists('content', $methodDefinition['requestBody'])) {
8-
//foreach ($methodDefinition['requestBody']['content'] as $contentType => &$contentDefinition) {}
9-
ksort($methodDefinition['requestBody']['content']);
10-
}/* */elseif (array_key_exists('requestBody', $methodDefinition)) {
11-
echo "$method $path\n";
12-
}/**/
13-
foreach ($methodDefinition['responses'] as $responseCode => &$responseDefinition) {
14-
if (array_key_exists('content', $responseDefinition)) {
15-
//foreach ($responseDefinition['content'] as $contentType => &$contentDefinition) {}
16-
ksort($responseDefinition['content']);
17-
}/* * /else {
18-
echo "$method $path: $responseCode\n";
19-
}/**/
3+
function sortOpenApiContent(array &$openApi): void
4+
{
5+
foreach ($openApi['paths'] as $path => &$pathMethods) {
6+
foreach ($pathMethods as $method => &$methodDefinition) {
7+
if (array_key_exists('requestBody', $methodDefinition) && array_key_exists('content', $methodDefinition['requestBody'])) {
8+
ksort($methodDefinition['requestBody']['content']);
9+
}
10+
foreach ($methodDefinition['responses'] as $responseCode => &$responseDefinition) {
11+
if (array_key_exists('content', $responseDefinition)) {
12+
ksort($responseDefinition['content']);
13+
}
14+
}
2015
}
2116
}
2217
}
2318

19+
$openApi = yaml_parse_file('openapi.yaml');
20+
sortOpenApiContent($openApi);
2421
yaml_emit_file('openapi.yaml', $openApi);
22+
23+
$openApiJson = json_decode(file_get_contents('openapi.json'), true);
24+
sortOpenApiContent($openApiJson);
25+
file_put_contents('openapi.json', json_encode($openApiJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

tools/api_refs/redocly.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ openapi:
44
logo:
55
maxWidth: '76px!important'
66
maxHeight: '26px'
7+
downloadUrls:
8+
- title: Download as YAML
9+
url: 'https://raw.githubusercontent.com/swagger-api/swagger-petstore/refs/heads/master/src/main/resources/openapi.yaml'
10+
- title: Download as JSON
11+
url: 'https://raw.githubusercontent.com/swagger-api/swagger-petstore/refs/heads/master/src/main/resources/openapi.yaml'

0 commit comments

Comments
 (0)