Commit graph

192 commits

Author SHA1 Message Date
jenkins-bot
1270ae1bab Merge "Hard-deprecate getBodyValidator" 2024-08-08 16:12:38 +00:00
jenkins-bot
1cbafee5de Merge "[ParsoidParser] Remove unneeded code to set render ID" 2024-07-30 16:54:51 +00:00
jenkins-bot
7e5b96ee7d Merge "Tests on forcing multi-value to be an array in JSON" 2024-07-20 03:42:32 +00:00
C. Scott Ananian
16de2c0851 [ParsoidParser] Remove unneeded code to set render ID
Since I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78 it is part of the
contract of ContentRenderer::getParserOutput() that the render ID (and
other cache parameters) will be set when it returns.
(ContentHandler::getParserOutput() can set them even earlier if it has
custom content-based overrides.)  We had a lot of temporary
backward-compatibility code "later" in the parse process to try to close
the barn door if some code path "forgot" to set them, but these are
unnecessary now.

This patch removes that backward-compatibility code in ParsoidParser;
there is similar remaining code in ParserCache etc. which can be
addressed in follow ups.

(For compatibility we do have to temporarily copy the render ID code
inside ParsoidOutputAccess::parseUncachable, but that class is
deprecated and will be removed.)

The HtmlOutputRendererHelper path which used to call
ParsoidParser::parseFakeRevision() is now replaced with a codepath that
goes through RevisionRenderer.  In order to maintain the same behavior
of the ParsoidHandler, we have also added 'useParsoid' handling to the
JsonContentHandler.  This support can perhaps be deprecated eventually.

Bug: T350538
Change-Id: I0853624cf785f72fd956c6c2336f979f4402a68f
2024-07-19 16:09:32 -04:00
C. Scott Ananian
c23420603c Deprecate Html*Helper::init()
Instead of creating a half-initialized helper and later calling ::init,
provide all the information necessary for the helper in the constructor.
This is facilitated by the fact that there already exists a factory
class, PageRestHelperFactory, which holds all the services required.

This affects:
* HtmlOutputRendererHelper::init()
* HtmlMessageOutputHelper::init()
* HtmlInputTransformHelper::init()

Change-Id: I1e1213597c6be012f2bc024c2b370c968ff3b472
2024-07-19 15:03:37 +00:00
frankie
40e4aaa5cc Tests on forcing multi-value to be an array in JSON
Provides mroe tests for I39a8c50a1cb884fee9807096750789837efdb061

Bug: T368132
Change-Id: I9658a827dbaafd442161901c03ef1514cc36d9b7
2024-07-19 08:48:57 +00:00
C. Scott Ananian
1a1ac6a82d [HtmlOutputRendererHelper] Remove use of ParsoidOutputAccess::parseUncacheable
This removes the last use of ParsoidOutputAccess in core, allowing it
to be deprecated and eventually removed.

Bug: T367074
Bug: T317018
Change-Id: Ica2c880e2e7c2b126aaea66a3e4be460b3f2234f
2024-07-19 03:09:32 -04:00
Translation updater bot
e93d7f0705 Hard-deprecate getBodyValidator
This patch emits deprecation warnings when extensions override
Handler::getBodyValidator() or otherwise use JsonBodyValidator.
Request body validation should be performed based on body parameter
declarations returned from getBodyParamSettings().

Bug: T358560
Depends-On: I8b12c9c9d5a73c620e3c5d035f815d7c951c7b30
Change-Id: I44b3395b8d2489c5e7ec8de0077fd7a7c9f49bfe
2024-07-16 13:06:01 +00:00
jenkins-bot
82d91ed739 Merge "Use MainConfigNames constants in tests where possible" 2024-07-11 12:46:04 +00:00
jenkins-bot
d9f64a6287 Merge "REST: Generate request body specs from param settings." 2024-07-10 19:27:24 +00:00
daniel
e6d0adcc68 REST: Generate request body specs from param settings.
Bug: T323786
Change-Id: I49da2a07efa32759480070c9d93c31d46f211d26
2024-07-10 20:23:56 +02:00
jenkins-bot
aef29a83c3 Merge "param-settings: Remove backward compatibility code from default" 2024-07-10 17:32:21 +00:00
Atieno
2b31f4c46f param-settings: Remove backward compatibility code from default
We no longer have to check for 'body' PARAM_SOURCE from within getParamSettings

