| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\Validator; |
| 4 |
|
| 5 |
use MediaWiki\Json\FormatJson; |
| 6 |
use MediaWiki\Rest\LocalizedHttpException; |
| 7 |
use MediaWiki\Rest\RequestInterface; |
| 8 |
use Wikimedia\Message\ListParam; |
| 9 |
use Wikimedia\Message\ListType; |
| 10 |
use Wikimedia\Message\MessageValue; |
| 11 |
use Wikimedia\ParamValidator\ParamValidator; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class JsonBodyValidator implements BodyValidator { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
private $bodyParamSettings; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function __construct( array $bodyParamSettings ) { |
| 28 |
wfDeprecatedMsg( |
| 29 |
__CLASS__ . ' is deprecated.', |
| 30 |
'1.43' |
| 31 |
); |
| 32 |
$this->bodyParamSettings = $bodyParamSettings; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function validateBody( RequestInterface $request ) { |
| 40 |
$jsonStream = $request->getBody(); |
| 41 |
$status = FormatJson::parse( "$jsonStream", FormatJson::FORCE_ASSOC ); |
| 42 |
|
| 43 |
if ( !$status->isOK() ) { |
| 44 |
throw new LocalizedHttpException( |
| 45 |
new MessageValue( 'rest-json-body-parse-error', [ "$status" ] ), |
| 46 |
400 |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
$data = $status->value; |
| 51 |
|
| 52 |
if ( !is_array( $data ) ) { |
| 53 |
throw new LocalizedHttpException( new MessageValue( 'rest-bad-json-body' ), 400 ); |
| 54 |
} |
| 55 |
|
| 56 |
$uncheckedBodyKeys = array_fill_keys( array_keys( $data ), true ); |
| 57 |
foreach ( $this->bodyParamSettings as $name => $settings ) { |
| 58 |
if ( !empty( $settings[ParamValidator::PARAM_REQUIRED] ) && !isset( $data[$name] ) ) { |
| 59 |
throw new LocalizedHttpException( |
| 60 |
new MessageValue( 'rest-missing-body-field', [ $name ] ), 400 |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
if ( !isset( $data[$name] ) ) { |
| 65 |
$data[$name] = $settings[ParamValidator::PARAM_DEFAULT] ?? null; |
| 66 |
} |
| 67 |
|
| 68 |
unset( $uncheckedBodyKeys[$name] ); |
| 69 |
|
| 70 |
} |
| 71 |
if ( $uncheckedBodyKeys ) { |
| 72 |
throw new LocalizedHttpException( |
| 73 |
new MessageValue( |
| 74 |
'rest-extraneous-body-fields', |
| 75 |
[ new ListParam( ListType::COMMA, array_keys( $uncheckedBodyKeys ) ) ] |
| 76 |
), |
| 77 |
400 |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
return $data; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function getOpenAPISpec(): array { |
| 94 |
$body = []; |
| 95 |
$required = []; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
foreach ( $this->bodyParamSettings as $name => $paramSetting ) { |
| 101 |
$param = Validator::getParameterSpec( |
| 102 |
$name, |
| 103 |
$paramSetting |
| 104 |
); |
| 105 |
|
| 106 |
$body['properties'][$name] = $param['schema']; |
| 107 |
|
| 108 |
if ( isset( $param['description'] ) ) { |
| 109 |
$body['properties'][$name]['description'] = $param['description']; |
| 110 |
} |
| 111 |
|
| 112 |
if ( $param['required'] ?? false ) { |
| 113 |
$required[] = $param['name']; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if ( $required ) { |
| 118 |
$body['required'] = $required; |
| 119 |
} |
| 120 |
|
| 121 |
return $body; |
| 122 |
} |
| 123 |
} |