[DDC-2910] JSON Pointer and JSON Patch syntax support Created: 12/Jan/14  Updated: 12/Jan/14  Resolved: 12/Jan/14

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: Mapping Drivers, Tools
Affects Version/s: None
Fix Version/s: None
Security Level: All

Type: New Feature Priority: Major
Reporter: Kévin Dunglas Assignee: Marco Pivetta
Resolution: Won't Fix Votes: 0
Labels: hateoas, json, jsonapi, jsonpatch, jsonpointer, rest, rfc


 Description   

I plan to add support for JSON Pointer (RFC 6901: http://tools.ietf.org/html/rfc6901) and JSON Patch (RFC 6902: http://tools.ietf.org/html/rfc6902) syntaxes to Doctrine.

JSON Pointer and JSON Patch are useful to create Hypermedia REST API (HATEOAS).
Some higher level standards such as JSON API (http://jsonapi.org/) also rely on JSON Pointer and JSON Patch. My final thought is to implement JSON API support (serialization and update) on top of the Doctrine ORM.

The use case for the JSON pointer syntax is to access entity properties through an object graph.

Ex:

$jpa = new JsonPointerAccess();
$jpa->get($myEntityOrCollection, '/foo/0/bar'); # Get the value of $myEntityOrCollection->$foo[0]->bar
$jpa->set($myEntityOrCollection, '/foo/0/bar', 22); # Set the value of $myEntityOrCollection->$foo[0]->bar to 22

And for JSON Patch it's to modify a whole object graph.

$jpe = new JsonPatchExecuter();
$jpe->apply($myEntityOrCollection, '{ "op": "replace", "path": "/a/b/c", "value": 42 }'); # Set the value of $myEntityOrCollection->a->b->c to 42

In order to add some security, I also plan to create a new annotation to set access permissions on properties.

Ex :

use Doctrine\ORM\Mapping as ORM;

class MyEntity {
  /**
   * @ORM\Column(type="integer")
   * @ORM\Id
   * @ORM\Expose({"group_read"})
   */
   public $id;
}

$jpa = new JsonPointerAccess();
$jpa->get($myEntity, '/id', ['group_read']); # OK
$jpa->set($myEntity, '/id', 22, ['group_read']); # Throw an exception

I'd like some feedback before starting the implementation:

  • Do you thing a such component should be in Doctrine or in a separate package?
  • What do you think of the purposed syntax?
  • Do you see other important features in this scope I've missed?

Thank you.



 Comments   
Comment by Marco Pivetta [ 12/Jan/14 ]

This idea is out of scope for ORM.

There's different projects you may look at:

These are obviously all trying to integrate with third party components, but the problem of REST APIs is out of the scope of persistence.
Please ping the people that are maintaining these projects before jumping forward and implementing from scratch.

And yes, the example API you've shown here is awesome - just out of scope for this project

Comment by Kévin Dunglas [ 12/Jan/14 ]

Thanks for your reply.

I'm aware of these projects and in touch with people building HATEOAS (as a Symfony developper, other projects you mention are awesome but not fit my use case): https://github.com/willdurand/Hateoas/issues/130

It seems that it's not easy to implement JSON API / Patch /Pointer in this kind of libraries but I'm keeping an eye on it. Maybe that a tiny library focusing on JSON stuffs support (instead of all hypermedia related things like HATEOAS) can be a good start (and maybe that some code can be shared between projects).

Generated at Sun Oct 26 04:08:15 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.