Bug: T367394
Change-Id: I16075fde2db6ea47f61d1d2df1e07a4ef46b537b
2024-07-10 19:55:08 +03:00
thiemowmde
4bd95cd96b Use MainConfigNames constants in tests where possible
I believe this makes the code less brittle, and also makes it a bit
more obvious what these strings are meant to represent.

Change-Id: Ia39b5c80af4b495931d0a68fd091b783645dd709
2024-07-10 10:11:22 +00:00
jenkins-bot
c3f8d61ed0 Merge "Add temporary account case for page history count" 2024-07-10 06:51:07 +00:00
STran
9d1f0813a9 Add temporary account case for page history count
Edits made by temporary accounts could not be counted independently
when accessing `v1/page/Test2/history/counts/{type}`.

- Add a `temporary` case for page history revision counts.

Bug: T365673
Change-Id: Ib5279684e2843c84a56eea04721dc62700c12577
2024-07-09 05:12:11 -07:00
jenkins-bot
b3ecbf29ec Merge "REST: Enable strict type validation in JSON request bodies" 2024-07-08 19:29:52 +00:00
Wendy Quarshie
5c61c65541 Normalise string params in MW Rest API
Bug: T340185
Change-Id: I1b7189d5951dea019897955f8f9749afcc325fe0
2024-07-08 17:20:14 +00:00
daniel
2b67cdc907 REST: Enable strict type validation in JSON request bodies
This switches from logging warnings when clients use string instead of
numbers in JSON requests.

Bug: T305973
Depends-On: I17f7d0b89cb584eef5a7203a8f26cb5b100e2abb
Change-Id: I2d00d320f59652040ff914005aad58bf3e724689
2024-07-04 15:34:17 +02:00
daniel
89be6e0c9f REST: detect mismatching value types in json request
DEPLOY: watch the api-warning channel for misbehaving clients.

We are now using TypeDef objects for validating fields in JSON
request bodies. Since TypeDef was designed for use in the action API, it
assumes that all client data is originally supplied as strings. These
strings are parsed and converted to the appropriate type.

But for JSON requests, we don't want that. If a field is defined to be a
boolean, it should be required to be a boolean, not the string "yes" or
"0".

This adds an option to TypeDefs that triggers strict type checks for
booleans and numbers. This option is enabled for all request types other
than form data.

For now, the check does not trigger a validation error. It just logs a
warning. This allows us to assess how often clients would trigger this
kind of error. The warning are logged to the "api-warning" channel.

Bug: T305973
Change-Id: I11e9e37af93bc3b9414eb77095e7cc0ce821a462
2024-06-28 11:10:07 +00:00
Jakob Warkotsch
33cfe4efeb REST: Allow other json request types
This changes parseBodyData() to treat request content types ending in
"+json" just like normal application/json requests in order to support
media types such as application/json-patch+json which is used by the
Wikibase REST API for patch endpoints.

Change-Id: Ic47f8f115ec403cab0a8fdd7d53edcd18d130537
2024-06-26 14:21:16 +02:00
daniel
41daf4d0d5 REST: Deprecate using "post" as the parameter source
Note that "post" parameters remain accessible through
getValidatedParams(), while "body" parameters have to be
accessed though getValidatedBody().

This adds a number of tests that ensure that this remains to be the case,
while deprecation warnings are triggered when appropriate.

Code search to check that this is unused in prod: https://codesearch.wmcloud.org/things/?q=SOURCE+*%3D%3E+*%27post%27&files=&excludeFiles=&repos=

Previous reverted incarnation of this: Ia0eedb383e69b

Bug: T362850
Bug: T358560
Depends-On: Id94335b3ec8f0549ff7350d31cb7cfee8feaa012
Change-Id: I88accc52c5faab70b453709b02ed88a8af4bc181
2024-06-25 14:57:58 +02:00
daniel
79c61e80dc REST: Make module definition files more like OpenAPI specs
This splits RouteFileModule into two classes, ExtraRoutesModule and
SpecBasedModule.

ExtraRoutesModule has no module prefix and supports
only "flat" route definition files and additional routes from
extension.json.

SpecBasedModule represents a single module defined in a definition
file similar to an OpenAPI spec. The idea is that a full OpenAPI spec
can be generated by filling in any missing information based on
information provided by the Handler implementation. In particular, the
definition of parameters and request body schemas will be generated.

A JSON schema for the new file format is added under docs/rest/.

Support for the intermediate format introduced in Iebcde4645d4 is
removed. It was not included in a release and was not being used outside
core tests.

Bug: T366837
Change-Id: I4ce306b0997f80b78a3d901e38bbfa8445bed604
2024-06-24 16:42:59 +02:00
C. Scott Ananian
64d343353e [HtmlOutputRendererHelper] Use ParserOutputAccess
One more step in gradually replacing uses of ParsoidOutputAccess with
ParserOutputAccess.  This is mostly just inlining the code from
ParsoidOutputAccess (which already uses ParserOutputAccess internally)
and then removing dead code.

Change-Id: I87d6568d9dc71bc11f406ea3c31ce86e01667e05
2024-06-05 10:37:36 -04:00
daniel
3a37e8d71b REST: HandlerTestTrait should mock validateBodyParams instead of validateBody.
The validateBody method in validator is based on BodyValidator.
Returning a non-null value from it triggers b/c behavior in
Handler::validate. That should generally be avoided.

Change-Id: I3047cb0b6fdd8ed623b43910e737d8882f333ff6
2024-05-28 09:44:41 +02:00
daniel
5ac1f785d4 REST: improve body validation testing
- fix test for body validation errors in HandlerTest that was never
  called.
- provide additional test cases for body validation failures
- provide an error key and failure code when encountering extra body
  parameters, for consistency with other validation errors.

Bug: T305973
Change-Id: I3453a37f9b5dd1348890046582fb2d25c00dfa6c
2024-05-21 16:57:27 +03:00
daniel
020c7f5e75 REST: introduce getSupportedRequestTypes()
REST handlers should explicitly list content types they support for
request bodies. This is needed for generating OpenAPI specs.

NOTE: this removes default support for form data requests, unless
there are "post" parameters declared. "body" parameters require JSON.

Bug: T362850
Change-Id: I1339b1a1770719bd4a5d4d414a8e55fcbb498ac1
2024-05-15 15:10:25 +02:00
thiemowmde
52ddf3e8ce Remove all @package comments
I don't think these do anything with the documentation generators
we currently use. Especially not in tests. How are tests part of a
"package" when the code is not?

Note how most of these are simply identical to the namespace. They
are most probably auto-generated by some IDEs but don't actually
mean anything.

Change-Id: I771b5f2041a8e3b077865c79cbebddbe028543d1
2024-05-10 13:53:15 +02:00
daniel
91a1741787 Introduce Modules into the REST framework
Modules group together endpoints by a shared prefix. The idea is that each module has its own version and can generated self-contained self-documentation. This allows clients to have clear expectations about the endpoints of each module, no matter what wiki they are accessing. So far, each wiki may be exposing a different set of endpoints, with no way to provide a spec that describes that set of endpoints in a way that would be consistent across wikis and stable over time.

Bug: T362480
Change-Id: Iebcde4645d472d27eee5a30adb6eee12cc7d046b
2024-05-08 16:12:30 +02:00
daniel
97eae33cf5 REST: introduce getBodyParamSettings
Since we introduced support for the "body" PARAM_SOURCE in
getParamSettings, fields in the request body can be defined in the same
way that path and query parameters are defined. However, body fields are
treated separately by the framework, and the value of body fields are
available through getValidatedBody(), rather than getValidatedParams().

Because of that, it makes sense to have a method that returns the param
settings just for the body fields. This also allows handler classes to
override this method separately to specify body fields. That way, it
also becomes possible to have body fields that have the same name as
other parameters.

Bug: T362850
Change-Id: Ia85bf7e46c949a999052d91f1b0d7d579a880108
2024-05-07 17:35:36 +00:00
daniel
c5d29b849a Tests: enable non-integration tests to use TitleFactory service.
This change implements two things that can be combined to allow
the usage of services like the TitleFactory in subclasses of
MediaWikiUnitTestCase:

1) This adds getServiceContainer() to the MediaWikiUnitTestCase base
   class, to mirror the method of the same name on
   MediaWikiIntegrationtest case, along with a a setService() method
   for defining service objects for use in tests.

2) This adds makeMockTitleFactory() to the MockTitleTrait, to allow
   tests to esaily define the TitleFactory service.

Making services available through a service container primarily
benefits code that relies on ObjectFactory, like REST handlers.

Needed-By: Iac113a9e766e38b2d19ae99b7e448548a515469e
Change-Id: Ida6c37c7133b9e3887268025daf6e5b7c1340e52
2024-04-25 13:00:14 +00:00
daniel
c7ee85d4e9 REST: skip unused path params when generating OpenAPI specs
Path parameters cannot be optional in OpenAPI specs: each route has a
fixed set of parameters. But the same REST handler can be used for
multiple routes, with some routes using certain parameters, while others
don't.

So from the handler's perspective, these parameters are optional. That
works fine, until we try to generate an OpenAPI spec from the parameter
definitions that the handler returns from getParamSettings. At that
point, we need to inspect the route that the handler was instantiated
for, to see which parameters are actually used in the path. Any
parameters not used in the path need to be excluded from the generated
spec.

Bug: T359652
Change-Id: I81d2c919c87727614a29f53e1dea498326928ef4
2024-04-24 19:38:28 +00:00
Jforrester
40bcffbce9 Revert "REST: Deprecate using "post" as the parameter source"
This reverts commit 53a16f757b.

Bug: T362817
Change-Id: I2512da5bd375322f79f0ef2cf90832e6cdb382c6
2024-04-17 15:24:52 -04:00
Derick Alangi
791cb9f676
search: Use ServiceOptions not full config object
This patch uses the recommended MW practice for injecting
settings into MW services. Instead of passing in the full
config object, pass in service options which MW service wiring
will use to lookup settings/options from config sources.

So in this patch, the various options have been identified
and put into `CONSTRUCTOR_OPTIONS` constant and inject via
service wiring.

Test plan
=========
Ensure that the special page: `Special:Search` still works
with this patch checkout on your local test wiki.

When you visit `Special:Search` on your local wiki, you should
see a search form view. Type in something like: "Main Page" and
then hit search. If everything works well, then local test passes.

NOTE
====
This patch also resolves a TODO which mentions the removal of
the `getConfig()` method from the SearchEngineConfig class.

Change-Id: Ib5dfc10f3f210c6c35247f4f30f9549dd60e0af7
2024-04-16 15:50:43 +01:00
daniel
53a16f757b REST: Deprecate using "post" as the parameter source
Note that "post" parameters remain accessible through
getValidatedParams(), while "body" parameters have to be
accessed though getValidatedBody().

This adds a numebr of tests that ensure that this remains to be the case, while deprecation warnings are triggered when appropriate.

Bug: T358560
Change-Id: Ia0eedb383e69b48a0baa0895a8c9d2229f670367
2024-04-15 20:02:06 +02:00
daniel
68e1fb24f6 REST: detect extraneous body fields
Throw if a client sends unexpected body fields to a handler that supports body validation.

Bug: T360434
Change-Id: I7b8ed5fe23613183f941ab3b162e0a3c488167df
2024-03-30 20:32:54 +00:00
jenkins-bot
f581b94913 Merge "REST: Wrap parameter validation errors for the request body" 2024-03-28 14:29:10 +00:00
daniel
637ab1165e REST: Wrap parameter validation errors for the request body
Since we are now using the parameter validation mechanism also for
fields in the request body, we need to wrap the validation errors to
make clear that the "parameters" are actually fields on the request
body.

Bug: T358850
Change-Id: I02e59a66459e8d88d1c5d0e454da9139ec1b42c6
2024-03-28 08:38:29 +00:00
Derick Alangi
d372626b97
objectcache: Introduce ObjectCacheFactory MW service
ObjectCache is already doing a lot of factory pattern logic like
creating instances of the various BagOStuff, this should really be
the responsibility of the factory servicet.

