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

Fix "capture fields to select" middleware #198

Merged
merged 4 commits into from
Oct 29, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

* `TypeError` thrown in `CaptureFieldsToSelect` middleware, when "select" query parameter is not a string. [#197](https://github.com/aedart/athenaeum/issues/197).

## [8.12.0] - 2024-10-21

### Changed
Expand Down
18 changes: 12 additions & 6 deletions packages/Http/Api/src/Middleware/CaptureFieldsToSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Closure;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

/**
* Capture Fields To Select Middleware
Expand Down Expand Up @@ -97,17 +98,22 @@ protected function cleanup(): void
/**
* Prepares the selected fields
*
* @param string $input Raw query parameter value
* @param mixed $input Raw query parameter value
*
* @return string[]
* @return array
*
* @throws ValidationException
* @throws BadRequestHttpException
*/
protected function prepareSelectedFields(string $input): array
protected function prepareSelectedFields(mixed $input): array
{
$fields = $this->validateSelectedFields(
explode(',', $input)
);
$input = match (true) {
is_string($input) => explode(',', $input),
is_array($input) => $input,
default => throw new BadRequestHttpException(sprintf('Malformed %s query parameter', $this->selectQueryKey))
};

$fields = $this->validateSelectedFields($input);

return array_map(function ($field) {
return strtolower(trim($field));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Aedart\Tests\TestCases\Http\ApiResourcesTestCase;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

/**
* CaptureFieldsToSelectMiddlewareTest
Expand Down Expand Up @@ -158,8 +160,67 @@ public function failsWhenRequestedFieldNamesAreInvalid(): void

$request = new Request([ 'select' => implode(',', $requestedFields) ]);

(new CaptureFieldsToSelect())->handle($request, function () use (&$result) {
(new CaptureFieldsToSelect())->handle($request, function () {
$result = IoCFacade::tryMake(SelectedFieldsCollection::class);
return new Response();
});
}

/**
* @test
*
* @return void
*/
public function doesNotFailWhenFieldsRequestedAsAnArray(): void
{
// @see https://github.com/aedart/athenaeum/issues/197

/** @var SelectedFieldsCollection|null $result */
$result = null;

Route::get('/users', function (Request $request) use (&$result) {
$result = IoCFacade::tryMake(SelectedFieldsCollection::class);

return new Response();
})
->middleware(CaptureFieldsToSelect::class)
->name('users.index');

Route::getRoutes()->refreshNameLookups();

// ------------------------------------------------------------------------------------ //

$url = route('users.index') . '?select[0]=id&select[1]=user&select[2]=details';
$this
->getJson($url)
->assertOk();

// ------------------------------------------------------------------------------------ //

ConsoleDebugger::output($result);

$this->assertNotNull($result, 'no fields collection registered');
$this->assertTrue($result->has('id'), '"id" field not available');
$this->assertTrue($result->has('user'), '"user" field not available');
$this->assertTrue($result->has('details'), '"details" field not available');
}

/**
* @test
*
* @return void
*
* @throws ValidationException
*/
public function failsWhenFieldsRequestedAreMalformed(): void
{
// @see https://github.com/aedart/athenaeum/issues/197

$this->expectException(BadRequestHttpException::class);

$request = new Request([ 'select' => true ]); // NOTE: This should result in a "400 Bad Request" being thrown.

(new CaptureFieldsToSelect())->handle($request, function () {
return new Response();
});
}
Expand Down
Loading