-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add source-specific typed request input #10216
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
Open
memleakd
wants to merge
6
commits into
codeigniter4:4.8
Choose a base branch
from
memleakd:feat/source-explicit-request-input
base: 4.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4aeb95e
feat(http): add source-specific typed request input
memleakd 5cde0cb
trigger CI
memleakd 09150cd
fix(http): allow input dependency in Structarmed
memleakd 6f38ae6
refactor(http): clarify typed request input sources
memleakd 11c2581
refactor(http): reshape typed request input API
memleakd bb1490d
refactor(http): address feedbacks
memleakd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\HTTP; | ||
|
|
||
| use CodeIgniter\HTTP\Exceptions\HTTPException; | ||
| use CodeIgniter\Input\InputData; | ||
| use CodeIgniter\Input\InputDataFactory; | ||
|
|
||
| /** | ||
| * Provides typed input access for request data sources. | ||
| * | ||
| * @see \CodeIgniter\HTTP\RequestInputTest | ||
| */ | ||
| final readonly class RequestInput | ||
| { | ||
| public function __construct( | ||
| private IncomingRequest $request, | ||
| private InputDataFactory $factory, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Returns GET parameters as a typed input object. | ||
| */ | ||
| public function get(): InputData | ||
| { | ||
| $data = $this->request->getGet(); | ||
|
|
||
| return $this->factory->create(is_array($data) ? $data : []); | ||
| } | ||
|
|
||
| /** | ||
| * Returns POST body parameters as a typed input object. | ||
| */ | ||
| public function post(): InputData | ||
| { | ||
| $data = $this->request->getPost(); | ||
|
|
||
| return $this->factory->create(is_array($data) ? $data : []); | ||
| } | ||
|
|
||
| /** | ||
| * Returns JSON body parameters as a typed input object. | ||
| */ | ||
| public function json(): InputData | ||
| { | ||
| $data = $this->request->getJSON(true) ?? []; | ||
|
|
||
| if (! is_array($data)) { | ||
| throw HTTPException::forUnsupportedJSONFormat(); | ||
| } | ||
|
|
||
| return $this->factory->create($data); | ||
| } | ||
|
|
||
| /** | ||
| * Returns raw input parameters as a typed input object. | ||
| */ | ||
| public function raw(): InputData | ||
| { | ||
| return $this->factory->create($this->request->getRawInput()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\HTTP; | ||
|
|
||
| use CodeIgniter\Config\Services; | ||
| use CodeIgniter\HTTP\Exceptions\HTTPException; | ||
| use CodeIgniter\Input\InputData; | ||
| use CodeIgniter\Superglobals; | ||
| use CodeIgniter\Test\CIUnitTestCase; | ||
| use Config\App; | ||
| use PHPUnit\Framework\Attributes\BackupGlobals; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[BackupGlobals(true)] | ||
| #[Group('SeparateProcess')] | ||
| final class RequestInputTest extends CIUnitTestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| Services::injectMock('superglobals', new Superglobals([], [], [], [], [], [])); | ||
| } | ||
|
|
||
| private function createRequest(?App $config = null, ?string $body = null): IncomingRequest | ||
| { | ||
| $config ??= new App(); | ||
|
|
||
| return new IncomingRequest( | ||
| $config, | ||
| new SiteURI($config, ''), | ||
| $body, | ||
| new UserAgent(), | ||
| ); | ||
| } | ||
|
|
||
| public function testInputReturnsRequestInput(): void | ||
| { | ||
| $request = $this->createRequest(); | ||
| $input = $request->input(); | ||
|
|
||
| $this->assertInstanceOf(RequestInput::class, $input); | ||
| } | ||
|
|
||
| public function testInputReturnsSameRequestInputInstance(): void | ||
| { | ||
| $request = $this->createRequest(); | ||
|
|
||
| $this->assertSame($request->input(), $request->input()); | ||
| } | ||
|
|
||
| public function testClonedRequestGetsNewRequestInputInstance(): void | ||
| { | ||
| $request = $this->createRequest(); | ||
| $input = $request->input(); | ||
|
|
||
| $clonedRequest = $request->withMethod(Method::POST); | ||
|
|
||
| $this->assertNotSame($input, $clonedRequest->input()); | ||
| } | ||
|
|
||
| public function testGetReadsGetData(): void | ||
| { | ||
| service('superglobals')->setGet('page', '3'); | ||
| service('superglobals')->setGet('filters', ['active' => 'true']); | ||
| service('superglobals')->setPost('page', '10'); | ||
|
|
||
| $input = $this->createRequest()->input()->get(); | ||
|
|
||
| $this->assertInstanceOf(InputData::class, $input); | ||
| $this->assertSame(3, $input->integer('page')); | ||
| $this->assertTrue($input->boolean('filters.active')); | ||
| $this->assertSame(1, $input->integer('missing', 1)); | ||
| } | ||
|
|
||
| public function testPostReadsPostData(): void | ||
| { | ||
| service('superglobals')->setGet('remember', '0'); | ||
| service('superglobals')->setPost('remember', '1'); | ||
| service('superglobals')->setPost('tags', ['php', 'ci4']); | ||
|
|
||
| $input = $this->createRequest()->input()->post(); | ||
|
|
||
| $this->assertInstanceOf(InputData::class, $input); | ||
| $this->assertTrue($input->boolean('remember')); | ||
| $this->assertSame(['php', 'ci4'], $input->array('tags')); | ||
| } | ||
|
|
||
| public function testJsonReadsJsonBody(): void | ||
| { | ||
| $json = json_encode([ | ||
| 'page' => '4', | ||
| 'filters' => ['active' => 'true'], | ||
| 'nullable' => null, | ||
| ]); | ||
|
|
||
| $input = $this->createRequest(new App(), $json)->input()->json(); | ||
|
|
||
| $this->assertInstanceOf(InputData::class, $input); | ||
| $this->assertSame(4, $input->integer('page')); | ||
| $this->assertTrue($input->boolean('filters.active')); | ||
| $this->assertTrue($input->has('nullable')); | ||
| } | ||
|
|
||
| public function testJsonReturnsEmptyInputForEmptyJsonBody(): void | ||
| { | ||
| $input = $this->createRequest(new App())->input()->json(); | ||
|
|
||
| $this->assertInstanceOf(InputData::class, $input); | ||
| $this->assertFalse($input->has('name')); | ||
| } | ||
|
|
||
| public function testJsonRejectsScalarJsonBody(): void | ||
| { | ||
| $this->expectException(HTTPException::class); | ||
| $this->expectExceptionMessage('The provided JSON format is not supported.'); | ||
|
|
||
| $this->createRequest(new App(), '"hello"')->input()->json(); | ||
| } | ||
|
|
||
| public function testJsonKeepsInvalidJsonError(): void | ||
| { | ||
| $this->expectException(HTTPException::class); | ||
| $this->expectExceptionMessage('Failed to parse JSON string. Error: Syntax error'); | ||
|
|
||
| $this->createRequest(new App(), 'Invalid JSON string')->input()->json(); | ||
| } | ||
|
|
||
| public function testRawReadsRawInputData(): void | ||
| { | ||
| $input = $this->createRequest(new App(), 'title=Hello&published=1')->input()->raw(); | ||
|
|
||
| $this->assertInstanceOf(InputData::class, $input); | ||
| $this->assertSame('Hello', $input->string('title')); | ||
| $this->assertTrue($input->boolean('published')); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <?php | ||
|
|
||
| $page = $request->input()->get()->integer('page', 1); | ||
| $remember = $request->input()->post()->boolean('remember', false); | ||
| $name = $request->input()->json()->string('name'); | ||
| $published = $request->input()->raw()->boolean('published', false); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.