Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1426684
JsonBodyValidator.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
JsonBodyValidator.php
View Options
<?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
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 13:34 (1 d, 18 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
e5/6c/114e57132da089e25402de519491
Default Alt Text
JsonBodyValidator.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment