-
Notifications
You must be signed in to change notification settings - Fork 90
AcceptableViewModelSelector returns wrong View Model #194
Comments
PS:
Possibly related to #16 ? |
I added this test to the zend-mvc test suite: public function testAppropriateFallbackOrderTestCase1()
{
$arr = [
'Zend\View\Model\JsonModel' => ['application/json'],
'Zend\View\Model\FeedModel' => ['application/rss+xml'],
];
$header = Accept::fromString('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
$this->request->getHeaders()->addHeader($header);
$plugin = $this->plugin;
$result = $plugin($arr);
$this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
$this->assertNotInstanceOf('Zend\View\Model\FeedModel', $result);
$this->assertNotInstanceOf('Zend\View\Model\JsonModel', $result);
} and it does fail because
As those match none of the explicit types specified in the Accept header, it comes down to that last piece So, this is caused by AcceptableViewModelSelector not adding the default view model with content type
It would work (as your Test Case 3 above shows). TL;DR: when specifying a list of acceptable view models, specify every acceptable one and don't rely on the fallback to render the HTML view. It can't be added automatically to the list as there might be cases where HTML isn't an acceptable return and you'd then have to opt-out of that by setting a different default. My recommendation is to update the docs for AcceptableViewModelSelector to make it clear that when specifying a list of acceptable models you must specify every acceptable type. |
In reality every browser appends
|
Essentially, yes. The default is used in only two cases:
However 2 is moot if the Accept header has a |
I tried using the AcceptableViewModelSelector with this Accept-Header in my request (Its the standard header for HTML in FF):
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Expectation:
It should return "ViewModel" with the given Accept-Header and it should return a JsonModel with "application/json" in my Accept-Header.
Reality:
Testcase 1:
Returns
Testcase 2:
Returns
Testcase 3:
Returns
Observation
It seems like the fallback to defaultViewModel does not work and it'll returns the first Model it gets if no match was successfull.
The text was updated successfully, but these errors were encountered: