Skip to content

Commit 0992790

Browse files
authored
Upgrade Swagger UI to v3 (React inside) (#1138)
1 parent 5729058 commit 0992790

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+256
-12624
lines changed

src/Bridge/Symfony/Bundle/Action/SwaggerUiAction.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Action;
1515

1616
use ApiPlatform\Core\Documentation\Documentation;
17+
use ApiPlatform\Core\Exception\RuntimeException;
1718
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
1819
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
1920
use Symfony\Component\HttpFoundation\Request;
@@ -117,8 +118,23 @@ private function getContext(Request $request, Documentation $documentation): arr
117118
} elseif (null !== $itemOperationName = $request->attributes->get('_api_item_operation_name')) {
118119
$swaggerData['operationId'] = sprintf('%s%sItem', $itemOperationName, $swaggerData['shortName']);
119120
}
121+
122+
list($swaggerData['path'], $swaggerData['method']) = $this->getPathAndMethod($swaggerData);
120123
}
121124

122125
return $context + ['swagger_data' => $swaggerData];
123126
}
127+
128+
private function getPathAndMethod(array $swaggerData): array
129+
{
130+
foreach ($swaggerData['spec']['paths'] as $path => $operations) {
131+
foreach ($operations as $method => $operation) {
132+
if ($operation['operationId'] === $swaggerData['operationId']) {
133+
return [$path, $method];
134+
}
135+
}
136+
}
137+
138+
throw new RuntimeException(sprintf('The operation "%s" cannot be found in the Swagger specification.', $swaggerData['operationId']));
139+
}
124140
}
Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,59 @@
1-
$(function () {
2-
var data = JSON.parse($('#swagger-data').html());
3-
window.swaggerUi = new SwaggerUi({
4-
url: data.url,
5-
spec: data.spec,
6-
dom_id: 'swagger-ui-container',
7-
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
8-
onComplete: function() {
9-
if (data.oauth.enabled && 'function' === typeof initOAuth) {
10-
initOAuth({
11-
clientId: data.oauth.clientId,
12-
clientSecret: data.oauth.clientSecret,
13-
realm: data.oauth.type,
14-
appName: data.spec.info.title,
15-
scopeSeparator: ' ',
16-
additionalQueryStringParams: {}
17-
});
18-
}
19-
20-
$('pre code').each(function(i, e) {
21-
hljs.highlightBlock(e)
22-
});
23-
24-
if (data.operationId !== undefined) {
25-
var queryParameters = data.queryParameters;
26-
var domSelector = '#' + data.shortName+'_'+data.operationId;
27-
28-
$(domSelector + ' form.sandbox input.parameter').each(function (i, e) {
29-
var $e = $(e);
30-
var name = $e.attr('name');
31-
32-
if (name in queryParameters) {
33-
$e.val(queryParameters[name]);
34-
}
35-
});
36-
37-
if (data.id) {
38-
$(domSelector + ' form.sandbox input[name="id"]').val(data.id);
39-
}
40-
41-
$(domSelector + ' form.sandbox').submit();
42-
document.location.hash = '#!/' + data.shortName + '/' + data.operationId;
43-
}
44-
},
45-
onFailure: function() {
46-
log('Unable to Load SwaggerUI');
47-
},
48-
docExpansion: 'list',
49-
jsonEditor: false,
50-
defaultModelRendering: 'schema',
51-
showRequestHeaders: true
1+
window.onload = () => {
2+
const data = JSON.parse(document.getElementById('swagger-data').innerText);
3+
const ui = SwaggerUIBundle({
4+
spec: data.spec,
5+
dom_id: '#swagger-ui',
6+
validatorUrl: null,
7+
presets: [
8+
SwaggerUIBundle.presets.apis,
9+
SwaggerUIStandalonePreset
10+
],
11+
plugins: [
12+
SwaggerUIBundle.plugins.DownloadUrl
13+
],
14+
layout: 'StandaloneLayout'
15+
});
16+
17+
if (data.oauth.enabled) {
18+
ui.initOAuth({
19+
clientId: data.oauth.clientId,
20+
clientSecret: data.oauth.clientSecret,
21+
realm: data.oauth.type,
22+
appName: data.spec.info.title,
23+
scopeSeparator: ' ',
24+
additionalQueryStringParams: {}
5225
});
26+
}
5327

54-
window.swaggerUi.load();
28+
window.ui = ui;
5529

56-
function log() {
57-
if ('console' in window) {
58-
console.log.apply(console, arguments);
59-
}
30+
if (!data.operationId) return;
31+
32+
const observer = new MutationObserver(function (mutations, self) {
33+
const op = document.getElementById(`operations,${data.method}-${data.path},${data.shortName}`);
34+
if (!op) return;
35+
36+
self.disconnect();
37+
38+
op.querySelector('.opblock-summary').click();
39+
op.querySelector('.try-out__btn').click();
40+
41+
if (data.id) {
42+
const inputId = op.querySelector('.parameters input[placeholder="id"]');
43+
inputId.value = data.id;
44+
inputId.dispatchEvent(new Event('input', { bubbles: true }));
6045
}
61-
});
46+
47+
for (let input of op.querySelectorAll('.parameters input')) {
48+
if (input.placeholder in data.queryParameters) {
49+
input.value = data.queryParameters[input.placeholder];
50+
input.dispatchEvent(new Event('input', { bubbles: true }));
51+
}
52+
}
53+
54+
op.querySelector('.execute').click();
55+
op.scrollIntoView();
56+
});
57+
58+
observer.observe(document, {childList: true, subtree: true});
59+
};

0 commit comments

Comments
 (0)