Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[PoC][API] Resolve proper IRI in admin/shop contexts#11925

Merged
lchrusciel merged 15 commits into
Sylius:masterfrom
Tomanhez:iri-converter-spike
Nov 12, 2020
Merged

[PoC][API] Resolve proper IRI in admin/shop contexts#11925
lchrusciel merged 15 commits into
Sylius:masterfrom
Tomanhez:iri-converter-spike

Conversation

@Tomanhez
Copy link
Copy Markdown
Contributor

@Tomanhez Tomanhez commented Oct 13, 2020

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
License MIT

This PR is first step to improve IRI that are return in responses on shop and admin endpoints.
First problem was IriConverter that was returning bad IRI because RouteNameResolver return first matching route without aware who was called - admin or shop. It was working bad because we break all path into two prefixes (before all entities had one path).
Second problem is resources configuration: All IRI-s have to proper paths on other sites: admin and shop.
My solution:

  • RouteNameResolver looks for not the first occurrence, but all matching routes and match it by current prefix.
  • Added necessary configuration for resources to get all proper IRI-s and routes on shop and admin contexts

To complete this PR, we need cover all occurs IRI in entities that have path on two contexts:

  • Product
  • ProductVariants
  • Translations
  • Images
  • ProductTaxon
  • MainTaxon
  • ProductReview
  • ProductOptions
  • Order
  • Channel
  • Customer
  • Payment
  • Shipment
  • OrderItem
    maybe I forgot about one

Example:
Before
Screenshot 2020-10-16 at 11 26 20

After:
Screenshot 2020-10-16 at 11 23 25

@Tomanhez Tomanhez requested a review from a team as a code owner October 13, 2020 14:09
@Tomanhez Tomanhez changed the title [Api][Spike] Iri converter [Api]Iri converter Oct 13, 2020
@Tomanhez Tomanhez force-pushed the iri-converter-spike branch 2 times, most recently from aa82aa2 to f32016f Compare October 14, 2020 11:16
@oallain
Copy link
Copy Markdown
Contributor

oallain commented Oct 14, 2020

Thanks @Tomanhez
I think, this is a part fix of #11893 💪

@Tomanhez Tomanhez force-pushed the iri-converter-spike branch 2 times, most recently from aab699a to c9ab309 Compare October 14, 2020 17:41
Comment thread src/Sylius/Bundle/ApiBundle/Resolver/RouteNameResolver.php Outdated
Comment thread src/Sylius/Bundle/ApiBundle/Resolver/RouteNameResolver.php Outdated
Comment thread src/Sylius/Bundle/ApiBundle/Resolver/CachedRouteNameResolver.php Outdated
@GSadee GSadee added API APIs related issues and PRs. RFC Discussions about potential changes or new features. labels Oct 15, 2020
@Tomanhez Tomanhez force-pushed the iri-converter-spike branch 3 times, most recently from aba9058 to 4723946 Compare October 15, 2020 06:39
@Tomanhez Tomanhez changed the title [Api]Iri converter [PoC][API] Resolve proper IRI in admin/shop contexts Oct 15, 2020
Comment thread src/Sylius/Behat/Service/Provider/RequestApiPathPrefixProvider.php Outdated
@Tomanhez Tomanhez force-pushed the iri-converter-spike branch 3 times, most recently from 1940564 to 17f3030 Compare October 16, 2020 08:45
@Tomanhez Tomanhez force-pushed the iri-converter-spike branch from 17f3030 to d896c11 Compare October 16, 2020 09:31
Comment thread src/Sylius/Bundle/ApiBundle/Provider/RequestApiPathPrefixProvider.php Outdated
Comment thread src/Sylius/Behat/Service/Provider/RequestApiPathPrefixProvider.php Outdated
@Tomanhez Tomanhez force-pushed the iri-converter-spike branch 2 times, most recently from 1bff68f to 2bf0ebe Compare October 19, 2020 13:16
Comment thread src/Sylius/Behat/Service/Provider/RequestApiPathPrefixProvider.php Outdated
Comment thread src/Sylius/Behat/Service/Provider/RequestApiPathPrefixProvider.php Outdated
Comment thread src/Sylius/Bundle/ApiBundle/spec/Provider/ApiPathPrefixProviderSpec.php Outdated
@GSadee GSadee force-pushed the iri-converter-spike branch from 2bf0ebe to 1afdc9f Compare November 11, 2020 11:39
@GSadee GSadee force-pushed the iri-converter-spike branch from 1afdc9f to 5ba4d7c Compare November 11, 2020 13:13
Comment thread src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/TaxonTranslation.xml Outdated
Comment thread src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/PaymentMethod.xml Outdated
Comment thread src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/OrderItem.xml Outdated
Comment thread src/Sylius/Behat/Context/Api/Shop/CartContext.php Outdated
Comment thread src/Sylius/Behat/Resources/config/services/api.xml Outdated
Comment thread src/Sylius/Behat/Resources/config/services/api.xml Outdated
@GSadee GSadee force-pushed the iri-converter-spike branch 2 times, most recently from 99b1835 to ef5675f Compare November 12, 2020 07:59
Comment on lines +52 to +57
* @When I try to see one of the items from the order placed by a customer :customer
*/
public function iTryToSeeOneOfTheItemsFromTheOrderPlacedByACustomer(): void
{
/** @var OrderInterface $order */
$order = $this->sharedStorage->get('order');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

customer is never checked in this context, we are checking the last order registered in shared storage. Perhaps adjusting of a step definition and/or asserting if the ordered belongs to customer should done.

}

