Skip to content

Commit

Permalink
do not overwrite explicit templates, fix issues with redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
lsmith77 authored and GuilhemN committed Dec 28, 2015
1 parent 9186978 commit 2892cd2
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 2 deletions.
5 changes: 4 additions & 1 deletion EventListener/ViewResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use JMS\Serializer\SerializationContext;
use FOS\RestBundle\View\View;
use FOS\RestBundle\Util\Codes;
use FOS\RestBundle\View\ViewHandlerInterface;

/**
* The ViewResponseListener class handles the View core event as well as the "@extra:Template" annotation.
Expand Down Expand Up @@ -99,11 +100,13 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
$vars = $request->attributes->get('_template_default_vars');
}

/** @var ViewHandlerInterface $viewHandler */
$viewHandler = $this->container->get('fos_rest.view_handler');

if ($viewHandler->isFormatTemplating($view->getFormat())) {
if (!empty($vars)) {
$parameters = (array) $viewHandler->prepareTemplateParameters($view);

foreach ($vars as $var) {
if (!array_key_exists($var, $parameters)) {
$parameters[$var] = $request->attributes->get($var);
Expand All @@ -113,7 +116,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
}

$template = $request->attributes->get('_template');
if ($template) {
if ($template && !$view->getTemplate()) {
if ($template instanceof TemplateReference) {
$template->set('format', null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Controller\Annotations\View;
use FOS\RestBundle\Controller\FOSRestController;

/**
* @RouteResource("Article")
*/
class ArticleController extends FOSRestController
{
/**
* Get list.
*
* @param Request $request
*
* @return View view instance
*
* @View()
*/
public function cgetAction(Request $request)
{
$view = $this->view();
$view->setTemplate('TestBundle:Article:foo.html.twig');

return $view;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ test_param_fetcher:
test_param_fetcher_test:
path: /params/test
defaults: { _controller: TestBundle:ParamFetcher:test }

test_view_response_listener:
resource: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\ArticleController
type: rest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fooo
27 changes: 27 additions & 0 deletions Tests/Functional/ViewResponseListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Tests\Functional;

class ViewResponseListenerTest extends WebTestCase
{
public function testTemplateOverride()
{
$client = $this->createClient(array('test_case' => 'ViewResponseListener'));
$client->request(
'GET',
'/articles'
);

$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertContains('fooo', $client->getResponse()->getContent());
}
}
17 changes: 17 additions & 0 deletions Tests/Functional/app/ViewResponseListener/bundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return array(
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new \FOS\RestBundle\FOSRestBundle(),
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(),
);
18 changes: 18 additions & 0 deletions Tests/Functional/app/ViewResponseListener/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
imports:
- { resource: ../config/default.yml }

framework:
serializer:
enabled: true

fos_rest:
view:
view_response_listener: 'force'
formats:
xml: true
json: true
templating_formats:
html: true
format_listener:
rules:
- { path: ^/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true }
3 changes: 2 additions & 1 deletion View/ViewHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
use Symfony\Component\Form\FormInterface;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use FOS\RestBundle\Util\Codes;

/**
Expand Down Expand Up @@ -430,7 +431,7 @@ public function createResponse(View $view, Request $request, $format)
{
$route = $view->getRoute();
$location = $route
? $this->getRouter()->generate($route, (array) $view->getRouteParameters(), true)
? $this->getRouter()->generate($route, (array) $view->getRouteParameters(), UrlGeneratorInterface::ABSOLUTE_URL)
: $view->getLocation();

if ($location) {
Expand Down

0 comments on commit 2892cd2

Please sign in to comment.