Skip to content

Commit d4ebcc9

Browse files
committed
Set documentation endpoint type to all endpoints
1 parent e5a9af8 commit d4ebcc9

File tree

7 files changed

+18
-4
lines changed

7 files changed

+18
-4
lines changed

features/security/validate_incoming_content-types.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ Feature: Validate incoming content type
1313
"""
1414
Then the response status code should be 406
1515
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
16-
And the JSON node "hydra:description" should be equal to 'The content-type "text/plain" is not supported. Supported MIME types are "application/ld+json", "application/hal+json", "application/vnd.api+json", "application/xml", "text/xml", "application/json", "text/html".'
16+
And the JSON node "hydra:description" should be equal to 'The content-type "text/plain" is not supported. Supported MIME types are "application/ld+json", "application/hal+json", "application/vnd.api+json", "application/xml", "text/xml", "application/json".'

src/Bridge/Symfony/Bundle/EventListener/SwaggerUiListener.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ public function onKernelRequest(GetResponseEvent $event)
3434

3535
$request->attributes->set('_controller', 'api_platform.swagger.action.ui');
3636
}
37+
38+
public function addEndpointTypeToRequest(GetResponseEvent $event)
39+
{
40+
$request = $event->getRequest();
41+
$request->attributes->set('_api_endpoint_type', 'documentation');
42+
}
3743
}

src/Bridge/Symfony/Bundle/Resources/config/routing/docs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<route id="api_doc" path="/docs.{_format}">
99
<default key="_controller">api_platform.action.documentation</default>
1010
<default key="_api_respond">1</default>
11+
<default key="_api_endpoint_type">documentation</default>
1112
<default key="_format" />
1213
</route>
1314

src/Bridge/Symfony/Bundle/Resources/config/swagger-ui.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
<services>
88

99
<service id="api_platform.swagger.listener.ui" class="ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\SwaggerUiListener">
10-
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" />
10+
<tag name="kernel.event_listener" event="kernel.request" method="addEndpointTypeToRequest" priority="8" />
11+
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="0" />
1112
</service>
1213

1314
</services>

src/EventListener/AddFormatListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function onKernelRequest(GetResponseEvent $event)
8888
return;
8989
}
9090

91-
throw $this->getNotAcceptableHttpException($mimeType);
91+
throw $this->getNotAcceptableHttpException($mimeType ?? $requestFormat);
9292
}
9393

9494
// Finally, if no Accept header nor Symfony request format is set, return the default format

tests/EventListener/AddFormatListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function testSupportedDocumentationAcceptHeader()
204204

205205
/**
206206
* @expectedException \Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException
207-
* @expectedExceptionMessage Requested format "application/ld+json" is not supported. Supported MIME types are "application/json".
207+
* @expectedExceptionMessageRegExp /^Requested format "(jsonld|application\/ld\+json)" is not supported. Supported MIME types are "application\/json".$/
208208
*/
209209
public function testUnsupportedDocumentationRequestFormat()
210210
{

tests/Fixtures/app/config/config_test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ api_platform:
3333
jsonapi: ['application/vnd.api+json']
3434
xml: ['application/xml', 'text/xml']
3535
json: ['application/json']
36+
documentation_formats:
37+
jsonld: ['application/ld+json']
38+
jsonhal: ['application/hal+json']
39+
jsonapi: ['application/vnd.api+json']
40+
xml: ['application/xml', 'text/xml']
41+
json: ['application/json']
3642
html: ['text/html']
3743
error_formats:
3844
jsonproblem: ['application/problem+json']

0 commit comments

Comments
 (0)