Skip to content
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.

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

[API] Test not providing required fields during registration #12811

Conversation

Zales0123
Copy link
Member

Q A
Branch? 1.9
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Related tickets
License MIT

The solution allows receiving proper, more explainable and less technical responses where some fields are not provided in the API request. Denormalizer detects if there are any missing required fields for the request input class and throws an exception. In fact, the same thing happens inside the API Platform, but the exception message is poor - and that's the reason we should change it. A normalizer is unfortunately needed (despite the exception mapping), to provide a proper response code in the response body. Maybe it could also be done in a more generic way 🖖

I'm not sure either it should be a bug fix or not... I feel that it can, as it solves a functionality for existing endpoints as well, but I'm not 100% sure about it 😄 🚀

@Zales0123 Zales0123 added DX Issues and PRs aimed at improving Developer eXperience. API APIs related issues and PRs. Bug Confirmed bugs or bugfixes. labels Jul 19, 2021
@Zales0123 Zales0123 requested a review from a team as a code owner July 19, 2021 06:46
@Zales0123 Zales0123 force-pushed the handle-missing-fields-in-command-based-requests-properly branch from 88fb3c0 to 02d9428 Compare July 19, 2021 06:52
@Zales0123 Zales0123 force-pushed the handle-missing-fields-in-command-based-requests-properly branch from 02d9428 to de381ee Compare July 19, 2021 06:55
@@ -27,6 +27,7 @@ api_platform:
SM\SMException: 422
Sylius\Bundle\ApiBundle\Exception\CannotRemoveCurrentlyLoggedInUser: 422
Sylius\Bundle\ApiBundle\Exception\ShippingMethodCannotBeRemoved: 422
Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException: 400
Copy link
Member

Choose a reason for hiding this comment

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

Is it needed? And if so, do we have exactly the same error structure as in normal missing field validation errors?

Copy link
Member Author

Choose a reason for hiding this comment

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

The goal of the change was to have a different error code in the response than 500. I'm not super-devoted to this code, but I suppose it should be 4**

Copy link
Member

Choose a reason for hiding this comment

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

I meant, that we already have some standardized way of exposing validation errors for not existing fields:
image

I'm wondering, how close validation error from this PR will be to standardized output

@Zales0123 Zales0123 force-pushed the handle-missing-fields-in-command-based-requests-properly branch from 52854b2 to 09e5c23 Compare July 19, 2021 12:20
@Zales0123 Zales0123 force-pushed the handle-missing-fields-in-command-based-requests-properly branch from 09e5c23 to 31aba0f Compare July 19, 2021 12:53
@Zales0123 Zales0123 force-pushed the handle-missing-fields-in-command-based-requests-properly branch from 985b790 to c535148 Compare July 20, 2021 06:31
Comment on lines +20 to +22
/**
* @experimental
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

One liner maybe? 🙂 And same for next class.

Scenario: Trying to register a new account without providing required fields
When I want to register a new account
And I specify the email as "goodman@gmail.com"
And I specify the password as "heisenberg"
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

*/
public function iShouldBeNotifiedThatFieldHaveToBeProvided(string ...$fields): void
{
$fields = $this->convertElementsToCamelCase($fields);
Copy link
Contributor

Choose a reason for hiding this comment

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

We could create camelCaseTransformer 😄


private function getResponseContent(): array
{
return json_decode($this->client->getResponse()->getContent(), true);
Copy link
Contributor

Choose a reason for hiding this comment

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

How about adding this method to the test client?

@GSadee GSadee merged commit 197c042 into Sylius:1.9 Jul 21, 2021
@GSadee
Copy link
Member

GSadee commented Jul 21, 2021

Thank you, Mateusz! 🎉

Zales0123 added a commit that referenced this pull request Jul 21, 2021
…default value (GSadee)

This PR was merged into the 1.9 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.9
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | fixes #12811
| License         | MIT

Fixes tests on 1.10 and master branches, where we test [RegisterShopUser](https://github.com/Sylius/Sylius/blob/1.10/src/Sylius/Bundle/ApiBundle/Command/RegisterShopUser.php#L69) that has new not nullable argument with a default value.

Failing test: https://github.com/Sylius/Sylius/runs/3122767036?check_suite_focus=true#step:18:75

Commits
-------

80836cf [API] Allow creation of commands with no arguments with a default value
GSadee added a commit that referenced this pull request Jul 22, 2021
… to populate (GSadee)

This PR was merged into the 1.10 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.10
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | fixes #12811
| License         | MIT

<!--
 - Bug fixes must be submitted against the 1.9 or 1.10 branch (the lowest possible)
 - Features and deprecations must be submitted against the master branch
 - Make sure that the correct base branch is set

 To be sure you are not breaking any Backward Compatibilities, check the documentation:
 https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html
-->


Commits
-------

56f14d4 [API] Do not check command arguments if there is an object to populate
f163dd4 [API] Use proper command bus in ApiBundle tests
c880876 [API] Fix tests for PHP 8.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API APIs related issues and PRs. Bug Confirmed bugs or bugfixes. DX Issues and PRs aimed at improving Developer eXperience.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants