Fosrestbundle: Form validation status code problem

Created on 4 Jun 2020  路  9Comments  路  Source: FriendsOfSymfony/FOSRestBundle

After updating to version 2.8 ViewHandler is returning View annotation's status code instead of declared failed_validation config value.

For method:

    /**
     * @Rest\Post("")
     * @Rest\View(statusCode=201)
     *
     * @return FormInterface|array<string, string>
     */
    public function create(Request $request)
    ...

Http status code of a response is valid but response's body code value is invalid.

{
    "code": 201,
    "message": "Validation Failed",
    ...

The problem is caused by one added if statement in method ViewHandler::getSerializationContext:

if (null !== $view->getStatusCode()) {
    $context->setAttribute('status_code', $view->getStatusCode());
}

The ViewHandler::initResponse method checks if there is a form and is not valid then sets view context attribute status_code to config value.
But unfortunately then It gets serialization context and status_codeattribute is being overridden by view status code which is view annotation one.

Because of this issue there is no way to set custom status code for successful response and have valid response body when form validation fail.

Bug Needs Feedback

All 9 comments

Maybe a bugfix for #2235 would fix this issue too.

@knyk Can you please check if #2253 fixes this issue too?

@knyk Can you please check if #2253 fixes this issue too?

Unfortunately, It is still an issue with #2253 applied

Too bad, could you then maybe create a small application that allows me to reproduce this? If possible, I would like to include patches for #2235 and this issue in the next release.

@xabbuh https://github.com/knyk/fos-rest-response-code-issue-demo I have just created simple demo app with simple phpunit test. You can now reproduce the issue by Your self

Thank you @knyk. I am trying to look into it tonight.

Can you confirm that #2255 fixes the issue?

I can confirm the change in #2255 fixes the issue :)

Thank you, I am going to write some tests later and will then tag new releases.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

angrygreenfrogs picture angrygreenfrogs  路  4Comments

feridmovsumov picture feridmovsumov  路  7Comments

JeroenDeDauw picture JeroenDeDauw  路  6Comments

dkorsak picture dkorsak  路  5Comments

Gnucki picture Gnucki  路  7Comments