Skip to content

Commit 5684bae

Browse files
committed
Finish the integration
1 parent 7aac694 commit 5684bae

File tree

2 files changed

+52
-57
lines changed

2 files changed

+52
-57
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: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
window.onload = function() {
1+
window.onload = () => {
22
const data = JSON.parse(document.getElementById('swagger-data').innerText);
33
const ui = SwaggerUIBundle({
44
spec: data.spec,
@@ -11,69 +11,48 @@ window.onload = function() {
1111
plugins: [
1212
SwaggerUIBundle.plugins.DownloadUrl
1313
],
14-
layout: "StandaloneLayout"
14+
layout: 'StandaloneLayout'
1515
});
16-
window.ui = ui
17-
};
1816

19-
/*$(function () {
20-
var data = JSON.parse($('#swagger-data').html());
21-
window.swaggerUi = new SwaggerUi({
22-
url: data.url,
23-
spec: data.spec,
24-
dom_id: 'swagger-ui-container',
25-
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
26-
onComplete: function() {
27-
if (data.oauth.enabled && 'function' === typeof initOAuth) {
28-
initOAuth({
29-
clientId: data.oauth.clientId,
30-
clientSecret: data.oauth.clientSecret,
31-
realm: data.oauth.type,
32-
appName: data.spec.info.title,
33-
scopeSeparator: ' ',
34-
additionalQueryStringParams: {}
35-
});
36-
}
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: {}
25+
});
26+
}
3727

38-
$('pre code').each(function(i, e) {
39-
hljs.highlightBlock(e)
40-
});
28+
window.ui = ui;
4129

42-
if (data.operationId !== undefined) {
43-
var queryParameters = data.queryParameters;
44-
var domSelector = '#' + data.shortName+'_'+data.operationId;
30+
if (!data.operationId) return;
4531

46-
$(domSelector + ' form.sandbox input.parameter').each(function (i, e) {
47-
var $e = $(e);
48-
var name = $e.attr('name');
32+
const observer = new MutationObserver(function (mutations, self) {
33+
const op = document.getElementById(`operations,${data.method}-${data.path},${data.shortName}`);
34+
if (!op) return;
4935

50-
if (name in queryParameters) {
51-
$e.val(queryParameters[name]);
52-
}
53-
});
36+
self.disconnect();
5437

55-
if (data.id) {
56-
$(domSelector + ' form.sandbox input[name="id"]').val(data.id);
57-
}
38+
op.querySelector('.opblock-summary').click();
39+
op.querySelector('.try-out__btn').click();
5840

59-
$(domSelector + ' form.sandbox').submit();
60-
document.location.hash = '#!/' + data.shortName + '/' + data.operationId;
61-
}
62-
},
63-
onFailure: function() {
64-
log('Unable to Load SwaggerUI');
65-
},
66-
docExpansion: 'list',
67-
jsonEditor: false,
68-
defaultModelRendering: 'schema',
69-
showRequestHeaders: true
70-
});
71-
72-
window.swaggerUi.load();
41+
if (data.id) {
42+
const inputId = op.querySelector('.parameters input[placeholder="id"]').value = data.id;
43+
inputId.dispatchEvent(new Event('input', { bubbles: true }));
44+
}
7345

74-
function log() {
75-
if ('console' in window) {
76-
console.log.apply(console, arguments);
77-
}
46+
for (let input of op.querySelectorAll('.parameters input')) {
47+
if (input.placeholder in data.queryParameters) {
48+
input.value = data.queryParameters[input.placeholder];
49+
input.dispatchEvent(new Event('input', { bubbles: true }));
50+
}
7851
}
79-
});*/
52+
53+
op.querySelector('.execute').click();
54+
op.scrollIntoView();
55+
});
56+
57+
observer.observe(document, {childList: true, subtree: true});
58+
};

0 commit comments

Comments
 (0)