Skip to content

Commit

Permalink
Merge "REST: Refactor edit route handlers validation"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Jun 28, 2024
2 parents 772e783 + 53b0d60 commit fefc8b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MediaWiki\Rest\ResponseInterface;
use MediaWiki\Rest\SimpleHandler;
use MediaWiki\Rest\StringStream;
use MediaWiki\Rest\Validator\Validator;
use Wikibase\Repo\RestApi\Application\UseCases\AddItemAliasesInLanguage\AddItemAliasesInLanguage;
use Wikibase\Repo\RestApi\Application\UseCases\AddItemAliasesInLanguage\AddItemAliasesInLanguageRequest;
use Wikibase\Repo\RestApi\Application\UseCases\AddItemAliasesInLanguage\AddItemAliasesInLanguageResponse;
Expand All @@ -26,7 +27,6 @@
*/
class AddItemAliasesInLanguageRouteHandler extends SimpleHandler {

use AssertContentType;
use AssertValidTopLevelFields;

private const ITEM_ID_PATH_PARAM = 'item_id';
Expand Down Expand Up @@ -77,7 +77,7 @@ public function run( ...$args ): Response {

public function runUseCase( string $itemId, string $languageCode ): Response {
$jsonBody = $this->getValidatedBody();
'@phan-var array $jsonBody'; // guaranteed to be an array per parseBodyData()
'@phan-var array $jsonBody'; // guaranteed to be an array per getBodyParamSettings()

try {
$useCaseResponse = $this->addItemAliases->execute(
Expand All @@ -103,12 +103,9 @@ public function runUseCase( string $itemId, string $languageCode ): Response {
return $this->newSuccessHttpResponse( $useCaseResponse );
}

public function parseBodyData( RequestInterface $request ): ?array {
$this->assertContentType( [ 'application/json' ], $request->getBodyType() ?? 'unknown' );
$body = parent::parseBodyData( $request );
$this->assertValidTopLevelTypes( $body, $this->getBodyParamSettings() );

return $body;
public function validate( Validator $restValidator ): void {
$this->assertValidTopLevelTypes( $this->getRequest()->getParsedBody(), $this->getBodyParamSettings() );
parent::validate( $restValidator );
}

public function getParamSettings(): array {
Expand All @@ -123,6 +120,11 @@ public function getParamSettings(): array {
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => true,
],
];
}

public function getBodyParamSettings(): array {
return [
self::ALIASES_BODY_PARAM => [
self::PARAM_SOURCE => 'body',
ParamValidator::PARAM_TYPE => 'array',
Expand Down
16 changes: 6 additions & 10 deletions repo/rest-api/src/RouteHandlers/CreateItemRouteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use MediaWiki\MediaWikiServices;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\RequestInterface;
use MediaWiki\Rest\Response;
use MediaWiki\Rest\SimpleHandler;
use MediaWiki\Rest\StringStream;
use MediaWiki\Rest\Validator\Validator;
use Wikibase\Repo\RestApi\Application\Serialization\AliasesSerializer;
use Wikibase\Repo\RestApi\Application\Serialization\DescriptionsSerializer;
use Wikibase\Repo\RestApi\Application\Serialization\ItemPartsSerializer;
Expand All @@ -32,7 +32,6 @@
*/
class CreateItemRouteHandler extends SimpleHandler {

use AssertContentType;
use AssertValidTopLevelFields;

private const ITEM_BODY_PARAM = 'item';
Expand Down Expand Up @@ -87,7 +86,7 @@ public function run( ...$args ): Response {

public function runUseCase(): Response {
$jsonBody = $this->getValidatedBody();
'@phan-var array $jsonBody'; // guaranteed to be an array per parseBodyData()
'@phan-var array $jsonBody'; // guaranteed to be an array per getBodyParamSettings()

try {
return $this->newSuccessHttpResponse(
Expand All @@ -106,15 +105,12 @@ public function runUseCase(): Response {
}
}

public function parseBodyData( RequestInterface $request ): ?array {
$this->assertContentType( [ 'application/json' ], $request->getBodyType() ?? 'unknown' );
$body = parent::parseBodyData( $request );
$this->assertValidTopLevelTypes( $body, $this->getBodyParamSettings() );

return $body;
public function validate( Validator $restValidator ): void {
$this->assertValidTopLevelTypes( $this->getRequest()->getParsedBody(), $this->getBodyParamSettings() );
parent::validate( $restValidator );
}

public function getParamSettings(): array {
public function getBodyParamSettings(): array {
return [
self::ITEM_BODY_PARAM => [
self::PARAM_SOURCE => 'body',
Expand Down

0 comments on commit fefc8b6

Please sign in to comment.