Page MenuHomePhabricator

Support response body examples in MediaWiki REST Framework OAD generation
Closed, ResolvedPublic5 Estimated Story Points

Description

The Wikimedia OpenAPI linter requires at least one example or examples field under each media type in a response (wikimedia-response-example-exists rule, severity: warn). The Handler base class provides no standard mechanism for this.

Context

Handler::generateResponseSpec() builds responses['200'] with schema and headers, but never with examples:

'content' => [
    'application/json' => [
        'schema' => [ '$ref' => '...' ]
        // ← no 'example' or 'examples'
    ]
]

A workaround exists — SitemapHandlerBase overrides generateResponseSpec() entirely — but this requires duplicating base class logic and is not a general pattern. It is the reason site/v1 is the only module with response examples today.

20 warnings across all REST modules.

Conditions of acceptance

  • Add a getResponseBodyExample() method (returning null by default) to the Handler base class, overridable by subclasses.
  • When non-null, include the result as example under content['application/json'] in the object generated by generateResponseSpec().
  • Add a unit test verifying the field is included when the method is overridden.

Related tasks

  • T425942 (spike that identified this gap)
  • T420988 (added PARAM_EXAMPLE for parameters — similar pattern)

Event Timeline

KBach lowered the priority of this task from High to Medium.May 27 2026, 11:50 AM

The sitemap endpoints mentioned in the description are a little uncommon in that they return application/xml rather than application/json. We always expected that handlers that do uncommon things would have to override things in uncommon ways (which still seems reasonable to me). I don't think that changes much in this task - we still do want a way to add examples in the general case - but it is worth pointing out that sitemap is a little unusual and therefore we should be careful when considering it as part of the general pattern.

For the proposed getResponseBodyExample() function, are we happy with embedding that directly in code as a php array, or do we want to add support for separating it out as an external file in the same way we do response schemas?

For context, here's an older task where we introduced the response schema files: T376290: Create example implementation for OpenAPI specs referencing response bodies inline . See the associated gerrit change for some discussion on the pros and cons of using an external file. Here's a relevant portion of @aaron 's comment on that change:

So, I can see some upsides:

Easier to use response specs outside of MediaWiki or even PHP.
With some caution, this supports response schema reuse for Handlers returning different subsets of the same collection.
Avoids slight awkwardness of describing JSON objects in PHP.

The downsides:

Slightly slower since the PHP array can use bytecode cache whereas this must read/parse the file.
Fragments aspects of each Handler into more places. Changing the response schema means updating the module(s) using it, so we have a module spec JSON file => handler => JSON file dependency.
Greater surface area for runtime errors (e.g. missing file). Currently mitigated by the mocha tests in CI.

That all seems relevant here too. I note that we've tended to use the external json files to describe response schemas, even though it is just as possible to override Handler::getResponseBodySchema and return them as PHP arrays.

If we support external json files for response examples, the process for creating an example will probably go something like:

  1. call the endpoint in a real-world situation (at least from your local)
  2. copy the json response into a .json file
  3. manually edit the .json file to make the response more example-ish (replace specific page titles with example ones, replace any PII with fake data, etc.)
  4. override a function named something like Handler::getResponseBodyExampleFileName() to reference the file

Of course, generateResponseSpec() would also be overrideable for any handler that preferred to use a PHP array, or had special needs (like sitemap does).

If we're unsure how useful external files would be, we could start the gerrit change without them, and try writing an example for one of the endpoints in core, preferable one with a complex response body, as a PHP array and see how convenient or inconvenient it feels.

AGhirelli-WMF set the point value for this task to 5.May 28 2026, 3:49 PM

Change #1299648 had a related patch set uploaded (by Aghirelli; author: Aghirelli):

[mediawiki/core@master] Rest: Add getResponseBodyExample() hook to Handler base class

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

Change #1299648 merged by jenkins-bot:

[mediawiki/core@master] Rest: Add getResponseBodyExample() to Handler base class

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

Resolving tasks as part of closing the sprint.