8000 [HttpKernel] RequestPayloadValueResolver Add support for custom http status code by zim32 · Pull Request #50767 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] RequestPayloadValueResolver Add support for custom http status code #50767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

8000 By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

zim32
Copy link
@zim32 zim32 commented Jun 25, 2023
Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets no
License MIT
Doc PR Will do if accepted

Reading this discussion #49134 (comment) it looks like a good idea to have ability to customize http status code in case of a validation error.

This MR does not create any BC's.

MapQueryString and MapRequestPayload attributes now have additional parameter validationFailedStatusCode, which allows to specify which http status code will be used if validation fails.

8000
@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@ghost
Copy link
ghost commented Jun 25, 2023

Please modify tests to check it's a status code from the attribute that is thrown.

@zim32
Copy link
Author
zim32 commented Jun 25, 2023

Please modify tests to check it's a status code from the attribute that is thrown.

Two tests added, for query and request payload.

} elseif ($argument instanceof MapRequestPayload) {
$payloadMapper = 'mapRequestPayload';
$validationFailedCode = Response::HTTP_UNPROCESSABLE_ENTITY;
$validationFailedCode = $argument->validationFailedStatusCode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be great to be able to define another error by default (from the constructor of this class). Otherwise it means that for every attribute we would have to do validationFailedStatusCode: Response::HTTP_SOMETHING.

Copy link
Author
@zim32 zim32 Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. But maybe it will be better to do this by another MR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't see why that can't be added to this PR? Seems easy enough to add two constructor arguments and rely on those for the default value. This means in the attribute those arguments should be nullable and null by default. This way we can override it with the argument on the attribute but don't rely on the attribute for the default value of those arguments.

If you want, I can help with that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this is another feature amd it is always better to separate features into several MR otherwise MR can become quite messy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is a good idea. Descriptive info (attributes here) should be self-descriptive. Making them rely on some external configuration blurs the line and readers can't expect a stable outcome anymore.

The solution to your concern is to create and use a child class that sets the code to what you want.

Copy link
Member
@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Please add a line to the changelog of the component.

@carsonbot carsonbot changed the title RequestPayloadValueResolver Add support for custom http status code [HttpKernel] RequestPayloadValueResolver Add support for custom http status code Jul 20, 2023
@nicolas-grekas nicolas-grekas force-pushed the HttpKernel/RequestPayloadValueResolver-custom-http-code branch from daff0e8 to 45ce17a Compare August 1, 2023 08:53
Copy link
Member
@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the remaining comments.

@nicolas-grekas nicolas-grekas force-pushed the HttpKernel/RequestPayloadValueResolver-custom-http-code branch from 45ce17a to 4d118c0 Compare August 1, 2023 08:57
@nicolas-grekas
Copy link
Member

Thank you @zim32.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0