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

Fix incorrect docs for subresource operations #779

Merged
merged 3 commits into from
Apr 4, 2019

Conversation

maks-rafalko
Copy link
Contributor

@maks-rafalko maks-rafalko commented Apr 2, 2019

We have just upgraded a big app from 2.3.6 to 2.4.2 and I think documentation is incorrect for subResources.

Without these changes, our tests were failing

  1. First of all, I think the new behavior in 2.4.2 is a BC break, because it did work before, but doesn't work after upgrading
  2. What we did is just replaced collectionOperations with subresourceOperations, pleasee see the simplified diff
# This is a subResource config

- collectionOperations:
+ subresourceOperations:
        api_services_service_products_get_subresource:
            normalization_context:
                groups: [...]

Why doesn't it work in 2.4.2 anymore?

Because the following code expects sub resources' configuration to be under subresourceOperations key:

https://github.com/api-platform/core/blob/ef76e6bc20ca0658a28c5ccccc1b406f47dbbec3/src/Metadata/Resource/ResourceMetadata.php#L179-L182

We have just upgraded a big app from `2.3.6` to `2.4.2` and I think documentation is incorrect for subResources.

Without these changes, our tests were failing

1. First of all, I think this is a BC break, because id *did* work before, but doesn't work after upgrading
2. What we did is just replaced `collectionOperations` with `subresourceOperations`, pleasee see the simplified diff

```diff
# This is a subResource config

- collectionOperations:
+ subresourceOperations:
            api_services_service_products_get_subresource:
                normalization_context:
                    groups: [...]
```

Why doesn't it work in `2.4.2` anymore?

Because the following code expects sub resources' configuration to be under `subresourceOperations` key:

https://github.com/api-platform/core/blob/ef76e6bc20ca0658a28c5ccccc1b406f47dbbec3/src/Metadata/Resource/ResourceMetadata.php#L179-L182
@teohhanhui
Copy link
Contributor

teohhanhui commented Apr 2, 2019

That does not seem correct.

subresourceOperations is used on the parent resource side (Question in the doc's example).

collectionOperations is used on the subresource side (Answer in the docs example).

There may well be a bug, but this is not it.

@maks-rafalko
Copy link
Contributor Author

maks-rafalko commented Apr 2, 2019

Then this is definitely a bug in the code.

Please look at the screenshot:

subresource-bug

  • App\Entity\ServiceProduct is a sub resource.
  • api_services_service_products_get_subresource is a route to get subresources (/api/services/{id}/service_products API endpoint)

As you can see, when the request is a request to get sub resources, the getSubresourceOperationAttribute() method is called, which, in its turn, fetches the data from subresourceOperations array, not collectionOperations

@teohhanhui
Copy link
Contributor

@borNfreee Could you share a stack trace please? Or add a Behat test. Thanks. 😄

@soyuka
Copy link
Member

soyuka commented Apr 3, 2019

This change looks correct to me.

@teohhanhui
Copy link
Contributor

teohhanhui commented Apr 3, 2019

XML config needs fixing.

We need to change this part too:

Because a subresource is nothing more than a collection operation, you can set normalization_context or denormalization_context on that operation. To do so, you need to override collectionOperations.

@teohhanhui
Copy link
Contributor

teohhanhui commented Apr 3, 2019

Also, @soyuka if I understand correctly, the operation names for subresource operations are weird... Those are route names, aren't they?

@soyuka
Copy link
Member

soyuka commented Apr 3, 2019

the operation names for subresource operations are weird... Those are route names, aren't they?

yes

Replace collectionOperations with subresourceOperations
@maks-rafalko
Copy link
Contributor Author

XML config needs fixing.

We need to change this part too:

Because a subresource is nothing more than a collection operation, you can set normalization_context or denormalization_context on that operation. To do so, you need to override collectionOperations.

done

@soyuka
Copy link
Member

soyuka commented Apr 4, 2019

Many thanks @borNfreee and sorry I forgot to update the docs when we fixed that bug!

@teohhanhui
Copy link
Contributor

@soyuka So the operation names are wrong, aren't they? Shouldn't we fix that? It's weird that the user has to use the route name...

@teohhanhui
Copy link
Contributor

Other than being weird, it also is a portability issue as we don't want to be tied to the Symfony Routing component.

@@ -376,7 +376,7 @@ If you put the subresource on a relation that is to-many, you will retrieve a co

Last but not least, subresources can be nested, such that `/questions/42/answer/comments` will get the collection of comments for the answer to question 42.

You may want custom groups on subresources. Because a subresource is nothing more than a collection operation, you can set `normalization_context` or `denormalization_context` on that operation. To do so, you need to override `collectionOperations`. Based on the above operation, because we retrieve an answer, we need to alter its configuration:
You may want custom groups on subresources. Because a subresource is nothing more than a collection operation, you can set `normalization_context` or `denormalization_context` on that operation. To do so, you need to override `subresourceOperations`. Based on the above operation, because we retrieve an answer, we need to alter its configuration:
Copy link
Contributor

Choose a reason for hiding this comment

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

Because a subresource is nothing more than a collection operation

This part needs to be removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I think this whole paragraph needs to be rewritten. I'll take care of it, if no one gets to it before me.

@maks-rafalko maks-rafalko deleted the patch-2 branch April 5, 2019 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants