Skip to content

Commit 0c21d0a

Browse files
committed
Merge branch '2.x' into 3.0
* 2.x: fix RequestBodyParamConverter tests Fix requests without content type do not implement (possibly) not available interface improve route loader upgrade instructions
2 parents 2554a05 + 0689da4 commit 0c21d0a

File tree

5 files changed

+80
-15
lines changed

5 files changed

+80
-15
lines changed

CHANGELOG.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ CHANGELOG
121121
* `FOS\RestBundle\View\View`
122122
* `FOS\RestBundle\View\ViewHandler`
123123

124+
2.8.1
125+
-----
126+
127+
* `FlattenExceptionNormalizer` does no longer implement the `CacheableSupportsMethodInterface` to
128+
ensure compatibility with older versions of the Symfony Serializer component
129+
124130
2.8.0
125131
-----
126132

@@ -150,8 +156,34 @@ CHANGELOG
150156
routing_loader: false
151157
```
152158

153-
You need to configure your routes explicitly or consider using the
154-
[RestRoutingBundle](https://github.com/handcraftedinthealps/RestRoutingBundle).
159+
You need to configure your routes explicitly, e.g. using the Symfony Core annotations or the FOSRestBundle
160+
shortcuts like `FOS\RestBundle\Controller\Annotations\Get`. You can use
161+
`bin/console debug:router --show-controllers` to help with the migration and compare routes before and after it.
162+
Change the routing loading:
163+
164+
Before:
165+
```
166+
Acme\Controller\TestController:
167+
type: rest
168+
resource: Acme\Controller\TestController
169+
```
170+
171+
After:
172+
```
173+
Acme\Controller\TestController:
174+
type: annotation
175+
resource: Acme\Controller\TestController
176+
```
177+
178+
When using the Symfony Core route loading, route names might change as the FOSRestBundle used a different naming
179+
convention. Mind the `.{_format}` suffix if you used the `fos_rest.routing_loader.include_format` option.
180+
181+
In case you have OpenAPI/Swagger annotations, you can also use [OpenAPI-Symfony-Routing](https://github.com/Tobion/OpenAPI-Symfony-Routing)
182+
which removes the need to have routing information duplicated. It also allows to add the `.{_format}` suffix automatically as before.
183+
184+
If migration to explicit routes is not possible or feasible, consider using
185+
[RestRoutingBundle](https://github.com/handcraftedinthealps/RestRoutingBundle) which extracted the auto-generation of routes
186+
in a BC way.
155187
* deprecated support for serializing exceptions, disable it by setting the `serialize_exceptions`
156188
option to false:
157189

Request/RequestBodyParamConverter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,16 @@ public function apply(Request $request, ParamConverter $configuration): bool
7676
}
7777
$this->configureContext($context = new Context(), $arrayContext);
7878

79+
$format = $request->getContentType();
80+
if (null === $format) {
81+
return $this->throwException(new UnsupportedMediaTypeHttpException(), $configuration);
82+
}
83+
7984
try {
8085
$object = $this->serializer->deserialize(
8186
$request->getContent(),
8287
$configuration->getClass(),
83-
$request->getContentType(),
88+
$format,
8489
$context
8590
);
8691
} catch (UnsupportedFormatException $e) {

Serializer/Normalizer/FlattenExceptionNormalizer.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
use FOS\RestBundle\Util\ExceptionValueMap;
1515
use Symfony\Component\ErrorHandler\Exception\FlattenException;
1616
use Symfony\Component\HttpFoundation\Response;
17-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
1817
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1918

2019
/**
2120
* @author Christian Flothmann <[email protected]>
2221
*
2322
* @internal
2423
*/
25-
final class FlattenExceptionNormalizer implements CacheableSupportsMethodInterface, NormalizerInterface
24+
final class FlattenExceptionNormalizer implements NormalizerInterface
2625
{
2726
private $messagesMap;
2827
private $debug;
@@ -76,9 +75,4 @@ public function supportsNormalization($data, $format = null)
7675
{
7776
return $data instanceof FlattenException;
7877
}
79-
80-
public function hasCacheableSupportsMethod(): bool
81-
{
82-
return true;
83-
}
8478
}

Tests/Request/RequestBodyParamConverterTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function testRequestAttribute()
134134
->expects($this->once())
135135
->method('deserialize')
136136
->willReturn('Object');
137-
$request = $this->createRequest();
137+
$request = $this->createRequest(null, 'application/json');
138138
$this->launchExecution($converter, $request);
139139
$this->assertEquals('Object', $request->attributes->get('foo'));
140140
}
@@ -155,7 +155,7 @@ public function testValidatorParameters()
155155

156156
$converter = new RequestBodyParamConverter($this->serializer, null, null, $validator, 'errors');
157157

158-
$request = $this->createRequest();
158+
$request = $this->createRequest(null, 'application/json');
159159
$configuration = $this->createConfiguration('FooClass', null, ['validator' => ['groups' => ['foo']]]);
160160
$this->launchExecution($converter, $request, $configuration);
161161
$this->assertEquals('fooError', $request->attributes->get('errors'));
@@ -175,7 +175,7 @@ public function testValidatorSkipping()
175175

176176
$converter = new RequestBodyParamConverter($this->serializer, null, null, $validator, 'errors');
177177

178-
$request = $this->createRequest();
178+
$request = $this->createRequest(null, 'application/json');
179179
$configuration = $this->createConfiguration('FooClass', null, ['validate' => false]);
180180
$this->launchExecution($converter, $request, $configuration);
181181
$this->assertNull($request->attributes->get('errors'));
@@ -248,6 +248,14 @@ public function testSupportsWithNoClass()
248248
$this->assertFalse($converter->supports($this->createConfiguration(null, 'post')));
249249
}
250250

251+
public function testNoContentTypeCausesUnsupportedMediaTypeException()
252+
{
253+
$converter = new RequestBodyParamConverter($this->serializer);
254+
$request = $this->createRequest();
255+
$this->expectException(UnsupportedMediaTypeHttpException::class);
256+
$this->launchExecution($converter, $request);
257+
}
258+
251259
protected function launchExecution($converter, $request = null, $configuration = null)
252260
{
253261
if (null === $request) {

UPGRADING-3.0.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,34 @@ Upgrading From 2.x To 3.0
99
routing_loader: false
1010
```
1111
12-
You need to configure your routes explicitly or consider using the
13-
[RestRoutingBundle](https://github.com/handcraftedinthealps/RestRoutingBundle).
12+
You need to configure your routes explicitly, e.g. using the Symfony Core annotations or the FOSRestBundle
13+
shortcuts like `FOS\RestBundle\Controller\Annotations\Get`. You can use
14+
`bin/console debug:router --show-controllers` to help with the migration and compare routes before and after it.
15+
Change the routing loading:
16+
17+
Before:
18+
```
19+
Acme\Controller\TestController:
20+
type: rest
21+
resource: Acme\Controller\TestController
22+
```
23+
24+
After:
25+
```
26+
Acme\Controller\TestController:
27+
type: annotation
28+
resource: Acme\Controller\TestController
29+
```
30+
31+
When using the Symfony Core route loading, route names might change as the FOSRestBundle used a different naming
32+
convention. Mind the `.{_format}` suffix if you used the `fos_rest.routing_loader.include_format` option.
33+
34+
In case you have OpenAPI/Swagger annotations, you can also use [OpenAPI-Symfony-Routing](https://github.com/Tobion/OpenAPI-Symfony-Routing)
35+
which removes the need to have routing information duplicated. It also allows to add the `.{_format}` suffix automatically as before.
36+
37+
If migration to explicit routes is not possible or feasible, consider using
38+
[RestRoutingBundle](https://github.com/handcraftedinthealps/RestRoutingBundle) which extracted the auto-generation of routes
39+
in a BC way.
1440

1541
* Support for serializing exceptions has been removed. Setting the
1642
`fos_rest.exception.serialize_exceptions` option to anything else than `false` leads to an exception.

0 commit comments

Comments
 (0)