Page MenuHomePhabricator

REST API vNext: validate REST API error response body against published schemas
Open, In Progress, HighPublic8 Estimated Story Points

Description

Once the schemas are published and served from schema.wikimedia.org(see T438208), reference them from the MediaWiki REST API codebase so the error response body produced by the error formatters is validated against the matching schema (V1 / V2 / RESTBase-compat).

Architectural note: Router selects/constructs ErrorFormatter based on errorSchemaVersion; ResponseFactory is where the formatter's array becomes the actual JSON body (createHttpError/createFromException/createLocalizedHttpError → encodeJson).
Validation needs to look at ResponseFactory's output, not just the formatter in isolation, since handlers can inject extra keys via HttpException::getErrorData().

Open question to fill in: validate at CI/test time, or at runtime?

Depends on: T438207, T438208

Event Timeline

Hi @AGhirelli-WMF, please associate one or more active project tags with this task (via the Add Action... → Change Project Tags dropdown). That will allow to see a task when looking at project workboards or searching for tasks in certain projects, and get notified about a task when watching a related project tag. Thanks.

AGhirelli-WMF renamed this task from r to REST API vNext: validate REST API error response body against published schemas.Sep 16 2026, 4:46 PM
AGhirelli-WMF claimed this task.
AGhirelli-WMF updated the task description. (Show Details)
AGhirelli-WMF added subscribers: HCoplin-WMF, daniel.

Hi @AGhirelli-WMF, please associate one or more active project tags with this task (via the Add Action... → Change Project Tags dropdown). That will allow to see a task when looking at project workboards or searching for tasks in certain projects, and get notified about a task when watching a related project tag. Thanks.

Sorry Andre, I misclicked enter before finishing to fill the task 😄

Disclaimer: @AGhirelli-WMF and I discussed this synchronously. The following comment reflects my recollection of that back-and-forth collaborative discussion and not just my own independent thoughts.

The task, as I understand it is to:

  • make certain schemas accessible via schema.wikimedia.org that (among other uses) need to be referenced by MediaWiki core in tests
  • possibly relocate some existing schemas currently stored in MediaWiki core to schema.wikimedia.org

As a developer, I'd prefer the schemas to be stored directly in MediaWiki. This way, they're always available and always in sync. If they're only accessible via an HTTP request to schema.wikimedia.org, then using them in tests requires (at least):

  • my network configuration allows me to make external requests from tests
  • my internet connection is actually functional at the moment I'm running tests
  • schema.wikimedia.org is accessible to me (outages, rate limits, and false positives on blocking happen)
  • the available schema matches the code I'm trying to test

There's quite a bit there that can go wrong. For example, I might be a corporate developer working on an internal network with certain restrictions I can't control. Or I might be a volunteer working on a MediaWiki patch from a country that places restrictions on my access to the public internet. Or be on a metered connection with per-byte charges in a financial situation or region of the world where such things are a real financial challenge to me.

Beyond that, if I'm working on code that requires a new, still-formative version of the schemas, how do I develop against it? I don't want to publish a half-baked schema on schema.wikimedia.org.

However, a shared schema repository has definite advantages, including:

  • more accessible to most callers than a schema in the code (imagine I'm not a MW developer, just someone who wants to call a MW API. I don't want to have to check out the code just to get a schema)
  • smoother for cross-project API implementations. Not everything is (or should be) in core, or even core+extensions. External services may legitimately want to publish schemas, and asking callers to even know which repos to check out would be challenging.
  • discoverability and availability in search engines and AI assistants

One possibility: have a schema git repo that's the source of truth for both tests and schema.wikimedia.org. Developers can check it out locally, so no network connection is necessary at the time tests are executed. And any bandwidth concern is mitigated by this being a one-time checkout rather than a repeated call. Maybe git submodules are involved somehow? Haven't thought this all the way through, this is still just a vague idea.

There's still the question of drift between the source-of-truth repo and the codebase. Any time things are divided, there's a chance they become out of sync. And if I'm working on a change that requires a new schema (or new schema version) how do I manage that between the schema repo and the code repo? As I understand it, the schema repo would likely be in gitlab, while MW is of course in gerrit. I'm not aware of a way to do cross-repo dependencies between gitlab and gerrit, so keeping the repos in sync would be a manual process (maybe I'm wrong about that?)

None of the above is intended as a criticism of this task's intent, which I agree with. I'm just trying to think through some pitfalls and practical considerations.

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

[operations/puppet@production] eventschemas: allow any origin for the MediaWiki REST API schemas

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

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

[mediawiki/tools/api-testing@master] openApi: Add useModuleSpec() to validate against a module's OpenAPI spec

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

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

[mediawiki/core@master] Add external-schemas/ with local copies of the REST error schemas

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

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

[mediawiki/core@master] api-testing: Load REST module specs with openApi.useModuleSpec()

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

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

[mediawiki/core@master] REST: Reference the published error schemas from the OpenAPI spec

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

Change #1355904 merged by jenkins-bot:

[mediawiki/tools/api-testing@master] openApi: Add useModuleSpec() to validate against a module's OpenAPI spec

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

Change #1355910 merged by jenkins-bot:

[mediawiki/core@master] maintenance: Add syncExternalSchemas and local copies of schemas

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

@BPirkle all of your thoughts about schema development and testing are exactly what we went through in T201643: RFC: Modern Event Platform: Schema Registry. See also https://diff.wikimedia.org/2020/09/17/wikimedias-event-data-platform-json-event-schemas/.

For development and testing in code repos, we usually advise people to check out the schema repo and add it as a git submodule to their code repo. Ideally, the schema loading library used can configure the 'base URI' used when looking up schemas by their schema $id. $id's are required to match the schema file's path in the repository, so that $refs always work when configured correctly with a base URI. The only con of this is that your schema lookup library has to know how to combine the configured base URI with a schema $id / path to construct the full lookup URI.

example

schema_uris = [
        os.path.join("file://", project_root_path, "event-schemas", "primary", "jsonschema"),
    ]

Where event-schemas/primary is a local file path to the checked out git submodule schema repo. And, fixture event data has "$schema": "/mediawiki/page/rendering_content_change/1.0.0".

This allows us to $ref (via $schema or otherwise) schemas in usages exactly the same way independent of where the schema is actually hosted.

@AGhirelli-WMF I'm ignorant about maintenance: Add syncExternalSchemas and local copies of schemas (1355910), but did yall consider just using git submodules?

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

[mediawiki/tools/api-testing@master] Prepare 1.9.0 release

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

@BPirkle all of your thoughts about schema development and testing are exactly what we went through in T201643: RFC: Modern Event Platform: Schema Registry. See also https://diff.wikimedia.org/2020/09/17/wikimedias-event-data-platform-json-event-schemas/.

For development and testing in code repos, we usually advise people to check out the schema repo and add it as a git submodule to their code repo. Ideally, the schema loading library used can configure the 'base URI' used when looking up schemas by their schema $id. $id's are required to match the schema file's path in the repository, so that $refs always work when configured correctly with a base URI. The only con of this is that your schema lookup library has to know how to combine the configured base URI with a schema $id / path to construct the full lookup URI.

example

schema_uris = [
        os.path.join("file://", project_root_path, "event-schemas", "primary", "jsonschema"),
    ]

Where event-schemas/primary is a local file path to the checked out git submodule schema repo. And, fixture event data has "$schema": "/mediawiki/page/rendering_content_change/1.0.0".

This allows us to $ref (via $schema or otherwise) schemas in usages exactly the same way independent of where the schema is actually hosted.

@AGhirelli-WMF I'm ignorant about maintenance: Add syncExternalSchemas and local copies of schemas (1355910), but did yall consider just using git submodules?

Thanks Andrew! We did think about submodules, but they like overkill for just 3 small files that only tests use, especially since core master has no submodules today and adding the first one would affect every clone and CI job.

The approach is the same as yours though: the local copies live at the path of their URL, and the resolver maps https:// to that directory, so refs are written exactly as in production and tests never hit the network.

resolver maps https:// to that directory, so refs are written exactly as in production

I'd also worry about hardcoding the schema.wikimedia.org domain into the $refs. Would it be better to be able to resolve the schemas strictly by their schema $id? Or I guess, you are also hardcoding the schema.wikimedia.org site into the $id?

Change #1354903 merged by Clément Goubert:

[operations/puppet@production] eventschemas: allow CORS requests from any origin

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

Change #1366348 merged by jenkins-bot:

[mediawiki/tools/api-testing@master] Prepare 1.9.0 release

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

Change #1355911 merged by jenkins-bot:

[mediawiki/core@master] api-testing: Load REST module specs with openApi.useModuleSpec()

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

The schemas don't hardcode the host: their $id is relative (just the file name). The host is only in the $ref in MediaWiki's OpenAPI spec, and it has to be absolute there, because generic OpenAPI tooling (e.g. Swagger UI) resolves $refs as URLs and has no configurable base URI.