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

[Maintenance][API] Normalize command missing fields' names #15775

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/Sylius/Bundle/ApiBundle/Serializer/CommandDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,28 @@ public function denormalize($data, $type, $format = null, array $context = [])
throw new InvalidRequestArgumentException(
sprintf(
'Request field "%s" should be of type "%s".',
$this->nameConverter->normalize($previousException->getPath(), class: $context['input']['class']),
$this->normalizeFieldName($previousException->getPath(), $context['input']['class']),
implode(', ', $previousException->getExpectedTypes()),
),
);
}

throw $exception;
} catch (MissingConstructorArgumentsException $exception) {
throw new MissingConstructorArgumentsException(
sprintf('Request does not have the following required fields specified: %s.', implode(', ', $exception->getMissingConstructorArguments())),
);
$class = $context['input']['class'];

throw new MissingConstructorArgumentsException(sprintf(
'Request does not have the following required fields specified: %s.',
implode(', ', array_map(
fn (string $field) => $this->normalizeFieldName($field, $class),
$exception->getMissingConstructorArguments(),
)),
));
}
}

private function normalizeFieldName(string $field, string $class): string
{
return $this->nameConverter->normalize($field, $class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,64 +30,75 @@ function let(DenormalizerInterface $baseNormalizer, AdvancedNameConverterInterfa
$this->beConstructedWith($baseNormalizer, $nameConverter);
}

function it_implements_context_aware_denormalizer_interface(): void
{
$this->shouldImplement(ContextAwareDenormalizerInterface::class);
}

function it_supports_denormalization_for_specified_input_class(): void
{
$this->supportsDenormalization(null, '', context: ['input' => ['class' => 'Class']])->shouldReturn(true);
}

function it_does_not_support_denormalization_for_not_specified_input_class(): void
{
$this->supportsDenormalization(null, '')->shouldReturn(false);
}

function it_throws_exception_if_not_all_required_parameters_are_present_in_the_context(
DenormalizerInterface $baseNormalizer,
AdvancedNameConverterInterface $nameConverter,
): void {
$exception = new MissingConstructorArgumentsException('Missing constructor arguments', 400, null, ['firstName', 'lastName']);
$exception = new MissingConstructorArgumentsException('', 400, null, ['firstName', 'lastName']);
$context = ['input' => ['class' => RegisterShopUser::class]];
$data = ['email' => 'test@example.com', 'password' => 'pa$$word'];

$nameConverter->normalize('firstName', class: RegisterShopUser::class)->willReturn('first_name');
$nameConverter->normalize('lastName', class: RegisterShopUser::class)->willReturn('lastName');

$baseNormalizer->denormalize($data, '', null, $context)->willThrow($exception);

$this
->shouldThrow(new MissingConstructorArgumentsException(
'Request does not have the following required fields specified: firstName, lastName.',
'Request does not have the following required fields specified: first_name, lastName.',
))
->during('denormalize', [$data, '', null, $context]);
->during('denormalize', [$data, '', null, $context])
;
}

function it_throws_exception_for_mismatched_argument_type(
DenormalizerInterface $baseNormalizer,
AdvancedNameConverterInterface $nameConverter,
): void {
$nameConverter->normalize('firstName', class: RegisterShopUser::class)->willReturn('first_name');
$previousException = NotNormalizableValueException::createForUnexpectedDataType('Not normalizable value', 1, ['string'], 'firstName');
$exception = new UnexpectedValueException('Unexpected value', 400, $previousException);
$previousException = NotNormalizableValueException::createForUnexpectedDataType('', 1, ['string'], 'firstName');
$exception = new UnexpectedValueException('', 400, $previousException);
$context = ['input' => ['class' => RegisterShopUser::class]];
$data = ['firstName' => 1];

$nameConverter->normalize('firstName', class: RegisterShopUser::class)->willReturn('first_name');

$baseNormalizer->denormalize($data, '', null, $context)->willThrow($exception);

$this
->shouldThrow(new InvalidRequestArgumentException(
'Request field "first_name" should be of type "string".',
))
->during('denormalize', [$data, '', null, $context]);
->during('denormalize', [$data, '', null, $context])
;
}

function it_throws_the_same_exception_if_previous_exception_is_not_not_normalizable_value_exception(
function it_throws_the_same_exception_if_previous_exception_is_not_normalizable_value_exception(
DenormalizerInterface $baseNormalizer,
): void {
$exception = new UnexpectedValueException('Unexpected value');
$context = ['input' => ['class' => RegisterShopUser::class]];
$data = ['firstName' => '1'];

$baseNormalizer->denormalize($data, '', null, $context)->willThrow($exception);

$this
->shouldThrow(new UnexpectedValueException('Unexpected value'))
->during('denormalize', [$data, '', null, $context]);
}

function it_implements_context_aware_denormalizer_interface(): void
{
$this->shouldImplement(ContextAwareDenormalizerInterface::class);
}

function it_supports_denormalization_for_specified_input_class(): void
{
$this->supportsDenormalization(null, '', null, ['input' => ['class' => 'Class']])->shouldReturn(true);
}

function it_does_not_support_denormalization_for_not_specified_input_class(): void
{
$this->supportsDenormalization(null, '', null, [])->shouldReturn(false);
->during('denormalize', [$data, '', null, $context])
;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"code": 400,
"message": "Request does not have the following required fields specified: productVariantCode."
"message": "Request does not have the following required fields specified: productVariant."
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"code": 400,
"message": "Request does not have the following required fields specified: title, rating, comment, productCode."
"message": "Request does not have the following required fields specified: title, rating, comment, product."
}
Loading