Skip to content

Commit

Permalink
BUGFIX: Do not pass empty values to parameters that have a default value
Browse files Browse the repository at this point in the history
  • Loading branch information
mficzel committed Jun 17, 2024
1 parent 525419b commit 7a85c61
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Classes/Application/ParameterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public static function resolveParameters(string $className, string $methodName,
$parameterValueFromRequest = $parameterAttribute->style->decodeParameterValue($parameterValueFromRequest);
}

$parameters[$parameter->name] = SchemaDenormalizer::denormalizeValue($parameterValueFromRequest, $type->getName(), $parameter);
if ($parameterValueFromRequest !== null || $parameter->isDefaultValueAvailable() === false) {
$parameters[$parameter->name] = SchemaDenormalizer::denormalizeValue($parameterValueFromRequest, $type->getName(), $parameter);
}
}

return $parameters;
Expand Down
19 changes: 19 additions & 0 deletions Tests/Fixtures/Controller/PathController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ public function scalarNullableParameterEndpointAction(
return new EndpointResponse('acknowledged');
}

public function scalarNullableParameterWithoutDefaultEndpointAction(
#[OpenApi\Parameter(ParameterLocation::LOCATION_QUERY)]
?string $message,
#[OpenApi\Parameter(ParameterLocation::LOCATION_QUERY)]
?int $number,
#[OpenApi\Parameter(ParameterLocation::LOCATION_QUERY)]
?float $weight,
#[OpenApi\Parameter(ParameterLocation::LOCATION_QUERY)]
?bool $switch,
#[OpenApi\Parameter(ParameterLocation::LOCATION_QUERY)]
?\DateTime $dateTime,
#[OpenApi\Parameter(ParameterLocation::LOCATION_QUERY)]
?\DateTime $dateTimeImmutable,
#[OpenApi\Parameter(ParameterLocation::LOCATION_QUERY)]
?\DateInterval $dateInterval,
): EndpointResponse {
return new EndpointResponse('acknowledged');
}

public function scalarParameterWithDefaultValuesAction(
#[OpenApi\Parameter(ParameterLocation::LOCATION_QUERY)]
string $message = "suppe",
Expand Down
20 changes: 14 additions & 6 deletions Tests/Unit/Application/ParameterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ public static function parameterProvider(): iterable
),
'className' => PathController::class,
'methodName' => 'scalarNullableParameterEndpointAction',
'expectedParameters' => [
]
];

yield 'withScalarNullableNoDefaultParameters' => [
'request' => ActionRequest::fromHttpRequest(
(new ServerRequest(
HttpMethod::METHOD_GET->value,
new Uri('https://acme.site/')
))->withQueryParams([])
),
'className' => PathController::class,
'methodName' => 'scalarNullableParameterWithoutDefaultEndpointAction',
'expectedParameters' => [
'message' => null,
'number' => null,
Expand All @@ -123,12 +136,7 @@ public static function parameterProvider(): iterable
),
'className' => PathController::class,
'methodName' => 'scalarParameterWithDefaultValuesAction',
'expectedParameters' => [
'message' => 'suppe',
'number' => 42,
'weight' => 666,
'switch' => false,
]
'expectedParameters' => []
];

$multipleParametersRequest = ActionRequest::fromHttpRequest(
Expand Down

0 comments on commit 7a85c61

Please sign in to comment.