{"id":18575811,"url":"https://github.com/paveldanilin/request-body-bundle","last_synced_at":"2025-04-10T08:30:55.957Z","repository":{"id":57036548,"uuid":"326424572","full_name":"paveldanilin/request-body-bundle","owner":"paveldanilin","description":"A Symfony bundle for the request body mapping to object","archived":false,"fork":false,"pushed_at":"2022-10-20T03:24:31.000Z","size":63,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"2.2.x","last_synced_at":"2025-04-06T22:32:18.269Z","etag":null,"topics":["annotation","bundle","php","request-body","symfony","symfony-bundle"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paveldanilin.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-03T14:21:43.000Z","updated_at":"2023-06-27T00:09:41.000Z","dependencies_parsed_at":"2022-08-24T06:40:15.134Z","dependency_job_id":null,"html_url":"https://github.com/paveldanilin/request-body-bundle","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paveldanilin%2Frequest-body-bundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paveldanilin%2Frequest-body-bundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paveldanilin%2Frequest-body-bundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paveldanilin%2Frequest-body-bundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paveldanilin","download_url":"https://codeload.github.com/paveldanilin/request-body-bundle/tar.gz/refs/heads/2.2.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248185271,"owners_count":21061486,"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":["annotation","bundle","php","request-body","symfony","symfony-bundle"],"created_at":"2024-11-06T23:22:14.714Z","updated_at":"2025-04-10T08:30:55.584Z","avatar_url":"https://github.com/paveldanilin.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"### @RequestBody annotation\n\nRequestBody is a way to populate objects and inject them as controller method arguments.\n\nThe Request body converter makes it possible to deserialize the request body into an object.\n\n#### Install\n\n\n`composer require paveldanilin/request-body-bundle`\n\n#### Usage\n\nBy default, RequestBody is trying to populate the single defined parameter.\n\n```php\n/**\n * @Route(\"/users\", methods={\"POST\"})\n *\n * @RequestBody\n *\n * @param User $user\n * @return Response\n */\npublic function createUser(User $user): Response\n{\n    return new Response();\n}\n```\n\nIf a method has several parameters we should explicitly define the parameter for populating.\n\n```php\n/**\n * @Route(\"/users\", methods={\"PATCH\"})\n *\n * @RequestBody(\"user\")\n *\n * @param int $userId\n * @param User $user\n * @return Response\n */\npublic function editUser(int $userId, User $user): Response\n{\n    return new Response();\n}\n```\n\n#### Deserialization\n\nWe can specify a deserialization context.\n\nMore about the object deserialization you can find [here](https://symfony.com/doc/4.4/components/serializer.html#deserializing-an-object)\n\n```php\n/**\n * @Route(\"/users\", methods={\"PATCH\"})\n *\n * @RequestBody(\"user\", deserializationContext={\"someAttribute\"=\"value\"})\n *\n * @param int $userId\n * @param User $user\n * @return Response\n */\npublic function editUser(int $userId, User $user): Response\n{\n    return new Response();\n}\n```\n\nAlso, it is possible to replace the deserialization error message with a custom message.\n\n```php\n/**\n * @Route(\"/users\", methods={\"PATCH\"})\n *\n * @RequestBody(\"user\", deserializationError=\"Bad DTO\")\n *\n * @param int $userId\n * @param User $user\n * @return Response\n */\npublic function editUser(int $userId, User $user): Response\n{\n    return new Response();\n}\n```\n\n#### Validation\n\nBy default, validation will be performed for each assertion which is defined per DTO.\n\nFor the following DTO will be performed two assertions after a deserialization process.\n\n```php\nclass User\n{\n    /**\n     * @Assert\\NotBlank(allowNull=false)\n     * @Assert\\Type(type=\"string\")\n     *\n     * @var string\n     */\n    public $name;\n}\n```\n\nWe can avoid a validation process by defining the `validationGroups` attribute as an empty array.\n\n```php\n/**\n * @Route(\"/users\", methods={\"PATCH\"})\n *\n * @RequestBody(\"user\", validationGroups={})\n *\n * @param int $userId\n * @param User $user\n * @return Response\n */\npublic function editUser(int $userId, User $user): Response\n{\n    return new Response();\n}\n```\n\nOr we can explicitly define validation groups by means of `validationGroups` attribute.\n\n```php\n/**\n * @Route(\"/users\", methods={\"PATCH\"})\n *\n * @RequestBody(\"user\", validationGroups={\"edit\"})\n *\n * @param int $userId\n * @param User $user\n * @return Response\n */\npublic function editUser(int $userId, User $user): Response\n{\n    return new Response();\n}\n```\n\nYou can read [more](https://symfony.com/doc/4.4/validation.html) about a validation process.\n\n#### Debug\n\nThe bundle comes with a handy console command which shows all controllers that use the @RequestBody annotation\n\n```\n$ php bin/console debug:request-body\n\n\n+--------------------+----------------------+---------------------------------------------------------------------------------------+----------------------------------------------------+--------------------+\n| Class              | Method               | Bind Param                                                                            | Param Type                                         | Validation Context |\n+--------------------+----------------------+---------------------------------------------------------------------------------------+----------------------------------------------------+--------------------+\n| TestUserController | createUser           | user                                                                                  | paveldanilin\\RequestBodyBundle\\Tests\\Fixtures\\User | all                |\n| TestUserController | editUser             | Method does not have such parameter 'user'                                            |                                                    | all                |\n| TestUserController | noTypeHint           | The 'user' parameter does not have a type hint                                        |                                                    | all                |\n| TestUserController | autoMap              | user                                                                                  | paveldanilin\\RequestBodyBundle\\Tests\\Fixtures\\User | all                |\n| TestUserController | autoMapNoParams      | Could not autodetect parameter for body mapping. The method does not have parameters. |                                                    | all                |\n| TestUserController | autoMapTooManyParams | Could not autodetect parameter for body mapping. The method has too many parameters.  |                                                    | all                |\n+--------------------+----------------------+---------------------------------------------------------------------------------------+----------------------------------------------------+--------------------+\n\n```\n\n#### Test\n- `composer test`\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaveldanilin%2Frequest-body-bundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaveldanilin%2Frequest-body-bundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaveldanilin%2Frequest-body-bundle/lists"}