Skip to content

Commit 6ec796c

Browse files
committed
Merge pull request #920 from gou1/body-listener-format-fallback
BodyListener default format in case no Content-Type is provided
2 parents 7650034 + c2fe7c0 commit 6ec796c

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ private function addBodyListenerSection(ArrayNodeDefinition $rootNode)
168168
->addDefaultsIfNotSet()
169169
->canBeUnset()
170170
->children()
171+
->scalarNode('default_format')->defaultNull()->end()
171172
->booleanNode('throw_exception_on_unsupported_content_type')
172173
->defaultFalse()
173174
->end()

DependencyInjection/FOSRestExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public function load(array $configs, ContainerBuilder $container)
156156
$loader->load('body_listener.xml');
157157

158158
$container->setParameter($this->getAlias().'.throw_exception_on_unsupported_content_type', $config['body_listener']['throw_exception_on_unsupported_content_type']);
159+
$container->setParameter($this->getAlias().'.body_default_format', $config['body_listener']['default_format']);
159160
$container->setParameter($this->getAlias().'.decoders', $config['body_listener']['decoders']);
160161

161162
$arrayNormalizer = $config['body_listener']['array_normalizer'];

EventListener/BodyListener.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class BodyListener
2828
{
2929
private $decoderProvider;
3030
private $throwExceptionOnUnsupportedContentType;
31+
private $defaultFormat;
3132

3233
/**
3334
* @var ArrayNormalizerInterface
@@ -56,6 +57,16 @@ public function setArrayNormalizer(ArrayNormalizerInterface $arrayNormalizer)
5657
$this->arrayNormalizer = $arrayNormalizer;
5758
}
5859

60+
/**
61+
* Sets the fallback format if there's no Content-Type in the request.
62+
*
63+
* @param string $defaultFormat
64+
*/
65+
public function setDefaultFormat($defaultFormat)
66+
{
67+
$this->defaultFormat = $defaultFormat;
68+
}
69+
5970
/**
6071
* Core request handler.
6172
*
@@ -78,6 +89,8 @@ public function onKernelRequest(GetResponseEvent $event)
7889
? $request->getRequestFormat()
7990
: $request->getFormat($contentType);
8091

92+
$format = $format ?: $this->defaultFormat;
93+
8194
$content = $request->getContent();
8295

8396
if (!$this->decoderProvider->supports($format)) {

Resources/config/body_listener.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="10" />
3737
<argument type="service" id="fos_rest.decoder_provider" />
3838
<argument>%fos_rest.throw_exception_on_unsupported_content_type%</argument>
39+
<call method="setDefaultFormat">
40+
<argument>%fos_rest.body_default_format%</argument>
41+
</call>
3942
</service>
4043

4144
</services>

Resources/doc/configuration-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fos_rest:
6767
# Prototype
6868
name: ~
6969
body_listener:
70+
default_format: null
7071
throw_exception_on_unsupported_content_type: false
7172
decoders:
7273

0 commit comments

Comments
 (0)