Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2910

JSON Pointer and JSON Patch syntax support

    Details

      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.

        Activity

        Hide
        Kévin Dunglas added a comment - - edited

        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).

        Show
        Kévin Dunglas added a comment - - edited 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).
        Hide
        Marco Pivetta added a comment -

        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

        Show
        Marco Pivetta added a comment - This idea is out of scope for ORM. There's different projects you may look at: https://github.com/willdurand/Hateoas https://github.com/leedavis81/drest https://github.com/zf-fr/ZfrRest 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

          People

          • Assignee:
            Marco Pivetta
            Reporter:
            Kévin Dunglas
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: