Closed
Description
I would like to send generated ID from UI to API but right now it is not possible. API is trying to do update, response is "Method is not allowed" So right now i'm sending it as uuid and then in the event doing extraction from request setting to entity.
It will be great to allow send ID param to POST and to have "optional" parameter for writing. What do you think?
Activity
Simperfit commentedon Jun 22, 2017
What do you mean ?
If you do
POST /something
withAnd the setters exists (and the good groups if you use any), that should work.
akadlec commentedon Jun 22, 2017
Yes this, but this does not work, the result is:
Update is not allowed for this operation.
Simperfit commentedon Jun 25, 2017
Could you please paste the entity you are using ?
akadlec commentedon Jul 7, 2017
Problem is, when in
POST
isID
api-platform is thinking that i want to do anUPDATE
action.GonZOO82 commentedon Sep 12, 2017
Any idea?
akadlec commentedon Sep 12, 2017
From me, no, i am leaving this bundle and going back to other solution
dunglas commentedon Sep 12, 2017
It's weird to have a setter for an ID that is generated externally. I'm not sure to get what you try to achieve.
akadlec commentedon Sep 12, 2017
@dunglas it is normal to send ID from app. I case you have app with optimistic ui and all ID's are in UUID format, you could generate ID in you app and send it to api endpoint and you don't have to wait for positive response.
dunglas commentedon Sep 12, 2017
@akadlec I agree on that, but in this case you shouldn't have this line
@ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
.akadlec commentedon Sep 12, 2017
Right now i'm not 100% surre but i thing i did a test where i remove this row. Problem is not in doctrine mapping but in apiplatform. There is a method which check request and when an ID is present in request and request is in POST method, platform refuse it
yoshz commentedon Feb 8, 2018
This issue still exists for me and I only get the error message "Update is not allowed for this operation" if actually post an id that has the property name "id". For other entities that have an id on the property "code" for example I don't get this error message. So this is pretty weird.
Should this check not rely on that a IdGenerator is actually defined in Doctrine for that entity?
coudenysj commentedon Mar 30, 2018
Any news on this? Or ideas how to prepare a merge request to add support for this?
arnedesmedt commentedon Mar 30, 2018
+1
Toflar commentedon May 1, 2018
This issue still exists. I'm trying to fix it but I'm pretty lost and it's hard to fix it without guidance as I'm not sure why things are the way they are at several places in the code.
First of all, this issue should be moved to
core
(not sure about the policy here though).Then the issue occurs here: https://github.com/api-platform/core/blob/master/src/Serializer/ItemNormalizer.php#L35
OBJECT_TO_POPULATE
is not set at this place yet, it happens only later. Andapi_allow_update
isfalse
because it's aPOST
request and this variable is only set totrue
ifPATCH
orPUT
requests are sent (see https://github.com/api-platform/core/blob/master/src/Serializer/SerializerContextBuilder.php#L72).So I'm not sure how to proceed here but it's perfectly valid to send a
POST
request with anid
if you have no autogenerated strategy.10 remaining items
nfacciolo commentedon Aug 28, 2019
Seriously there is no way to [POST] an object with a specified
id
named "id" !?maks-rafalko commentedon Aug 28, 2019
no, we had to implement it ourselves (see my comment)
nfacciolo commentedon Aug 28, 2019
I tried your solution but it leads to this error:
Argument 10 passed to ApiPlatform\Core\Serializer\AbstractItemNormalizer::__construct() must be of the type array, null given, called in /usr/src/api/var/cache/dev/ContainerVNe5Qdr/srcApp_KernelDevDebugContainer.php on line 823
btw thanks for the code
maks-rafalko commentedon Aug 28, 2019
This is the full code of our class that allows positing custom
id
fields and works with2.4.3
api-platform:nfacciolo commentedon Aug 29, 2019
With the full code on api-platform version 2.4.6, it works for simple entities. But if there are nested entities, it does not work.
Thanks for the code and the time.
quentinus95 commentedon Dec 18, 2019
I have found out that posting with the
content-type
header set toapplication/ld+json
fixes the issue on my side.moay commentedon May 5, 2020
Thanks. We added a slight modification to allow for nested id references, which we automatically convert to iris.
Here is our adapted version:
https://gist.github.com/moay/47ef07b67d701c2ef7355d0bbba8b4d6
Renrhaf commentedon Jun 10, 2020
Same here with a custom POST action, using a custom input DTO object.
A key "id" is passed to identify some other object from the application.
With content type json/ld everything is fine as API platform is searching for a key "@id" but with simple json it fails with the error "Update is not allowed".
MGDSoft commentedon Jun 21, 2022
Thanks @maks-rafalko for the code it works perfectly in a simple entities
Its strange API platform can't do a simple insert by id 😞
Renrhaf commentedon Aug 1, 2023
Simplest solution : creating two decorators for Json & JsonLd item normalizers.
It seems that API Platform ElasticSearch component is already decorating the JsonLD ItemNormalizer (ApiPlatform\Elasticsearch\Serializer\ItemNormalizer), with a higher priority (-895). So we need to keep that in mind and put some higher priority on ours.
Code snippet
Normalizer for JsonLD requests :
Normalizer for Json requests :
trusek commentedon Apr 26, 2024
Context modifications can be done directly.
galliroberto commentedon Sep 26, 2024
I get
But I don't want to use a provider
noahsmyers commentedon Nov 25, 2024
In my case, I could write a Symfony Event Listener that would just set the content type to application/ld+json if application/json is detected, as the LD+JSON does not have this issue when supplying the $id property in POST requests.
app/src/EventListener/JsonToLdJsonListener.php
app/config/services.yaml