MediaWiki master
JsonBodyValidator.php
Go to the documentation of this file.
1<?php
2
4
12
17
21 private $bodyParamSettings;
22
27 public function __construct( array $bodyParamSettings ) {
29 __CLASS__ . ' is deprecated.',
30 '1.43'
31 );
32 $this->bodyParamSettings = $bodyParamSettings;
33 }
34
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 // TODO: use a ParamValidator to check field value, etc!
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
93 public function getOpenAPISpec(): array {
94 $body = [];
95 $required = [];
96
97 // XXX: Maybe we want to be able to define a spec file in the route definition?
98 // NOTE: the route definition may not be loaded when this is called before init()!
99
100 foreach ( $this->bodyParamSettings as $name => $paramSetting ) {
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}
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
JSON formatter wrapper class.
validateBody(RequestInterface $request)
Validate the body of a request.This may return a data structure representing the parsed body....
getOpenAPISpec()
Returns an OpenAPI Schema Object specification structure as an associative array.
static getParameterSpec(string $name, array $paramSetting)
Convert a param settings array into an OpenAPI Parameter Object specification structure.
Value object representing a message parameter that consists of a list of values.
Definition ListParam.php:15
The constants used to specify list types.
Definition ListType.php:9
Value object representing a message for i18n.
Service for formatting and validating API parameters.
A request interface similar to PSR-7's ServerRequestInterface.
getBody()
Gets the body of the message.
Interface for validating a request body.