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

Skip to content

allow developer to specify iri for operations#2213

Closed
videni wants to merge 10 commits into
api-platform:masterfrom
videni:master
Closed

allow developer to specify iri for operations#2213
videni wants to merge 10 commits into
api-platform:masterfrom
videni:master

Conversation

@videni

@videni videni commented Sep 25, 2018

Copy link
Copy Markdown
Q A
Bug fix? no
New feature? yes
BC breaks? no
License MIT
Doc PR api-platform/docs#1234

now we can specify IRI for individual operation, this PR resolves issue 839 I created, we should let the developer decide what IRI is used for resource.

App\Bundle\OrderBundle\Entity\Order:
    collectionOperations:
        post:
            path: '/admin/orders'
            denormalization_context:
               groups: ["write"]
            access_control: is_granted('app_order_create')
        get:
            path: '/admin/orders'
            force_eager: false
            access_control: is_granted('app_order_view')
            iri:
                route: 'api_orders_get_item'
                parameters:
                    id: expr(object.getId())
        show_customer_orders:
            method: 'GET'
            force_eager: false
            path: /shop/account/orders
            iri:
                route: 'api_shop_account_order_view'
                parameters:
                    id: expr(object.getId())
    itemOperations:
        get:
            path: '/admin/orders/{id}'
            acl_enabled: true
            access_control: is_granted('app_order_view', object)
            iri:
                route: 'api_orders_get_item'
                parameters:
                    id: expr(object.getId())
        delete:
            path: '/admin/orders/{id}'
            access_control: is_granted('app_order_delete', object)
        show_customer_order_details:
            method: 'GET'
            route_name: 'api_shop_account_order_view'
            access_control: "object.getCustomer() === user"
    attributes:
        normalization_context:
            groups: ["Details", 'Default']
        denormalization_context:
            groups: ["write"]
        validation_groups:  ["app"]

* @throws RuntimeException
*/
public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string;
public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH, $context = []): string;

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.

We can't change this interface it breaks the backward compatibility :/.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

hi, there is a default value, it should not break the compatibility, can you explain it further ?

@soyuka soyuka Sep 25, 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.

Sure, we respect the symfony backward compatibility promise and we can't add arguments:

https://symfony.com/doc/current/contributing/code/bc.html#changing-interfaces (see the Add argument with a default value => No).

Don't worry about this yet, I'm sure there's a solution that doesn't involve adding context here!

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.

A solution is to add a new interface extending the existing one. See https://github.com/api-platform/core/blob/master/src/DataProvider/ContextAwareCollectionDataProviderInterface.php#L32 for instance.

@soyuka soyuka left a comment

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.

I can definitely see an interest in this 👍. We may not need an expression evaluator though, I think that a simple properties array would be enough and would maintain consistency with our filters for example.

        get:
            path: '/admin/orders'
            force_eager: false
            access_control: is_granted('app_order_view')
            iri:
                route: 'api_orders_get_item'
                properties: ['id'] # will take $order->getId() through the PropertyAccessor 

<argument type="collection"/>
<call method="setContextVariable">
<argument>container</argument>
<argument type="service" id="service_container"/>

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 we avoid using a setter and especially injecting the service container?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

you are right , the PropertyAccessor is enough for current case

<argument type="service" id="api_platform.identifier.converter" on-invalid="ignore" />
<call method="setExpressionEvaluator">
<argument type="service" id="api_platform.expression.evaluator" />
</call>

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.

same, we should prefer direct injection, but there's an issue with BC break again.

@videni

videni commented Sep 26, 2018

Copy link
Copy Markdown
Author

when serialize object has object properties like an Order object has a property payment who is also an object. this PR won't work, because the resolved IRI for order is also passed into payment. the IRI is indeed a problem, see Please let us to decide if an entity it is dereferencable or not, don't fore us to do this, it only work when object has no properties that are managed by api-platform .

@dunglas

dunglas commented Oct 1, 2018

Copy link
Copy Markdown
Member

It looks like this PR has been mangled with your fork. Can you restore only the appropriate changes to ease the review @videni?

For the record, I'm 👍 to allow "internal" IRI (urn for instance).

@videni videni closed this Oct 11, 2018
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.

don't force use to specify item route for an entity

4 participants