api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.45k stars 882 forks source link

[Question] Check PUT payload #2507

Closed matlar83 closed 3 years ago

matlar83 commented 5 years ago

Hi, what is the best way to check the client payload in PUT operations?

I mean: PUT operation supports partial update of the Resource. In my EventSubscriber I need to know which Resource properties were changed and get the old values.

At the moment, I simply get the 'data' attribute of the request set by the ReadListener and extract the property of interest.

Is there a more robust way?

Thank you. Matteo

z31fbras commented 5 years ago

You can retrieve the previous instance through the entity manager: $previousInstance = $this->entityManager->getUnitOfWork()->getOriginalEntityData($newInstance);

getOriginalEntityData returns an array though, not an object ;)

What are you trying to do? Is it to check some values before committing the changes to the database?

matlar83 commented 5 years ago

Thank you very much for your hint, but this applies only to doctrine entities.

Yes, I need to check the values to perform some operations before committing the changes. For example, only if the client changed the user password, encode it.

Thank you

Il giorno lun 11 feb 2019 alle ore 11:11 z31fbras notifications@github.com ha scritto:

You can retrieve the previous instance through the entity manager: $previousInstance = $this->entityManager->getUnitOfWork()->getOriginalEntityData($newInstance);

getOriginalEntityData returns an array though, not an object ;)

What are you trying to do? Is it to check some values before committing the changes to the database?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/api-platform/core/issues/2507#issuecomment-462274909, or mute the thread https://github.com/notifications/unsubscribe-auth/Aomk7IqN-df6LMnWH9LfZvCiGUJoithZks5vMUHXgaJpZM4azwJ0 .

brunoric commented 5 years ago

Hi,

I'm also curious if this is intentional or a bug in API Platform. I would rather prefer to support partial updates in PATCH operations and keep PUT validating required fields (and other constraints) as POST operations.

I've opened this question in Stack Overflow: https://stackoverflow.com/questions/55743274/put-operations-are-accepting-requests-without-required-fields

Kind regards, Bruno

soyuka commented 3 years ago

Greetings! We appreciate your concern but weren't able to reproduce this issue or it is more of a question. As described in the API Platform contributing guide, we use GitHub issues for bugs and feature requests only.

For support question ("How To", usage advice, or troubleshooting your own code), you have several options:

Feel free reach one of the support channels above. In the meantime we're closing this issue.