Skip to content

Commit

Permalink
temporary remove subclass query parameter in GetShortObjectDescriptio…
Browse files Browse the repository at this point in the history
…nAction
  • Loading branch information
mpoiriert committed Jun 15, 2022
1 parent f9d6325 commit c0e9eb3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Action/GetShortObjectDescriptionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,21 @@ public function __invoke(Request $request): Response
throw new BadRequestParamHttpException('objectId', ['string', 'int'], $objectId);
}

$object = $admin->getObject($objectId);
if (null === $object) {
throw new NotFoundHttpException(sprintf('Could not find subject for id "%s"', $objectId));
// If the subclass parameter is present it can cause conflict with other admin.
// The admin do not need subclass parameter to load an existing object.
$subclass = $request->query->get('subclass');
$request->query->remove('subclass');

try {
$object = $admin->getObject($objectId);
if (null === $object) {
throw new NotFoundHttpException(sprintf('Could not find subject for id "%s"', $objectId));
}
} finally {
// Restore the subclass if present to reduce impact of the parameter removal above.
if (null !== $subclass) {
$request->query->set('subclass', $subclass);
}
}

if ('json' === $request->get('_format')) {
Expand Down
31 changes: 31 additions & 0 deletions tests/Action/GetShortObjectDescriptionActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,35 @@ public function testGetShortObjectDescriptionActionObjectAsJson(): void

static::assertSame('{"result":{"id":"42","label":"bar"}}', $response->getContent());
}

public function testGetShortObjectDescriptionActionSubclassQueryParameterTemporaryRemoved(): void
{
$request = new Request([
'_sonata_admin' => 'sonata.post.admin',
'objectId' => 42,
'uniqid' => 'asdasd123',
'subclass' => $subclass = uniqid('subclass'),
'_format' => 'json',
]);
$object = new \stdClass();

$this->adminFetcher->method('get')->willReturn($this->admin);

$this->admin->method('id')->with($object)->willReturn('42');
$this->admin->method('getObject')->with(42)->willReturnCallback(static function () use ($object, $request) {
static::assertFalse($request->query->has('subclass'), 'subclass query parameter should be removed at this stage');

return $object;
});

$this->admin->method('toString')->with($object)->willReturn('bar');

($this->action)($request);

static::assertSame(
$subclass,
$request->query->get('subclass'),
'subclass query parameter should be restored at this stage'
);
}
}

0 comments on commit c0e9eb3

Please sign in to comment.