API: Don't return a deprecation warning for default values
authorBrad Jorsch <bjorsch@wikimedia.org>
Thu, 7 Feb 2019 20:32:36 +0000 (15:32 -0500)
committerBrad Jorsch <bjorsch@wikimedia.org>
Thu, 7 Feb 2019 20:32:36 +0000 (15:32 -0500)
If a deprecated parameter has a default value, or a deprecated value is
part of the default value for a parameter, don't give the client a
deprecation warning about it.

Bug: T215548
Change-Id: I980763e3d44fb1b7459c64b175fcaddf5fd44a13

includes/api/ApiBase.php
tests/phpunit/includes/api/ApiBaseTest.php

index 1efd747..21e20c2 100644 (file)
@@ -1157,6 +1157,7 @@ abstract class ApiBase extends ContextSource {
                        }
 
                        $value = $this->getMain()->getCheck( $encParamName );
+                       $provided = $value;
                } elseif ( $type == 'upload' ) {
                        if ( isset( $default ) ) {
                                // Having a default value is not allowed
@@ -1169,6 +1170,7 @@ abstract class ApiBase extends ContextSource {
                                self::dieDebug( __METHOD__, "Multi-values not supported for $encParamName" );
                        }
                        $value = $this->getMain()->getUpload( $encParamName );
+                       $provided = $value->exists();
                        if ( !$value->exists() ) {
                                // This will get the value without trying to normalize it
                                // (because trying to normalize a large binary file
@@ -1183,6 +1185,7 @@ abstract class ApiBase extends ContextSource {
                        }
                } else {
                        $value = $this->getMain()->getVal( $encParamName, $default );
+                       $provided = $this->getMain()->getCheck( $encParamName );
 
                        if ( isset( $value ) && $type == 'namespace' ) {
                                $type = MWNamespace::getValidNamespaces();
@@ -1373,7 +1376,7 @@ abstract class ApiBase extends ContextSource {
                        }
 
                        // Set a warning if a deprecated parameter has been passed
-                       if ( $deprecated && $value !== false ) {
+                       if ( $deprecated && $provided ) {
                                $feature = $encParamName;
                                $m = $this;
                                while ( !$m->isMain() ) {
@@ -1387,7 +1390,7 @@ abstract class ApiBase extends ContextSource {
                        }
 
                        // Set a warning if a deprecated parameter value has been passed
-                       $usedDeprecatedValues = $deprecatedValues && $value !== false
+                       $usedDeprecatedValues = $deprecatedValues && $provided
                                ? array_intersect( array_keys( $deprecatedValues ), (array)$value )
                                : [];
                        if ( $usedDeprecatedValues ) {
index 121820a..8049a47 100644 (file)
@@ -529,12 +529,36 @@ class ApiBaseTest extends ApiTestCase {
                                'foo',
                                [ [ 'apiwarn-deprecation-parameter', 'myParam' ] ],
                        ],
+                       'Deprecated parameter with default, unspecified' => [
+                               null,
+                               [ ApiBase::PARAM_DEPRECATED => true, ApiBase::PARAM_DFLT => 'foo' ],
+                               'foo',
+                               [],
+                       ],
+                       'Deprecated parameter with default, specified' => [
+                               'foo',
+                               [ ApiBase::PARAM_DEPRECATED => true, ApiBase::PARAM_DFLT => 'foo' ],
+                               'foo',
+                               [ [ 'apiwarn-deprecation-parameter', 'myParam' ] ],
+                       ],
                        'Deprecated parameter value' => [
                                'a',
                                [ ApiBase::PARAM_DEPRECATED_VALUES => [ 'a' => true ] ],
                                'a',
                                [ [ 'apiwarn-deprecation-parameter', 'myParam=a' ] ],
                        ],
+                       'Deprecated parameter value as default, unspecified' => [
+                               null,
+                               [ ApiBase::PARAM_DEPRECATED_VALUES => [ 'a' => true ], ApiBase::PARAM_DFLT => 'a' ],
+                               'a',
+                               [],
+                       ],
+                       'Deprecated parameter value as default, specified' => [
+                               'a',
+                               [ ApiBase::PARAM_DEPRECATED_VALUES => [ 'a' => true ], ApiBase::PARAM_DFLT => 'a' ],
+                               'a',
+                               [ [ 'apiwarn-deprecation-parameter', 'myParam=a' ] ],
+                       ],
                        'Multiple deprecated parameter values' => [
                                'a|b|c|d',
                                [ ApiBase::PARAM_DEPRECATED_VALUES =>