Skip to content

[HttpKernel] Allow customising HTTP status code on validation error of #[MapRequestPayload] objects #50992

@siketyan

Description

@siketyan

Description

#[MapRequestPayload] attribute, introduced in Symfony 6.3, is an awesome feature for APIs that use JSON for requests. The attribute works with Serializer and Validator components, and returns 422 Unprocessable Entity status code on Validator returned any violation(s). However, in API development, sometimes we want 400 Bad Request instead of 422. So it would be great if we can customise the status code that MapRequestPayload returns on validation error.

Example

Option 1. Use attribute to customise for each endpoint

#[Route('/pets', methods: ['POST'])]
public function createPet(
    #[MapRequestPayload(statusOnError: Response::HTTP_BAD_REQUEST) PetDto $pet,
): Response;

Option 2. Use config to customise globally

framework:
    map_request_payload:
        default_status_code_on_error: 400

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy