Page MenuHomePhabricator

MediaWiki REST Framework: Localize requestBody descriptions in OpenAPI document generation
Open, MediumPublic

Description

Problem

The MediaWiki Core REST framework currently does not localize the request body metadata returned by Handler::getRequestSpec().

While the framework recursively translates response body schemas (getResponseBodySchema()) and individual parameters (getParamSettings()), it assigns the requestBody specifications directly to the compiled OpenAPI document. Therefore, developers cannot use x-i18n-description keys to translate request body descriptions, forcing them to write hard-coded English literals in the PHP code.

Technical Details

In includes/Rest/Handler.php, inside the Handler.php method, the request spec is extracted and assigned directly to the spec array:

if ( !in_array( $method, RequestInterface::NO_BODY_METHODS ) ) {                                                     
    $requestBody = $this->getRequestSpec( $method );                                                                 
    if ( $requestBody ) {                                                                                            
        $spec['requestBody'] = $requestBody; // <-- Missing localization                                             
    }                                                                                                                
}

In contrast, the response body spec inside Handler.php passes the schema array through JsonLocalizer::localizeJson() to resolve translations recursively:

$bodySchema = $this->getResponseBodySchema( $method );                                                               
if ( $bodySchema ) {                                                                                                 
    $bodySchema = $this->getJsonLocalizer()->localizeJson( $bodySchema );                                            
    $ok['content']['application/json']['schema'] = $bodySchema;                                                      
}

Proposed Solution

Update Handler::getOpenApiSpec() to pass the request body specification through JsonLocalizer before attaching the request body specification to the final spec:

if ( !in_array( $method, RequestInterface::NO_BODY_METHODS ) ) {                                                     
    $requestBody = $this->getRequestSpec( $method );                                                                 
    if ( $requestBody ) {                                                                                            
-       $spec['requestBody'] = $requestBody;                                                                         
+       $spec['requestBody'] = $this->getJsonLocalizer()->localizeJson( $requestBody );                              
    }                                                                                                                
}

Conditions of Acceptance

  • Keys prefixed with x-i18n-description that are used in the array returned by Handler::getRequestSpec() are successfully translated and replaced with their localized counterparts (e.g., description).
  • Existing hard-coded English strings in getRequestSpec() continue to work as fallbacks without errors.
  • Unit tests are added to verify JsonLocalizer runs on request body specifications.

References