Skip to content

bugfix: throw when a subresources id is not found#1819

Merged
soyuka merged 1 commit into
api-platform:2.2from
Simperfit:bug/throw-when-an-subresources-id-is-not-found
Apr 6, 2018
Merged

bugfix: throw when a subresources id is not found#1819
soyuka merged 1 commit into
api-platform:2.2from
Simperfit:bug/throw-when-an-subresources-id-is-not-found

Conversation

@Simperfit

@Simperfit Simperfit commented Apr 6, 2018

Copy link
Copy Markdown
Contributor
Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #1811
License MIT
Doc PR

This could go maybe agasint 2.0 since it has been introduced in 2.0, WDYT ?

@Simperfit Simperfit force-pushed the bug/throw-when-an-subresources-id-is-not-found branch from 878a35f to 5ab51da Compare April 6, 2018 09:43
* @throws RuntimeException
* @throws ItemNotFoundException
*/
public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the dataprovider's responsability to throw an ItemNotFoundException. To stay constistent with ItemDataProvider, please patch this inside the ReadListener instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@Simperfit Simperfit force-pushed the bug/throw-when-an-subresources-id-is-not-found branch 2 times, most recently from 2c725b4 to e3d891b Compare April 6, 2018 09:57
@soyuka

soyuka commented Apr 6, 2018

Copy link
Copy Markdown
Member

This could go maybe agasint 2.0 since it has been introduced in 2.0, WDYT ?

No, security patches only on 2.0.

Also, could you add a phpunit test expectedException on the ReadListenerTest? Tyvm <3.

@Simperfit Simperfit force-pushed the bug/throw-when-an-subresources-id-is-not-found branch from e3d891b to a5f3294 Compare April 6, 2018 10:44
@Simperfit

Copy link
Copy Markdown
Contributor Author

@soyuka Unit test added.

Comment thread src/EventListener/ReadListener.php Outdated
$data = $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'], $attributes['subresource_operation_name']);

if (null === $data) {
throw new ItemNotFoundException(sprintf('Object of type "%s" with identifiers %s could not be found', $attributes['resource_class'], implode(',', $identifiers)), 400);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we use the NotFoundHttpException here?

@Simperfit Simperfit Apr 6, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better too yeah.

$listener = new ReadListener($collectionDataProvider->reveal(), $itemDataProvider->reveal(), $subresourceDataProvider->reveal());
$listener->onKernelRequest($event->reveal());

$this->assertSame($data, $request->attributes->get('data'));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$data is not set...

@Simperfit Simperfit force-pushed the bug/throw-when-an-subresources-id-is-not-found branch 3 times, most recently from ff787d4 to 52fa4be Compare April 6, 2018 11:57
Comment thread src/EventListener/ReadListener.php Outdated
$data = $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'], $attributes['subresource_operation_name']);

if (null === $data) {
throw new NotFoundHttpException(sprintf('Object of type "%s" with identifiers %s could not be found', $attributes['resource_class'], implode(',', $identifiers)));

@meyerbaptiste meyerbaptiste Apr 6, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the message to keep consistency with this?

throw new NotFoundHttpException('Not Found');

@Simperfit Simperfit force-pushed the bug/throw-when-an-subresources-id-is-not-found branch from 52fa4be to 2a8ef27 Compare April 6, 2018 12:10
@Simperfit Simperfit force-pushed the bug/throw-when-an-subresources-id-is-not-found branch from 2a8ef27 to a65e09e Compare April 6, 2018 12:21
@soyuka soyuka merged commit 9943760 into api-platform:2.2 Apr 6, 2018
@soyuka

soyuka commented Apr 6, 2018

Copy link
Copy Markdown
Member

thanks @Simperfit

@Simperfit Simperfit deleted the bug/throw-when-an-subresources-id-is-not-found branch April 6, 2018 12:51
@lvillarino

Copy link
Copy Markdown

When this update will be release? Thanks

@soyuka

soyuka commented Jun 7, 2018

Copy link
Copy Markdown
Member

It should be in 2.2.6 and later

@lvillarino

lvillarino commented Jun 7, 2018

Copy link
Copy Markdown

The code is not on this release https://github.com/api-platform/core/releases/tag/v2.2.7

I download the zip file and the code is missing and missing from here too https://github.com/api-platform/core/blob/v2.2.7/src/EventListener/ReadListener.php

@bendavies

Copy link
Copy Markdown
Contributor

@lvillarino

Copy link
Copy Markdown

The variable $data is not null when you try to access a subresource ex: post/id/comments

I think that we have to update the line https://github.com/Simperfit/core/blob/a65e09e354df3f5f408234e0cf8eb1e4b3bbd18b/src/EventListener/ReadListener.php#L150 with this code
if (null === $data || 0 == $data->getTotalItems()) { throw new NotFoundHttpException('Not Found.'); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants