Skip to content

Commit 0acd9dd

Browse files
committed
REST: Handle invalid field type via json-diff lib
Removes temporary solution in JsonDiffJsonPatchValidator in favour of handling the `InvalidFieldTypeException` thrown by the JsonDiff lib after PR#60 merged upstream. swaggest/json-diff#60 Bug: T320705 Change-Id: I7eb29e574964cffa65d470afb8170a238d6827e0
1 parent 6901c7d commit 0acd9dd

File tree

2 files changed

+7
-27
lines changed

2 files changed

+7
-27
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"psr/log": "^1.1.3",
4040
"psr/http-message": "^1.0.1",
4141
"onoi/message-reporter": "~1.4",
42-
"swaggest/json-diff": "^3.10"
42+
"swaggest/json-diff": "^3.10.4"
4343
},
4444
"require-dev": {
4545
"mediawiki/mediawiki-codesniffer": "38.0.0",

repo/rest-api/src/Infrastructure/JsonDiffJsonPatchValidator.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Wikibase\Repo\RestApi\Infrastructure;
44

55
use Swaggest\JsonDiff\Exception;
6+
use Swaggest\JsonDiff\InvalidFieldTypeException;
67
use Swaggest\JsonDiff\JsonPatch;
78
use Swaggest\JsonDiff\MissingFieldException;
89
use Swaggest\JsonDiff\UnknownOperationException;
@@ -15,39 +16,18 @@
1516
class JsonDiffJsonPatchValidator implements JsonPatchValidator {
1617

1718
public function validate( array $patch ): ?ValidationError {
18-
// TODO: remove foreach checks when upstream PR merged
19-
// https://github.com/swaggest/json-diff/pull/60
20-
foreach ( $patch as $operation ) {
21-
if ( !is_array( $operation ) ) {
22-
return new ValidationError( self::CODE_INVALID );
23-
}
24-
if ( array_key_exists( 'op', $operation ) && !is_string( $operation['op'] ) ) {
25-
return new ValidationError(
26-
self::CODE_INVALID_FIELD_TYPE,
27-
[ self::ERROR_CONTEXT_OPERATION => $operation, self::ERROR_CONTEXT_FIELD => 'op' ]
28-
);
29-
}
30-
if ( array_key_exists( 'path', $operation ) && !is_string( $operation['path'] ) ) {
31-
return new ValidationError(
32-
self::CODE_INVALID_FIELD_TYPE,
33-
[ self::ERROR_CONTEXT_OPERATION => $operation, self::ERROR_CONTEXT_FIELD => 'path' ]
34-
);
35-
}
36-
if ( array_key_exists( 'from', $operation ) && !is_string( $operation['from'] ) ) {
37-
return new ValidationError(
38-
self::CODE_INVALID_FIELD_TYPE,
39-
[ self::ERROR_CONTEXT_OPERATION => $operation, self::ERROR_CONTEXT_FIELD => 'from' ]
40-
);
41-
}
42-
}
43-
4419
try {
4520
JsonPatch::import( $patch );
4621
} catch ( MissingFieldException $e ) {
4722
return new ValidationError(
4823
self::CODE_MISSING_FIELD,
4924
[ self::ERROR_CONTEXT_OPERATION => (array)$e->getOperation(), self::ERROR_CONTEXT_FIELD => $e->getMissingField() ]
5025
);
26+
} catch ( InvalidFieldTypeException $e ) {
27+
return new ValidationError(
28+
self::CODE_INVALID_FIELD_TYPE,
29+
[ self::ERROR_CONTEXT_OPERATION => (array)$e->getOperation(), self::ERROR_CONTEXT_FIELD => $e->getField() ]
30+
);
5131
} catch ( UnknownOperationException $e ) {
5232
return new ValidationError(
5333
self::CODE_INVALID_OPERATION,

0 commit comments

Comments
 (0)