{"id":13752865,"url":"https://github.com/nilportugues/symfony-jsonapi","last_synced_at":"2025-04-07T12:11:11.375Z","repository":{"id":36581855,"uuid":"40887816","full_name":"nilportugues/symfony-jsonapi","owner":"nilportugues","description":"JSON API Transformer Bundle for Symfony 2 and Symfony 3","archived":false,"fork":false,"pushed_at":"2019-05-27T11:54:16.000Z","size":37,"stargazers_count":114,"open_issues_count":13,"forks_count":18,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-05T15:05:58.600Z","etag":null,"topics":["api","json","json-api","jsonapi","marshaller","microservice","microservices","php","php7","serialization","serializer","symfony","symfony-bundle","symfony2","symfony3","transformer"],"latest_commit_sha":null,"homepage":"http://nilportugues.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nilportugues.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-17T11:05:30.000Z","updated_at":"2024-11-26T10:13:05.000Z","dependencies_parsed_at":"2022-09-08T22:50:09.066Z","dependency_job_id":null,"html_url":"https://github.com/nilportugues/symfony-jsonapi","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fsymfony-jsonapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fsymfony-jsonapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fsymfony-jsonapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nilportugues%2Fsymfony-jsonapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nilportugues","download_url":"https://codeload.github.com/nilportugues/symfony-jsonapi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247648977,"owners_count":20972945,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","json","json-api","jsonapi","marshaller","microservice","microservices","php","php7","serialization","serializer","symfony","symfony-bundle","symfony2","symfony3","transformer"],"created_at":"2024-08-03T09:01:11.910Z","updated_at":"2025-04-07T12:11:11.355Z","avatar_url":"https://github.com/nilportugues.png","language":"PHP","readme":"Symfony JSON-API Transformer Bundle\n=========================================\nFor Symfony 2 and Symfony 3\n\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nilportugues/symfony2-jsonapi-transformer/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/nilportugues/symfony2-jsonapi-transformer/?branch=master)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/3269f12e-a707-462a-bef5-22e5ed522e8e/mini.png?)](https://insight.sensiolabs.com/projects/3269f12e-a707-462a-bef5-22e5ed522e8e) \n[![Latest Stable Version](https://poser.pugx.org/nilportugues/jsonapi-bundle/v/stable)](https://packagist.org/packages/nilportugues/jsonapi-bundle)\n[![Total Downloads](https://poser.pugx.org/nilportugues/jsonapi-bundle/downloads)](https://packagist.org/packages/nilportugues/jsonapi-bundle) \n[![License](https://poser.pugx.org/nilportugues/jsonapi-bundle/license)](https://packagist.org/packages/nilportugues/jsonapi-bundle) \n[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://paypal.me/nilportugues)\n\n- [Installation](#installation)\n- [Usage](#usage)\n    - [Creating the mappings](#creating-the-mappings)\n- [Outputing API Responses](#outputing-api-responses)\n    - [JSON API Requests](#request-objects)\n    - [JSON API Responses](#response-objects-jsonapiresponsetrait)\n- [Integration with NelmioApiDocBundleBundle](#integration-with-nelmioapidocbundlebundle)\n- [Quality](#quality)\n- [Contribute](#contribute)\n- [Support](#support)\n- [Authors](#authors)\n- [License](#license)\n \n\n\n## Installation\n\n**Step 1: Download the Bundle**\n\nOpen a command console, enter your project directory and execute the\nfollowing command to download the latest stable version of this bundle:\n\n```bash\n$ composer require nilportugues/jsonapi-bundle\n```\n\n\n**Step 2: Enable the Bundle**\n\nThen, enable the bundle by adding it to the list of registered bundles\nin the `app/AppKernel.php` file of your project:\n\n```php\n\u003c?php\n// app/AppKernel.php\n// ...\nclass AppKernel extends Kernel\n{\n    public function registerBundles()\n    {\n        $bundles = array(\n            // ...\n            new NilPortugues\\Symfony\\JsonApiBundle\\NilPortuguesSymfonyJsonApiBundle(),\n        );\n        // ...\n    }\n    // ...\n}\n```\n\n## Usage\n\n### Creating the mappings\n\n**Mapping directory**\n\nMapping files should be located at the `app/config/serializer` directory. This directory must be created.\n\nIt can be also be customized and placed elsewhere by editing the `app/config/config.yml` configuration file:\n\n```yml\n# app/config/config.yml\n\nnilportugues_json_api:\n    mappings: \n        - \"%kernel.root_dir%/config/serializer/\"\n        - @AppBundle/Product/config/Mappings\n\n```\n\n**Mapping files**\n\nThe JSON-API transformer works by transforming an existing PHP object into its JSON representation. For each object, a mapping file is required. \n\nMapping files **must** be placed in the mappings directory. The expected mapping file format is `.yml` and  will allow you to rename, hide and create links relating all of your data.\n\nFor instance, here's a quite complex `Post` object to demonstrate how it works:\n\n```php\n$post = new Post(\n    new PostId(9),\n    'Hello World',\n    'Your first post',\n    new User(\n        new UserId(1),\n        'Post Author'\n    ),\n    [\n        new Comment(\n            new CommentId(1000),\n            'Have no fear, sers, your king is safe.',\n            new User(new UserId(2), 'Barristan Selmy'),\n            [\n                'created_at' =\u003e (new DateTime('2015/07/18 12:13:00'))-\u003eformat('c'),\n                'accepted_at' =\u003e (new DateTime('2015/07/19 00:00:00'))-\u003eformat('c'),\n            ]\n        ),\n    ]\n);\n```\n\nAnd the series of mapping files required:\n\n```yml\n# app/config/serializer/acme_domain_dummy_post.yml\n\nmapping:\n  class: Acme\\Domain\\Dummy\\Post\n  alias: Message\n  aliased_properties:\n    author: author\n    title: headline\n    content: body\n  hide_properties: []\n  id_properties:\n    - postId\n  urls:\n    self: get_post ## @Route name\n    comments: get_post_comments ## @Route name\n  relationships:\n    author:\n      related: get_post_author ## @Route name\n      self: get_post_author_relationship  ## @Route name\n```\n\n```yml\n# app/config/serializer/acme_domain_dummy_value_object_post_id.yml\n\nmapping:\n  class: Acme\\Domain\\Dummy\\ValueObject\\PostId\n  aliased_properties: []\n  hide_properties: []\n  id_properties:\n  - postId\n  urls:\n    self: get_post  ## @Route name\n  relationships:\n    comment:\n      self: get_post_comments_relationship  ## @Route name\n```\n\n\n```yml\n# app/config/serializer/acme_domain_dummy_comment.yml\n\nmapping:\n  class: Acme\\Domain\\Dummy\\Comment\n  aliased_properties: []\n  hide_properties: []\n  id_properties:\n    - commentId\n  urls:\n    self: get_comment ## @Route name\n  relationships:\n    post:\n      self: get_post_comments_relationship ## @Route name\n```\n\n```yml\n# app/config/serializer/acme_domain_dummy_value_object_comment_id.yml\n\nmapping:\n  class: Acme\\Domain\\Dummy\\ValueObject\\CommentId\n  aliased_properties: []\n  hide_properties: []\n  id_properties:\n    - commentId\n  urls:\n    self: get_comment ## @Route name\n  relationships:\n    post:\n      self: get_post_comments_relationship ## @Route name\n```\n\n\n```yml\n# app/config/serializer/acme_domain_dummy_user.yml\n\nmapping:\n  class: Acme\\Domain\\Dummy\\User\n  aliased_properties: []\n  hide_properties: []\n  id_properties:\n  - userId\n  urls:\n    self: get_user\n    friends: get_user_friends  ## @Route name\n    comments: get_user_comments  ## @Route name\n```\n\n\n```yml\n# app/config/serializer/acme_domain_dummy_value_object_user_id.yml\n\nmapping:\n  class: Acme\\Domain\\Dummy\\ValueObject\\UserId\n  aliased_properties: []\n  hide_properties: []\n  id_properties:\n  - userId\n  urls:\n    self: get_user  ## @Route name\n    friends: get_user_friends  ## @Route name\n    comments: get_user_comments  ## @Route name\n```\n\n\n## Outputing API Responses\n\nIt is really easy, just get an instance of the `JsonApiSerializer` from the **Service Container** and pass the object to its `serialize()` method. Output will be valid JSON-API.\n\nHere's an example of a `Post` object being fetched from a Doctrine repository.\n\nFinally, a helper trait, `JsonApiResponseTrait` is provided to write fully compilant responses wrapping the PSR-7 Response objects provided by the original JSON API Transformer library.\n\n```php\n\u003c?php\nnamespace AppBundle\\Controller;\n\nuse NilPortugues\\Symfony\\JsonApiBundle\\Serializer\\JsonApiResponseTrait;\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller;\n\nclass PostController extends Controller\n{\n    use JsonApiResponseTrait;\n\n    /**\n     * @\\Symfony\\Component\\Routing\\Annotation\\Route(\"/post/{postId}\", name=\"get_post\")\n     *\n     * @param $postId\n     * @return \\Symfony\\Component\\HttpFoundation\\Response\n     */\n    public function getPostAction($postId)\n    {\n        $post = $this-\u003eget('doctrine.post_repository')-\u003efind($postId);\n        \n        $serializer = $this-\u003eget('nil_portugues.serializer.json_api_serializer');\n\n        /** @var \\NilPortugues\\Api\\JsonApi\\JsonApiTransformer $transformer */\n        $transformer = $serializer-\u003egetTransformer();\n        $transformer-\u003esetSelfUrl($this-\u003egenerateUrl('get_post', ['postId' =\u003e $postId], true));\n        $transformer-\u003esetNextUrl($this-\u003egenerateUrl('get_post', ['postId' =\u003e $postId+1], true));\n\n        return $this-\u003eresponse($serializer-\u003eserialize($post));\n    }\n} \n```\n\n\n**Output:**\n\n```\nHTTP/1.1 200 OK\nCache-Control: private, max-age=0, must-revalidate\nContent-type: application/vnd.api+json\n```\n\n```json\n{\n    \"data\": {\n        \"type\": \"message\",\n        \"id\": \"9\",\n        \"attributes\": {\n            \"headline\": \"Hello World\",\n            \"body\": \"Your first post\"\n        },\n        \"links\": {\n            \"self\": {\n                \"href\": \"http://example.com/posts/9\"\n            },\n            \"comments\": {\n                \"href\": \"http://example.com/posts/9/comments\"\n            }\n        },\n        \"relationships\": {\n            \"author\": {\n                \"links\": {\n                    \"self\": {\n                        \"href\": \"http://example.com/posts/9/relationships/author\"\n                    },\n                    \"related\": {\n                        \"href\": \"http://example.com/posts/9/author\"\n                    }\n                },\n                \"data\": {\n                    \"type\": \"user\",\n                    \"id\": \"1\"\n                }\n            }\n        }\n    },\n    \"included\": [\n        {\n            \"type\": \"user\",\n            \"id\": \"1\",\n            \"attributes\": {\n                \"name\": \"Post Author\"\n            },\n            \"links\": {\n                \"self\": {\n                    \"href\": \"http://example.com/users/1\"\n                },\n                \"friends\": {\n                    \"href\": \"http://example.com/users/1/friends\"\n                },\n                \"comments\": {\n                    \"href\": \"http://example.com/users/1/comments\"\n                }\n            }\n        },\n        {\n            \"type\": \"user\",\n            \"id\": \"2\",\n            \"attributes\": {\n                \"name\": \"Barristan Selmy\"\n            },\n            \"links\": {\n                \"self\": {\n                    \"href\": \"http://example.com/users/2\"\n                },\n                \"friends\": {\n                    \"href\": \"http://example.com/users/2/friends\"\n                },\n                \"comments\": {\n                    \"href\": \"http://example.com/users/2/comments\"\n                }\n            }\n        },\n        {\n            \"type\": \"comment\",\n            \"id\": \"1000\",\n            \"attributes\": {\n                \"dates\": {\n                    \"created_at\": \"2015-08-13T21:11:07+02:00\",\n                    \"accepted_at\": \"2015-08-13T21:46:07+02:00\"\n                },\n                \"comment\": \"Have no fear, sers, your king is safe.\"\n            },\n            \"relationships\": {\n                \"user\": {\n                    \"data\": {\n                        \"type\": \"user\",\n                        \"id\": \"2\"\n                    }\n                }\n            },            \n            \"links\": {\n                \"self\": {\n                    \"href\": \"http://example.com/comments/1000\"\n                }\n            }\n        }\n    ],\n    \"links\": {\n        \"self\": {\n            \"href\": \"http://example.com/posts/9\"\n        },\n        \"next\": {\n            \"href\": \"http://example.com/posts/10\"\n        }\n    },\n    \"jsonapi\": {\n        \"version\": \"1.0\"\n    }\n}\n```\n\n#### Request objects\n\nJSON API comes with a helper Request class, `NilPortugues\\Api\\JsonApi\\Http\\Request\\Request(ServerRequestInterface $request)`, implementing the PSR-7 Request Interface. Using this request object will provide you access to all the interactions expected in a JSON API:\n\n##### JSON API Query Parameters:\n\n- **\u0026fields[resource]=field1,field2** will only show the specified fields for a given resource.\n- **\u0026include=resource** show the relationship for a given resource.\n- **\u0026include=resource.resource2** show the relationship field for those depending on resource2.\n- **\u0026sort=field1,-field2** sort by field2 as DESC and field1 as ASC.\n- **\u0026sort=-field1,field2** sort by field1 as DESC and field2 as ASC.\n- **\u0026page[number]** will return the current page elements in a *page-based* pagination strategy.\n- **\u0026page[size]** will return the total amout of elements in a *page-based* pagination strategy.\n- **\u0026page[limit]** will return the limit in a *offset-based* pagination strategy.\n- **\u0026page[offset]** will return the offset value in a *offset-based* pagination strategy.\n- **\u0026page[cursor]** will return the cursor value  in a *cursor-based* pagination strategy.\n- **\u0026filter** will return data passed in the filter param.\n\n\n##### NilPortugues\\Api\\JsonApi\\Http\\Request\\Request\n\nGiven the query parameters listed above, Request implements helper methods that parse and return data already prepared.\n\n```php\nnamespace \\NilPortugues\\Api\\JsonApi\\Http\\Request;\n\nclass Request\n{\n  public function __construct(ServerRequestInterface $request = null) { ... }\n  public function getIncludedRelationships() { ... }\n  public function getSort() { ... }\n  public function getPage() { ... }\n  public function getFilters() { ... }\n  public function getFields() { ... }\n}\n```\n\n#### Response objects (JsonApiResponseTrait)\n\nThe following `JsonApiResponseTrait` methods are provided to return the right headers and HTTP status codes are available:\n\n```php\n    private function errorResponse($json);\n    private function resourceCreatedResponse($json);\n    private function resourceDeletedResponse($json);\n    private function resourceNotFoundResponse($json);\n    private function resourcePatchErrorResponse($json);\n    private function resourcePostErrorResponse($json);\n    private function resourceProcessingResponse($json);\n    private function resourceUpdatedResponse($json);\n    private function response($json);\n    private function unsupportedActionResponse($json);\n```    \n## Integration with NelmioApiDocBundleBundle\n\nThe [NelmioApiDocBundle](https://github.com/nelmio/NelmioApiDocBundle/blob/master/Resources/doc/index.md) is a very well known bundle used to document APIs. Integration with the current bundle is terrible easy. \n\nHere's an example following the `PostContoller::getPostAction()` provided before:\n\n```php\n\u003c?php\nnamespace AppBundle\\Controller;\n\nuse NilPortugues\\Symfony\\JsonApiBundle\\Serializer\\JsonApiResponseTrait;\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller;\n\nclass PostController extends Controller\n{\n    use JsonApiResponseTrait;\n\n    /**\n     * Get a Post by its identifier. Will return Post, Comments and User data.\n     *\n     * @Nelmio\\ApiDocBundle\\Annotation\\ApiDoc(\n     *  resource=true,\n     *  description=\"Get a Post by its unique id\",\n     * )\n     *\n     * @Symfony\\Component\\Routing\\Annotation\\Route(\"/post/{postId}\", name=\"get_post\")\n     * @Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Method({\"GET\"})\n     *\n     * @param $postId\n     * @return \\Symfony\\Component\\HttpFoundation\\Response\n     */\n    public function getPostAction($postId)\n    {\n        $post = $this-\u003eget('doctrine.post_repository')-\u003efind($postId);\n        \n        $serializer = $this-\u003eget('nil_portugues.serializer.json_api_serializer');\n\n        /** @var \\NilPortugues\\Api\\JsonApi\\JsonApiTransformer $transformer */\n        $transformer = $serializer-\u003egetTransformer();\n        $transformer-\u003esetSelfUrl($this-\u003egenerateUrl('get_post', ['postId' =\u003e $postId], true));\n        $transformer-\u003esetNextUrl($this-\u003egenerateUrl('get_post', ['postId' =\u003e $postId+1], true));\n\n        return $this-\u003eresponse($serializer-\u003eserialize($post));\n    }\n} \n```\n\nAnd the recommended configuration to be added in `app/config/config.yml`\n\n```yml\n#app/config/config.yml\n\nnelmio_api_doc:\n  sandbox:\n        authentication:\n          name: access_token\n          delivery: http\n          type:     basic\n          custom_endpoint: false\n        enabled:  true\n        endpoint: ~\n        accept_type: ~\n        body_format:\n            formats: []\n            default_format: form\n        request_format:\n            formats:\n                json: application/vnd.api+json\n            method: accept_header\n            default_format: json\n        entity_to_choice: false\n```        \n\n\n## Quality\n\nTo run the PHPUnit tests at the command line, go to the tests directory and issue phpunit.\n\nThis library attempts to comply with [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/), [PSR-4](http://www.php-fig.org/psr/psr-4/) and [PSR-7](http://www.php-fig.org/psr/psr-7/).\n\nIf you notice compliance oversights, please send a patch via [Pull Request](https://github.com/nilportugues/Symfony-jsonapi-transformer/pulls).\n\n\n## Contribute\n\nContributions to the package are always welcome!\n\n* Report any bugs or issues you find on the [issue tracker](https://github.com/nilportugues/Symfony-jsonapi-transformer/issues/new).\n* You can grab the source code at the package's [Git repository](https://github.com/nilportugues/Symfony-jsonapi-transformer).\n\n\n## Support\n\nGet in touch with me using one of the following means:\n\n - Emailing me at \u003ccontact@nilportugues.com\u003e\n - Opening an [Issue](https://github.com/nilportugues/Symfony-jsonapi-transformer/issues/new)\n - Using Gitter: [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/nilportugues/Symfony-jsonapi-transformer?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge)\n\n\n## Authors\n\n* [Nil Portugués Calderó](http://nilportugues.com)\n* [The Community Contributors](https://github.com/nilportugues/Symfony-jsonapi-transformer/graphs/contributors)\n\n\n## License\nThe code base is licensed under the [MIT license](LICENSE).\n","funding_links":["https://paypal.me/nilportugues"],"categories":["transformer","Web Services"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilportugues%2Fsymfony-jsonapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnilportugues%2Fsymfony-jsonapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnilportugues%2Fsymfony-jsonapi/lists"}