8000 [HttpKernel] Allow customising HTTP status code on validation error of #[MapRequestPayload] objects · Issue #50992 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[HttpKernel] Allow customising HTTP status code on validation error of #[MapRequestPayload] objects #50992
Closed
@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

      0