Skip to content

Commit

Permalink
Merge "REST: Use new validation mechanism in GetItemDescription"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Sep 18, 2023
2 parents d87345b + f77e476 commit 5b81c29
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare( strict_types = 1 );

namespace Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription;

use Wikibase\Repo\RestApi\Application\UseCases\DeserializedItemIdRequest;
use Wikibase\Repo\RestApi\Application\UseCases\DeserializedLanguageCodeRequest;

/**
* @license GPL-2.0-or-later
*/
interface DeserializedGetItemDescriptionRequest extends DeserializedItemIdRequest, DeserializedLanguageCodeRequest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription;

use Wikibase\DataModel\Entity\ItemId;
use Wikibase\Repo\RestApi\Application\UseCases\GetLatestItemRevisionMetadata;
use Wikibase\Repo\RestApi\Application\UseCases\ItemRedirect;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;
Expand Down Expand Up @@ -32,17 +31,17 @@ public function __construct(
* @throws ItemRedirect
*/
public function execute( GetItemDescriptionRequest $request ): GetItemDescriptionResponse {
$this->validator->assertValidRequest( $request );

$itemId = new ItemId( $request->getItemId() );
$deserializedRequest = $this->validator->validateAndDeserialize( $request );
$itemId = $deserializedRequest->getItemId();
$languageCode = $deserializedRequest->getLanguageCode();

[ $revisionId, $lastModified ] = $this->getRevisionMetadata->execute( $itemId );

$description = $this->itemDescriptionRetriever->getDescription( $itemId, $request->getLanguageCode() );
$description = $this->itemDescriptionRetriever->getDescription( $itemId, $languageCode );
if ( $description === null ) {
throw new UseCaseError(
UseCaseError::DESCRIPTION_NOT_DEFINED,
"Item with the ID {$itemId} does not have a description in the language: {$request->getLanguageCode()}"
"Item with the ID {$itemId} does not have a description in the language: {$languageCode}"
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription;

use Wikibase\Repo\RestApi\Application\UseCases\ItemIdRequest;
use Wikibase\Repo\RestApi\Application\UseCases\LanguageCodeRequest;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseRequest;

/**
* @license GPL-2.0-or-later
*/
class GetItemDescriptionRequest {
class GetItemDescriptionRequest implements UseCaseRequest, ItemIdRequest, LanguageCodeRequest {

private string $itemId;
private string $languageCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,28 @@
<?php declare( strict_types=1 );
<?php declare( strict_types = 1 );

namespace Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription;

use Wikibase\Repo\RestApi\Application\UseCases\RequestValidation\DeserializedRequestAdapter;
use Wikibase\Repo\RestApi\Application\UseCases\RequestValidation\ValidatingRequestDeserializer;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;
use Wikibase\Repo\RestApi\Application\Validation\ItemIdValidator;
use Wikibase\Repo\RestApi\Application\Validation\LanguageCodeValidator;

/**
* @license GPL-2.0-or-later
*/
class GetItemDescriptionValidator {

private ItemIdValidator $itemIdValidator;
private LanguageCodeValidator $languageCodeValidator;
private ValidatingRequestDeserializer $requestDeserializer;

public function __construct(
ItemIdValidator $itemIdValidator,
LanguageCodeValidator $languageCodeValidator
) {
$this->itemIdValidator = $itemIdValidator;
$this->languageCodeValidator = $languageCodeValidator;
public function __construct( ValidatingRequestDeserializer $requestDeserializer ) {
$this->requestDeserializer = $requestDeserializer;
}

/**
* @throws UseCaseError
*/
public function assertValidRequest( GetItemDescriptionRequest $request ): void {
$this->validateItemId( $request->getItemId() );
$this->validateLanguageCode( $request->getLanguageCode() );
public function validateAndDeserialize( GetItemDescriptionRequest $request ): DeserializedGetItemDescriptionRequest {
return new class( $this->requestDeserializer->validateAndDeserialize( $request ) )
extends DeserializedRequestAdapter implements DeserializedGetItemDescriptionRequest {
};
}

/**
* @throws UseCaseError
*/
private function validateItemId( string $itemId ): void {
$validationError = $this->itemIdValidator->validate( $itemId );
if ( $validationError ) {
throw new UseCaseError(
UseCaseError::INVALID_ITEM_ID,
"Not a valid item ID: {$validationError->getContext()[ItemIdValidator::CONTEXT_VALUE]}"
);
}
}

/**
* @throws UseCaseError
*/
private function validateLanguageCode( string $languageCode ): void {
$validationError = $this->languageCodeValidator->validate( $languageCode );
if ( $validationError ) {
throw new UseCaseError(
UseCaseError::INVALID_LANGUAGE_CODE,
"Not a valid language code: {$validationError->getContext()[LanguageCodeValidator::CONTEXT_LANGUAGE_CODE_VALUE]}"
);
}
}

}
6 changes: 1 addition & 5 deletions repo/rest-api/src/WbRestApi.ServiceWiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
use Wikibase\Repo\RestApi\Application\UseCases\SetItemLabel\SetItemLabel;
use Wikibase\Repo\RestApi\Application\UseCases\SetItemLabel\SetItemLabelValidator;
use Wikibase\Repo\RestApi\Application\Validation\EditMetadataValidator;
use Wikibase\Repo\RestApi\Application\Validation\ItemIdValidator;
use Wikibase\Repo\RestApi\Application\Validation\LanguageCodeValidator;
use Wikibase\Repo\RestApi\Application\Validation\StatementValidator;
use Wikibase\Repo\RestApi\Domain\Services\ItemUpdater;
Expand Down Expand Up @@ -232,10 +231,7 @@
WikibaseRepo::getTermLookup( $services ),
WikibaseRepo::getTermsLanguages( $services )
),
new GetItemDescriptionValidator(
new ItemIdValidator(),
new LanguageCodeValidator( WikibaseRepo::getTermsLanguages( $services )->getLanguages() )
)
new GetItemDescriptionValidator( WbRestApi::getValidatingRequestDeserializer( $services ) )
);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
use Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription\GetItemDescriptionResponse;
use Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription\GetItemDescriptionValidator;
use Wikibase\Repo\RestApi\Application\UseCases\GetLatestItemRevisionMetadata;
use Wikibase\Repo\RestApi\Application\UseCases\RequestValidation\ValidatingRequestDeserializer;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseException;
use Wikibase\Repo\RestApi\Application\Validation\ItemIdValidator;
use Wikibase\Repo\RestApi\Application\Validation\LanguageCodeValidator;
use Wikibase\Repo\RestApi\Domain\ReadModel\Description;
use Wikibase\Repo\RestApi\Domain\Services\ItemDescriptionRetriever;
use Wikibase\Repo\WikibaseRepo;
use Wikibase\Repo\Tests\RestApi\Application\UseCases\RequestValidation\TestValidatingRequestFieldDeserializerFactory;

/**
* @covers \Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription\GetItemDescription
Expand Down Expand Up @@ -110,25 +109,12 @@ public function testGivenInvalidItemId_throws(): void {
}
}

public function testGivenInvalidLanguageCode_throwsUseCaseException(): void {
try {
$this->newUseCase()->execute( new GetItemDescriptionRequest( 'Q123', '1e' ) );

$this->fail( 'this should not be reached' );
} catch ( UseCaseError $useCaseEx ) {
$this->assertSame( UseCaseError::INVALID_LANGUAGE_CODE, $useCaseEx->getErrorCode() );
$this->assertSame( 'Not a valid language code: 1e', $useCaseEx->getErrorMessage() );
$this->assertSame( [], $useCaseEx->getErrorContext() );
}
}

private function newUseCase(): GetItemDescription {
return new GetItemDescription(
$this->getRevisionMetadata,
$this->descriptionRetriever,
new GetItemDescriptionValidator(
new ItemIdValidator(),
new LanguageCodeValidator( WikibaseRepo::getTermsLanguages()->getLanguages() )
new ValidatingRequestDeserializer( TestValidatingRequestFieldDeserializerFactory::newFactory() )
)
);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use Wikibase\Repo\RestApi\Application\UseCases\GetItemAliasesInLanguage\DeserializedGetItemAliasesInLanguageRequest;
use Wikibase\Repo\RestApi\Application\UseCases\GetItemAliasesInLanguage\GetItemAliasesInLanguageRequest;
use Wikibase\Repo\RestApi\Application\UseCases\GetItemAliasesInLanguage\GetItemAliasesInLanguageValidator;
use Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription\DeserializedGetItemDescriptionRequest;
use Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription\GetItemDescriptionRequest;
use Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription\GetItemDescriptionValidator;
use Wikibase\Repo\RestApi\Application\UseCases\GetItemDescriptions\DeserializedGetItemDescriptionsRequest;
use Wikibase\Repo\RestApi\Application\UseCases\GetItemDescriptions\GetItemDescriptionsRequest;
use Wikibase\Repo\RestApi\Application\UseCases\GetItemDescriptions\GetItemDescriptionsValidator;
Expand Down Expand Up @@ -110,6 +113,7 @@
* @covers \Wikibase\Repo\RestApi\Application\UseCases\AddPropertyStatement\AddPropertyStatementValidator
* @covers \Wikibase\Repo\RestApi\Application\UseCases\GetProperty\GetPropertyValidator
* @covers \Wikibase\Repo\RestApi\Application\UseCases\GetItemStatement\GetItemStatementValidator
* @covers \Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription\GetItemDescriptionValidator
*
* @group Wikibase
*
Expand Down Expand Up @@ -265,6 +269,11 @@ public function validatorProvider(): Generator {
GetItemStatementRequest::class,
DeserializedGetItemStatementRequest::class,
];
yield [
GetItemDescriptionValidator::class,
GetItemDescriptionRequest::class,
DeserializedGetItemDescriptionRequest::class,
];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class GetItemDescriptionRouteHandlerTest extends MediaWikiIntegrationTestCase {
protected function setUp(): void {
parent::setUp();
$this->setMockPreconditionMiddlewareFactory();
$this->setMockChangeTagsStore();
}

public function testValidSuccessHttpResponse(): void {
Expand Down

0 comments on commit 5b81c29

Please sign in to comment.