FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.79k stars 702 forks source link

It's not possible to customize HTTP status code for failed validation #2376

Closed garak closed 2 years ago

garak commented 2 years ago

The current code is passed to ViewHandler::create() with a default value of 400 (took from Response::HTTP_BAD_REQUEST). I guess this was decided after the discussion in #19

Could we reconsider that decision, taking into account that Symfony opted for 422 (Response::HTTP_UNPROCESSABLE_ENTITY) a few times ago? Or, as a possible alternative, could we make this code configurable by the user?

goetas commented 2 years ago

Are https://github.com/FriendsOfSymfony/FOSRestBundle/blob/3.x/View/View.php#L34

View takes as second parameter the status code, isn't that what you need?

garak commented 2 years ago

That parameter is overridden by the handler

yceruto commented 2 years ago

When it's Symfony Form instance this bundle follows this config to return the status code:

fos_rest:
    view:
        failed_validation: 422

In other cases, the status code you've passed to the View instance will be taken into account.

yceruto commented 2 years ago

However, I think the one passed to the View instance should have priority over the default config: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/b888195589d245002880d07073ef23ab60e3795d/View/ViewHandler.php#L217-L224

goetas commented 2 years ago

@garak does this answer your question?

garak commented 2 years ago

@garak does this answer your question?

it does, but the option is not documented. You know the old saying: "if it isn't documented, it doesn't exist"

goetas commented 2 years ago

You know the old saying: "if it isn't documented, it doesn't exist"

feel free to create a pull request with the docs, it is very welcomed

for now i'm closing this ticket