PUT/POST nested entities (relations) without modifying them
Assume following uni-directional relation:
Product > many-to-many > Additive
Product has visibility on Additive via "additives"-property.
Endpoint additives: /additives/* Endpoint products: /products/*
- I want to modify the relation between Additive and Product via /products/* - endpoint (additives property of product).
- I do not want to modify Additive data via /products/* endpoint
- I do not want to create new additives via /products/* endpoint
- I want to embed Additives::number when fetching from /products/* (both collection & item)
I know how to archive each of the above points. But the problem is when having it all together. At least I do not find any clue in the documentation, how to manage it, if there is any way.
The issue is, that on an API you want to be able to GET a resource, make modification and PUT it back for example.
- Now if you make both denormalization and normalization equal, you break 2. & 3. because when you embed it, you would also be able to write it back or even create new additives, providing the validation succeed.
- When i use a normalization group which includes the Additive::number, but a denormalization group, which equals, except Additive::number property it also does not work, because in that case a plain IRI-collection is expected. But when you fetch embedded data, you have something like that:
[{"@id": "IRI_1", "more": "more"}, {"@id": "IRI_2", "more": "more"}]
And this is expected:
["IRI_1", "IRI_2"]
This breaks the concept of GET & PUT back.
An alternative would be to create a /products/{id}/additives resource, but currently its an issue-driven way, because its not backed by the "Bundle" and certain things get fiddly.
Very interesting. The following works, but renders the nested Entity completely modifiable.
Basic example:
Additive:
attributes:
@id:
groups: [ 'product.write' ]
number:
groups: [ 'product.read' ]
Product:
attributes:
title:
groups: [ "product.read", "product.write" ]
additives:
groups: [ "product.read", "product.write" ]
resource.product:
parent: "api.resource"
arguments: [ Product ]
calls:
- method: "initNormalizationContext"
arguments: [ { groups: [ "product.read" ] } ]
- method: "initDenormalizationContext"
arguments: [ { groups: [ "product.write" ] } ]
tags: [ { name: "api.resource" } ]
At least now both works:
["/additives/1", "/additives/2"]
and:
[{"@id": "/additives/1"}, {"@id": "/additives/2"}]
But:
[{"@id": "/additives/1", "number": 6}, {"@id": "/additives/2"}]
Would also modify /additives/1.
Yes this is intended, here is the doc: https://api-platform.com/doc/1.0/api-bundle/serialization-groups-and-relations
I'v read the documentation completely. I know how the groups work. The issue is, that if a resource is not owned by only one another resource it does not make much sense to modify it via endpoint of another resource. Currently, if you want to embed information for performance reasons, you automatically enables the modification of a resource over the endpoint of another resource.
This allows weird things like modifying resource {"@id": "/additives/1", "number": 5} this way:
PUT /products/2
{
"@id": "/products/2",
"title": "Product A",
"additives": [
{"@id": "/additives/1", "number": 10}
]
}
Current solution is to not embed it and fetch each Additive related to each Product (when required) in a separate request. In a table displaying 10 or 20 products this means 10 to 20 API hits each time the data has to be updated - e. G. using applying filters, etc.
Do you agree that the way to modify a resource, as mentioned in previous post, is not necessary desired? Currently, my thoughts tell me, that its weird and should not be possible - or it should be possible to prohibit it, by e. G. not having the field(s) in denormalization group(s). This would mean, that both IRI and object, with @id should be accepted, but only fields written, which are in denormalization group.
Otherwise you could not for example GET and then PUT back resources, after modifications, without having to rewrite related/embedded resources on the client side. Alternative: not using the embedded data feature or live with the fact, that your resources can be modified via post & put on different resources or hack around and going the obstacle course to make it work as intended.
Since I'm rather new in this area (ws API, linked data), I'm open to any "fix" of false beliefs :)
@akomm Ok I think I understand the problem. You want to have embedded documents in read, be able to send the same embedded document in write (and not just it's IRI) but without modifying it. It's currently not possible but it should be possible to fix the ItemNormalizer easily to allow both IRI and embedded documents to be used in the case where write of the embedded relation is deactivated.
I'm :+1: to merge this feature if someone contribute it.
I agree that this is a very helpful feature. In my project, the frontend needs to take care not to modify any related entities.
Here's my use case:
- I retrieve a Part, which has 1:n relations to manufacturers and distributors
- Due to the current design (and due to performance reasons), manufacturers and distributor relations are normalized (and not passed via the IRI)
- The frontend denormalizes the data. In theory, the user cannot modify the manufacturer and distributor data, just add and delete new relations.
- Upon save, the frontend simply pushes all data back to the API, with any changes which may have occured in the manufacturers and distributors
However, that feature is probably not that easy to implement. Here are my thoughts:
The simple solution
Adding a new annotation to entities which defines if a relation may be an embedded relation or an IRI only, like so:
class Product {
/**
* @ORM\ManyToOne(targetEntity="Manufacturer")
* @Groups{"default"})
* @DisallowWriteEmbeddedRelation (name is IMHO bad, better annotation name required)
*/
private $manufacturer;
}
However, the question is how the system should react to DisallowWriteEmbeddedRelation. Should it throw an Exception if data was changed in the embedded relation, silently ignore it or should the behavior be configurable?
Also, this approach is global, so it can't be changed on a per-resource context.
The more advanced solution
A more flexible solution would be to use entity addressing. The developer could define a path of entities to define what's allowed and what's not. I already use an entity addressing method in PartKeepr to search and filter for any property within an object hierarchy.
Here's an example to illustrate addressing:
class Product {
/**
* @ORM\OneToMany(targetEntity="ProductManufacturer",mappedBy="product", orphanRemoval=true)
* @Groups{"default"})
*/
private $manufacturers;
}
class ProductManufacturer {
/**
* @ORM\ManyToOne(targetEntity="Product",inversedBy="manufacturers")
*/
private $product;
/**
* @ORM\ManyToOne(targetEntity="Manufacturer")
*/
private $manufacturer;
private $partNumber;
}
class Manufacturer {
private $name;
}
If I was about to define an API based on the product, I want the user to add/delete ProductManufacturers and the partNumber but not the Manufacturers themselves.
From an API design perspective I would rather not configure the way the manufacturer property on ProductManufacturer itself, as I don't know yet if there are additional requirements in the API development later.
Instead, I'd like to restrict embedded relation writes to Product.ProductManufacturer.Manufacturer from a Product API entrypoint only, like so:
class Product {
/**
* @ORM\OneToMany(targetEntity="ProductManufacturer",mappedBy="product", orphanRemoval=true)
* @Groups{"default"})
* @DisallowWriteEmbeddedRelation{"manufacturers.manufacturer"}
*/
private $manufacturers;
}
The addressing system could also be used to define access to individual properties.
Any feedback is appreciated.
@felicitus So basically a blacklist of (nested) property paths? Perhaps this belongs in Symfony Serializer?
What do you think, @dunglas?
Hello,
It's unclear if this has been solved. Could you redirect me to the corresponding PR if there is one ? Thank you.
I don't think so. I did not follow this anymore.
After a lot of wasted time trying to solve issues which arise with REST, I came to the conclusion, that it is a concept, which is nice in theory, but unpractical in a lot of use cases. This leads to accidental complexity. So I switched to GraphQL backend and issues like that do not even arise to be "fixed". I still use REST, where it works well. Like JWT Auth, binary upload/fetch, linked from GraphQL data. But most of by API is GraphQL.
REST ideas are interesting, and jsonld too. I just do think, that people try to solve things for which it (currently) is not well designed. If I have a situation where I can use it properly, I will do it without hesitation.