File: JsonBodyValidator.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (123 lines) | stat: -rw-r--r-- 3,109 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php

namespace MediaWiki\Rest\Validator;

use MediaWiki\Json\FormatJson;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\RequestInterface;
use Wikimedia\Message\ListParam;
use Wikimedia\Message\ListType;
use Wikimedia\Message\MessageValue;
use Wikimedia\ParamValidator\ParamValidator;

/**
 * @deprecated since 1.43, return body properties from Handler::getParamSettings().
 */
class JsonBodyValidator implements BodyValidator {

	/**
	 * @var array[]
	 */
	private $bodyParamSettings;

	/**
	 * @deprecated since 1.43, Return body parameters from getBodyParamSettings() instead.
	 * @param array[] $bodyParamSettings
	 */
	public function __construct( array $bodyParamSettings ) {
		wfDeprecatedMsg(
			__CLASS__ . ' is deprecated.',
			'1.43'
		);
		$this->bodyParamSettings = $bodyParamSettings;
	}

	/**
	 * @inheritDoc
	 * @return array
	 */
	public function validateBody( RequestInterface $request ) {
		$jsonStream = $request->getBody();
		$status = FormatJson::parse( "$jsonStream", FormatJson::FORCE_ASSOC );

		if ( !$status->isOK() ) {
			throw new LocalizedHttpException(
				new MessageValue( 'rest-json-body-parse-error', [ "$status" ] ),
				400
			);
		}

		$data = $status->value;

		if ( !is_array( $data ) ) {
			throw new LocalizedHttpException( new MessageValue( 'rest-bad-json-body' ), 400 );
		}

		$uncheckedBodyKeys = array_fill_keys( array_keys( $data ), true );
		foreach ( $this->bodyParamSettings as $name => $settings ) {
			if ( !empty( $settings[ParamValidator::PARAM_REQUIRED] ) && !isset( $data[$name] ) ) {
				throw new LocalizedHttpException(
					new MessageValue( 'rest-missing-body-field', [ $name ] ), 400
				);
			}

			if ( !isset( $data[$name] ) ) {
				$data[$name] = $settings[ParamValidator::PARAM_DEFAULT] ?? null;
			}

			unset( $uncheckedBodyKeys[$name] );
			// TODO: use a ParamValidator to check field value, etc!
		}
		if ( $uncheckedBodyKeys ) {
			throw new LocalizedHttpException(
				new MessageValue(
					'rest-extraneous-body-fields',
					[ new ListParam( ListType::COMMA, array_keys( $uncheckedBodyKeys ) ) ]
				),
				400
			);
		}

		return $data;
	}

	/**
	 * Returns an OpenAPI Schema Object specification structure as an associative array.
	 * @see https://swagger.io/specification/#schema-object
	 *
	 *
	 * This will contain information about the supported parameters.
	 *
	 * @return array
	 */
	public function getOpenAPISpec(): array {
		$body = [];
		$required = [];

		// XXX: Maybe we want to be able to define a spec file in the route definition?
		// NOTE: the route definition may not be loaded when this is called before init()!

		foreach ( $this->bodyParamSettings as $name => $paramSetting ) {
			$param = Validator::getParameterSpec(
				$name,
				$paramSetting
			);

			$body['properties'][$name] = $param['schema'];

			if ( isset( $param['description'] ) ) {
				$body['properties'][$name]['description'] = $param['description'];
			}

			if ( $param['required'] ?? false ) {
				$required[] = $param['name'];
			}
		}

		if ( $required ) {
			$body['required'] = $required;
		}

		return $body;
	}
}