Skip to content

Commit

Permalink
TASK: Add special NoSuchParameter return value to the ParameterLocati…
Browse files Browse the repository at this point in the history
…on->resolveParameterFromRequest method that indicates that no value was passed and thus no parameter will be decoded or passed to the action
  • Loading branch information
mficzel committed Jun 17, 2024
1 parent 7a85c61 commit f044d48
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 51 deletions.
13 changes: 8 additions & 5 deletions Classes/Application/ParameterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Neos\Flow\ObjectManagement\Proxy\ProxyInterface;
use Sitegeist\SchemeOnYou\Domain\Metadata\Parameter as ParameterAttribute;
use Sitegeist\SchemeOnYou\Domain\Metadata\RequestBody;
use Sitegeist\SchemeOnYou\Domain\Path\NoSuchParameter;
use Sitegeist\SchemeOnYou\Domain\Schema\SchemaDenormalizer;

class ParameterFactory
Expand Down Expand Up @@ -37,18 +38,20 @@ public static function resolveParameters(string $className, string $methodName,
$requestBodyAttribute = RequestBody::tryFromReflectionParameter($parameter);
if ($requestBodyAttribute) {
$parameterValueFromRequest = $requestBodyAttribute->contentType->resolveParameterFromRequest($request, $parameter->name);
$parameterValueFromRequest = $requestBodyAttribute->contentType->decodeParameterValue($parameterValueFromRequest);
if (!$parameterValueFromRequest instanceof NoSuchParameter) {
$parameterValueFromRequest = $requestBodyAttribute->contentType->decodeParameterValue($parameterValueFromRequest);
}
} else {
$parameterAttribute = ParameterAttribute::fromReflectionParameter($parameter);
$parameterValueFromRequest = $parameterAttribute->in->resolveParameterFromRequest($request, $parameter->name);
$parameterValueFromRequest = $parameterAttribute->style->decodeParameterValue($parameterValueFromRequest);
if (!$parameterValueFromRequest instanceof NoSuchParameter) {
$parameterValueFromRequest = $parameterAttribute->style->decodeParameterValue($parameterValueFromRequest);
}
}

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

return $parameters;
}
}
9 changes: 9 additions & 0 deletions Classes/Domain/Path/NoSuchParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Sitegeist\SchemeOnYou\Domain\Path;

class NoSuchParameter
{

}
12 changes: 6 additions & 6 deletions Classes/Domain/Path/ParameterLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ enum ParameterLocation: string implements \JsonSerializable

/**
* @todo really?
* @return array<mixed>|int|bool|string|float|null
* @return NoSuchParameter|array<mixed>|int|bool|string|float|null
*/
public function resolveParameterFromRequest(ActionRequest $request, string $parameterName): array|int|bool|string|float|null
public function resolveParameterFromRequest(ActionRequest $request, string $parameterName): NoSuchParameter|array|int|bool|string|float|null
{
return match ($this) {
ParameterLocation::LOCATION_PATH => $request->hasArgument($parameterName) ? $request->getArgument($parameterName) : null,
ParameterLocation::LOCATION_QUERY => $request->getHttpRequest()->getQueryParams()[$parameterName] ?? null,
ParameterLocation::LOCATION_HEADER => $request->getHttpRequest()->hasHeader($parameterName) ? $request->getHttpRequest()->getHeader($parameterName) : null,
ParameterLocation::LOCATION_COOKIE => $request->getHttpRequest()->getCookieParams()[$parameterName] ?? null,
ParameterLocation::LOCATION_PATH => $request->hasArgument($parameterName) ? $request->getArgument($parameterName) : new NoSuchParameter(),
ParameterLocation::LOCATION_QUERY => array_key_exists($parameterName,$request->getHttpRequest()->getQueryParams()) ? $request->getHttpRequest()->getQueryParams()[$parameterName] : new NoSuchParameter(),
ParameterLocation::LOCATION_HEADER => $request->getHttpRequest()->hasHeader($parameterName) ? $request->getHttpRequest()->getHeader($parameterName) : new NoSuchParameter(),
ParameterLocation::LOCATION_COOKIE => $request->getHttpRequest()->getCookieParams()[$parameterName] ?? new NoSuchParameter(),
};
}

Expand Down
32 changes: 0 additions & 32 deletions Tests/Fixtures/Controller/PathController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,38 +81,6 @@ 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",
#[OpenApi\Parameter(ParameterLocation::LOCATION_QUERY)]
int $number = 42,
#[OpenApi\Parameter(ParameterLocation::LOCATION_QUERY)]
float $weight = 666,
#[OpenApi\Parameter(ParameterLocation::LOCATION_QUERY)]
bool $switch = false,
): EndpointResponse {
return new EndpointResponse('acknowledged');
}

public function requestBodyAndSingleResponseEndpointAction(
#[OpenApi\RequestBody(RequestBodyContentType::CONTENT_TYPE_JSON)]
EndpointQuery $endpointQuery
Expand Down
40 changes: 32 additions & 8 deletions Tests/Unit/Application/ParameterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static function parameterProvider(): iterable
]
];

yield 'withScalarNullableParameters' => [
yield 'withScalarNullableParametersNoQueryArguments' => [
'request' => ActionRequest::fromHttpRequest(
(new ServerRequest(
HttpMethod::METHOD_GET->value,
Expand All @@ -107,15 +107,23 @@ public static function parameterProvider(): iterable
]
];

yield 'withScalarNullableNoDefaultParameters' => [
yield 'withScalarNullableParametersNullPassed' => [
'request' => ActionRequest::fromHttpRequest(
(new ServerRequest(
HttpMethod::METHOD_GET->value,
new Uri('https://acme.site/')
))->withQueryParams([])
))->withQueryParams([
'message' => null,
'number' => null,
'weight' => null,
'switch' => null,
'dateTime' => null,
'dateTimeImmutable' => null,
'dateInterval' => null
])
),
'className' => PathController::class,
'methodName' => 'scalarNullableParameterWithoutDefaultEndpointAction',
'methodName' => 'scalarNullableParameterEndpointAction',
'expectedParameters' => [
'message' => null,
'number' => null,
Expand All @@ -127,16 +135,32 @@ public static function parameterProvider(): iterable
]
];

yield 'withScalarParametersAndDefaultValues' => [
yield 'withScalarNullableParametersValuePassed' => [
'request' => ActionRequest::fromHttpRequest(
(new ServerRequest(
HttpMethod::METHOD_GET->value,
new Uri('https://acme.site/')
))->withQueryParams([])
))->withQueryParams([
'message' => 'aaa',
'number' => 123,
'weight' => 456.0,
'switch' => 1,
'dateTime' => '2005-08-15T15:52:01+00:00',
'dateTimeImmutable' => '2005-08-15T15:52:01+00:00',
'dateInterval' => 'P7D'
])
),
'className' => PathController::class,
'methodName' => 'scalarParameterWithDefaultValuesAction',
'expectedParameters' => []
'methodName' => 'scalarNullableParameterEndpointAction',
'expectedParameters' => [
'message' => 'aaa',
'number' => 123,
'weight' => 456.0,
'switch' => true,
'dateTime' => \DateTime::createFromFormat(\DateTime::ATOM, '2005-08-15T15:52:01+00:00'),
'dateTimeImmutable' => \DateTimeImmutable::createFromFormat(\DateTime::ATOM, '2005-08-15T15:52:01+00:00'),
'dateInterval' => new \DateInterval("P7D")
]
];

$multipleParametersRequest = ActionRequest::fromHttpRequest(
Expand Down

0 comments on commit f044d48

Please sign in to comment.