Skip to content

Commit

Permalink
Merge pull request #20 from sitegeist/task/nonNullDefaultValues
Browse files Browse the repository at this point in the history
TASK: Handle non `null` default values correctly
  • Loading branch information
mficzel authored Jun 17, 2024
2 parents f7d2033 + f39bc8f commit 68df7c8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 14 deletions.
15 changes: 10 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,16 +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 instanceof NoSuchParameter) {
$parameters[$parameter->name] = SchemaDenormalizer::denormalizeValue($parameterValueFromRequest, $type->getName(), $parameter);
}

$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
6 changes: 3 additions & 3 deletions Classes/Domain/Path/RequestBodyContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ enum RequestBodyContentType: 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) {
RequestBodyContentType::CONTENT_TYPE_JSON => (string)$request->getHttpRequest()->getBody(),
RequestBodyContentType::CONTENT_TYPE_FORM => $request->getArgument($parameterName)
RequestBodyContentType::CONTENT_TYPE_FORM => $request->hasArgument($parameterName) ? $request->getArgument($parameterName) : new NoSuchParameter()
};
}

Expand Down
69 changes: 69 additions & 0 deletions Tests/Unit/Application/ParameterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,75 @@ public static function parameterProvider(): iterable
]
];

yield 'withScalarNullableParametersNoQueryArguments' => [
'request' => ActionRequest::fromHttpRequest(
(new ServerRequest(
HttpMethod::METHOD_GET->value,
new Uri('https://acme.site/')
))->withQueryParams([])
),
'className' => PathController::class,
'methodName' => 'scalarNullableParameterEndpointAction',
'expectedParameters' => [
]
];

yield 'withScalarNullableParametersNullPassed' => [
'request' => ActionRequest::fromHttpRequest(
(new ServerRequest(
HttpMethod::METHOD_GET->value,
new Uri('https://acme.site/')
))->withQueryParams([
'message' => null,
'number' => null,
'weight' => null,
'switch' => null,
'dateTime' => null,
'dateTimeImmutable' => null,
'dateInterval' => null
])
),
'className' => PathController::class,
'methodName' => 'scalarNullableParameterEndpointAction',
'expectedParameters' => [
'message' => null,
'number' => null,
'weight' => null,
'switch' => null,
'dateTime' => null,
'dateTimeImmutable' => null,
'dateInterval' => null
]
];

yield 'withScalarNullableParametersValuePassed' => [
'request' => ActionRequest::fromHttpRequest(
(new ServerRequest(
HttpMethod::METHOD_GET->value,
new Uri('https://acme.site/')
))->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' => '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(
(new ServerRequest(
HttpMethod::METHOD_GET->value,
Expand Down

0 comments on commit 68df7c8

Please sign in to comment.