Page MenuHomePhabricator

Rest: make getValidatedBody return body parameters
Closed, ResolvedPublic

Description

With T358558 we now can validate JSON body fields just like path parameters for form data. But getValidatedBody() will not return the result of that validation, it is still based solely on a BodyValidator.

To fix that, some new logic needs to be introduced into Handler::validate(). It should loop over the validated parameters returned by $restValidator->validateParams, and look up the corresponding parameter definition in the array returned by $this->getParamSettings(). In each parameter definition, it should check if the parameter's soruce is "body". If so, it should be added to $this->validatedBody.

NOTE: Note that we must remain compatible with Handler subclasses that use a BodyValidator: for them, $restValidator->validateBody() will return a value. In theory, that could be anything, but in practice, this is an array coming from JsonBodyValidator. If we have an array here as well as "body parameters", what do we do? Should we be nice and try to merge them? What if they overlap, which one wins? Perhaps we should just throw - Handlers have to decide which system they want to use, they can mix them.
NOTE: We need to decide whether mody parameters should be included in $this->validatedParams or not. On the one hand, they are not exactly parameters. On the other hand, we do include "post params", which also come from the body, but get special treatment from the PHP runtime.
NOTE: We need to decide what to do if we have parsed body data that is not covered by any body params. Do we throw an error complaining about extra data in the body? Or do we keep the data unvalidated? How can a handler accept a JSON body that contains a list, rather than an object?

Related Objects

Event Timeline

Change 1009230 had a related patch set uploaded (by WQuarshie; author: WQuarshie):

[mediawiki/core@master] Rest: make getValidatedBody return body parameters

https://gerrit.wikimedia.org/r/1009230

Change 1010496 had a related patch set uploaded (by WQuarshie; author: WQuarshie):

[mediawiki/core@master] rest: use new body validation in creation handler

https://gerrit.wikimedia.org/r/1010496

Change 1010502 had a related patch set uploaded (by Daniel Kinzler; author: Daniel Kinzler):

[mediawiki/core@master] REST: HandlerTestTrait should set parsed body data

https://gerrit.wikimedia.org/r/1010502

Change 1010511 had a related patch set uploaded (by Daniel Kinzler; author: Daniel Kinzler):

[mediawiki/core@master] REST: Wrap parameter validation errors for the request body

https://gerrit.wikimedia.org/r/1010511

Change 1010502 merged by jenkins-bot:

[mediawiki/core@master] REST: HandlerTestTrait should set parsed body data

https://gerrit.wikimedia.org/r/1010502

Change 1009230 merged by jenkins-bot:

[mediawiki/core@master] Rest: make getValidatedBody return body parameters

https://gerrit.wikimedia.org/r/1009230

Change #1010511 merged by jenkins-bot:

[mediawiki/core@master] REST: Wrap parameter validation errors for the request body

https://gerrit.wikimedia.org/r/1010511

Change #1010496 merged by jenkins-bot:

[mediawiki/core@master] rest: use new body validation in creation and update handler

https://gerrit.wikimedia.org/r/1010496