Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove subclass parameter when generating sonata_admin_short_object_information #7845

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
);
}
}