/**
* @When I try to see one of the units from the order placed by a customer :customer
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same as above

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And in every customer aware endpoint

Comment on lines +52 to +58
if ($customer !== null && in_array('ROLE_USER', $user->getRoles(), true)) {
return $this->orderItemRepository->findOneByCustomer($id, $customer);
}

if ($user instanceof AdminUserInterface && in_array('ROLE_API_ACCESS', $user->getRoles(), true)) {
return $this->orderItemRepository->findOneBy(['id' => $id]);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we should extract this behavior to separate classes to make it easier to override.

}

if ($user instanceof AdminUserInterface && in_array('ROLE_API_ACCESS', $user->getRoles(), true)) {
return $this->shipmentRepository->findOneBy(['id' => $id]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
return $this->shipmentRepository->findOneBy(['id' => $id]);
return $this->shipmentRepository->find($id);

private function returnMatchingRouteName(
array $matchingRoutes,
string $operationType,
string$resourceClass
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
string$resourceClass
string $resourceClass

Comment on lines +38 to +52
$pathElements = array_values(array_filter(explode('/', $path)));

if ('/' . $pathElements[0] !== $this->apiRoute) {
return null;
}

if ($pathElements[1] === PathPrefixes::SHOP_PREFIX) {
return PathPrefixes::SHOP_PREFIX;
}

if ($pathElements[1] === PathPrefixes::ADMIN_PREFIX) {
return PathPrefixes::ADMIN_PREFIX;
}

return null;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we can reuse UriBasedSectionProvider instead of this construction. But it should be an idea for future improvements.

</itemOperations>

<subresourceOperations>
<subresourceOperation name="methods_get_subresource">
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
<subresourceOperation name="methods_get_subresource">
<subresourceOperation name="shop_get_shipping_methods_subresource">

or

Suggested change
<subresourceOperation name="methods_get_subresource">
<subresourceOperation name="shop_shipping_methods_subresource_get">

WDYT?

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.

Looks cool, but this subresource name couldn't be changed as it is an identifier of this operation. This is the only way I can change the operation's parameters for a subresource endpoint that is configured below

<property name="method">
    <subresource resourceClass="%sylius.model.shipping_method.class%" />
</property>

>
<class name="Sylius\Component\Core\Model\Address">
<attribute name="id">
<group>address:read</group>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

shop/admin?


class OrderItemRepository extends BaseOrderItemRepository implements OrderItemRepositoryInterface
{
public function findOneByCustomer($id, CustomerInterface $customer): ?OrderItemInterface
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
public function findOneByCustomer($id, CustomerInterface $customer): ?OrderItemInterface
public function findOneByIdAndCustomer($id, CustomerInterface $customer): ?OrderItemInterface

@lchrusciel lchrusciel merged commit 11dd149 into Sylius:master Nov 12, 2020
@lchrusciel
Copy link
Copy Markdown
Contributor

Thanks, Tomasz! 🥇

lchrusciel added a commit that referenced this pull request Nov 13, 2020
…om slug to code (GSadee)

This PR was merged into the 1.9-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | based on #11925
| License         | MIT


Commits
-------

58ce6bd [API][Product] Change identifier for shop endpoint from slug to code
lchrusciel added a commit that referenced this pull request Nov 13, 2020
…t contexts (GSadee)

This PR was merged into the 1.9-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | fixes after #11925
| License         | MIT


Commits
-------

f8a6101 [API] Fixes after PR with resolving proper iri in different contexts
lchrusciel added a commit to Sylius/Core that referenced this pull request Nov 13, 2020
…om slug to code (GSadee)

This PR was merged into the 1.9-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | based on Sylius/Sylius#11925
| License         | MIT


Commits
-------

58ce6bd3d4745cb281ed97374f2324fd9e0af66d [API][Product] Change identifier for shop endpoint from slug to code
lchrusciel added a commit to Sylius/Core that referenced this pull request Nov 13, 2020
…t contexts (GSadee)

This PR was merged into the 1.9-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | fixes after Sylius/Sylius#11925
| License         | MIT


Commits
-------

f8a6101be7004d97dc7d112dcebccb7a36deb381 [API] Fixes after PR with resolving proper iri in different contexts
AdamKasp added a commit that referenced this pull request Nov 16, 2020
…ces (GSadee)

This PR was merged into the 1.9-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | yes
| New feature?    | yes
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | after #11925
| License         | MIT


Commits
-------

4fbbbde [API] Add missing endpoints for address and country resources
pamil pushed a commit to Sylius/SyliusCoreBundle that referenced this pull request Nov 26, 2020
…om slug to code (GSadee)

This PR was merged into the 1.9-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | based on Sylius/Sylius#11925
| License         | MIT


Commits
-------

58ce6bd3d4745cb281ed97374f2324fd9e0af66d [API][Product] Change identifier for shop endpoint from slug to code
pamil pushed a commit to Sylius/SyliusCoreBundle that referenced this pull request Nov 26, 2020
…t contexts (GSadee)

This PR was merged into the 1.9-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | fixes after Sylius/Sylius#11925
| License         | MIT


Commits
-------

f8a6101be7004d97dc7d112dcebccb7a36deb381 [API] Fixes after PR with resolving proper iri in different contexts
pamil pushed a commit to Sylius/SyliusApiBundle that referenced this pull request Dec 23, 2020
…om slug to code (GSadee)

This PR was merged into the 1.9-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | based on Sylius/Sylius#11925
| License         | MIT


Commits
-------

58ce6bd3d4745cb281ed97374f2324fd9e0af66d [API][Product] Change identifier for shop endpoint from slug to code
pamil pushed a commit to Sylius/SyliusApiBundle that referenced this pull request Dec 23, 2020
…t contexts (GSadee)

This PR was merged into the 1.9-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | fixes after Sylius/Sylius#11925
| License         | MIT


Commits
-------

f8a6101be7004d97dc7d112dcebccb7a36deb381 [API] Fixes after PR with resolving proper iri in different contexts
pamil pushed a commit to Sylius/SyliusApiBundle that referenced this pull request Dec 23, 2020
…ces (GSadee)

This PR was merged into the 1.9-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | yes
| New feature?    | yes
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | after Sylius/Sylius#11925
| License         | MIT


Commits
-------

4fbbbde7eb6224f733d736dc4f712d4cb2bb9378 [API] Add missing endpoints for address and country resources
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API APIs related issues and PRs. RFC Discussions about potential changes or new features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants