Coder Social home page Coder Social logo

request-body-bundle's Introduction

@RequestBody annotation

RequestBody is a way to populate objects and inject them as controller method arguments.

The Request body converter makes it possible to deserialize the request body into an object.

Install

composer require paveldanilin/request-body-bundle

Usage

By default, RequestBody is trying to populate the single defined parameter.

/**
 * @Route("/users", methods={"POST"})
 *
 * @RequestBody
 *
 * @param User $user
 * @return Response
 */
public function createUser(User $user): Response
{
    return new Response();
}

If a method has several parameters we should explicitly define the parameter for populating.

/**
 * @Route("/users", methods={"PATCH"})
 *
 * @RequestBody("user")
 *
 * @param int $userId
 * @param User $user
 * @return Response
 */
public function editUser(int $userId, User $user): Response
{
    return new Response();
}

Deserialization

We can specify a deserialization context.

More about the object deserialization you can find here

/**
 * @Route("/users", methods={"PATCH"})
 *
 * @RequestBody("user", deserializationContext={"someAttribute"="value"})
 *
 * @param int $userId
 * @param User $user
 * @return Response
 */
public function editUser(int $userId, User $user): Response
{
    return new Response();
}

Also, it is possible to replace the deserialization error message with a custom message.

/**
 * @Route("/users", methods={"PATCH"})
 *
 * @RequestBody("user", deserializationError="Bad DTO")
 *
 * @param int $userId
 * @param User $user
 * @return Response
 */
public function editUser(int $userId, User $user): Response
{
    return new Response();
}

Validation

By default, validation will be performed for each assertion which is defined per DTO.

For the following DTO will be performed two assertions after a deserialization process.

class User
{
    /**
     * @Assert\NotBlank(allowNull=false)
     * @Assert\Type(type="string")
     *
     * @var string
     */
    public $name;
}

We can avoid a validation process by defining the validationGroups attribute as an empty array.

/**
 * @Route("/users", methods={"PATCH"})
 *
 * @RequestBody("user", validationGroups={})
 *
 * @param int $userId
 * @param User $user
 * @return Response
 */
public function editUser(int $userId, User $user): Response
{
    return new Response();
}

Or we can explicitly define validation groups by means of validationGroups attribute.

/**
 * @Route("/users", methods={"PATCH"})
 *
 * @RequestBody("user", validationGroups={"edit"})
 *
 * @param int $userId
 * @param User $user
 * @return Response
 */
public function editUser(int $userId, User $user): Response
{
    return new Response();
}

You can read more about a validation process.

Debug

The bundle comes with a handy console command which shows all controllers that use the @RequestBody annotation

$ php bin/console request-body:debug


+--------------------+----------------------+------------+--------------------+
| Class              | Method               | Bind Param | Validation Context |
+--------------------+----------------------+------------+--------------------+
+--------------------+----------------------+------------+--------------------+
| TestUserController | createUser           | user       | all                |
| TestUserController | editUser             | user       | all                |
| TestUserController | noTypeHint           |            | all                |
| TestUserController | autoMap              |            | all                |
| TestUserController | autoMapNoParams      |            | all                |
| TestUserController | autoMapTooManyParams |            | all                |
+--------------------+----------------------+------------+--------------------+

Test

  • composer test

request-body-bundle's People

Contributors

paveldanilin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.