This patch introduces a proper factory (ObjectCacheFactory) to handle
the responsibility of creating various instances of BagOStuff. Since
`newFromParams()` is a static function that gets passed in configuration
of $wgObjectCaches, that can stay that way (to keep supporting how we do
this in prod today).

Technical Breaking Change: `ObjectCache::makeLocalServerCache()` now has
a parameter and requires it but there are no callers of this method outside
MW core hence it is safe to change (and this patch update all callers) to
work correctly. Cache prefix is gotten from global state because sometimes
at this stage, the services container is not available.

Bug: T358346
Change-Id: I3179a387486377c6a575d173f39f82870c49c321
2024-03-19 12:38:39 +03:00
daniel
1986f2bfb0 REST: Accept POST with empty body and no content-type
All POST requests must have a body, even if the body's is
empty, with content-length set to 0. When the body is empty, it should
not be necessary to specify a content-type either.

A typical use case would be some kind of "touch" semantics that would
update a resource's state without needing any additional information
from the client.

Change-Id: I7d20bb6a8c484b9b4725f056342ff8d015a171ac
2024-03-14 14:56:05 +01:00
jenkins-bot
6bb297717f Merge "Rest: make getValidatedBody return body parameters" 2024-03-14 10:35:46 +00:00
jenkins-bot
c3c0a60f5e Merge "REST: HandlerTestTrait should set parsed body data" 2024-03-14 10:28:23 +00:00
Bill Pirkle
c3fe4b037d Remove experimental contributions endpoints
These four endpoints have been experimental for many months:
  /coredev/v0/me/contributions
  /coredev/v0/user/{user}/contributions
  /coredev/v0/me/contributions/count
  /coredev/v0/user/{user}/contributions/count

Analytics data shows they are not receiving any traffic.
Remove to clear out the coreDevelopmentRoutes.json file for
unrelated experiments, and to reduce future code maintenance.

Bug: T305506
Change-Id: I21cf9a398638d34725a3c34d348fbad50df40153
2024-03-13 21:22:47 -05:00
Wendy Quarshie
e8699c23d6 Rest: make getValidatedBody return body parameters
Bug: T358850
Change-Id: Icf11749dcddd11e6a9b68f3795923157a108c107
2024-03-13 14:09:46 +00:00
daniel
36b0c8a048 REST: HTML endpoints should support all content models
This allows HtmlOutputRendererHelper to function for all kinds of
content.

Bug: T311728
Bug: T311648
Bug: T359426
Change-Id: Ib32af7cf2a7ad989eb0b13ecca37c857fc9199ec
2024-03-13 04:32:59 -05:00
daniel
ef64f25e10 REST: HandlerTestTrait should set parsed body data
HandlerTestTrait::initHandler needs to emulate the behavior of Router::setBodyData,
so test cases that provide an unparsed body in a RequestData object
don't break when we migrate handlers away from using BodyValidator.

Bug: T358850
Depends-On: Icebf044dd4668b36bc9ba76f11520b7acc3347c0
Change-Id: Ib64dbfa4b76fd5c1cc575c835f63347581cc6a9d
2024-03-12 12:38:34 +00:00
Wendy Quarshie
f4de463f88 Rest router should provide parsed body data to handler.
bug: T358557
Change-Id: I41e81f1166a09a4409319cced20a76dc08c6869e
2024-03-04 12:54:02 +00:00
Atieno
3087ecea75 handler: Throw on unsupported body
Bug: T358557
Change-Id: I62c429521cce24303fa91c3a2e47db77f912b967
2024-02-29 14:23:52 +03:00
Wendy Quarshie
026d8d02a2 RequestInterface: add getParsedBody and setParsedBody.
Adding getParsedBody is intended to make it easier for us to implement proper request body validation. Also, it get us closer to PSR-7.

Bug: T357025
Change-Id: Ib36d22f15f9226f1f088e55bb335d05dbb7a0e6a
2024-02-27 10:21:26 +00:00
Reedy
c8276f731a tests: Add more use statements in namespaced classes
Change-Id: I2629cfcb09fde6f18be824779a2c12c013ea0cb5
2024-02-18 21:31:02 +00:00