jenkins-bot has submitted this change and it was merged.

Change subject: API: ApiResult must validate even when using numeric 
auto-indexes
......................................................................


API: ApiResult must validate even when using numeric auto-indexes

Bug: T97490
Change-Id: I5301a615a992b090000a59f86e13b9f78dcd5aec
(cherry picked from commit ce6e288ee85f7989172e5a670df68fd2fd3c151e)
---
M includes/api/ApiResult.php
M tests/phpunit/includes/api/ApiResultTest.php
2 files changed, 94 insertions(+), 4 deletions(-)

Approvals:
  Anomie: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/api/ApiResult.php b/includes/api/ApiResult.php
index 5db8ca7..5bc893e 100644
--- a/includes/api/ApiResult.php
+++ b/includes/api/ApiResult.php
@@ -277,6 +277,10 @@
         * @param int $flags Zero or more OR-ed flags like OVERRIDE | 
ADD_ON_TOP.
         */
        public static function setValue( array &$arr, $name, $value, $flags = 0 
) {
+               if ( !( $flags & ApiResult::NO_VALIDATE ) ) {
+                       $value = self::validateValue( $value );
+               }
+
                if ( $name === null ) {
                        if ( $flags & ApiResult::ADD_ON_TOP ) {
                                array_unshift( $arr, $value );
@@ -284,10 +288,6 @@
                                array_push( $arr, $value );
                        }
                        return;
-               }
-
-               if ( !( $flags & ApiResult::NO_VALIDATE ) ) {
-                       $value = self::validateValue( $value );
                }
 
                $exists = isset( $arr[$name] );
diff --git a/tests/phpunit/includes/api/ApiResultTest.php 
b/tests/phpunit/includes/api/ApiResultTest.php
index 03d058a..8d59415 100644
--- a/tests/phpunit/includes/api/ApiResultTest.php
+++ b/tests/phpunit/includes/api/ApiResultTest.php
@@ -107,8 +107,29 @@
                        );
                }
                try {
+                       ApiResult::setValue( $arr, null, $fh );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
                        $obj->file = $fh;
                        ApiResult::setValue( $arr, 'sub', $obj );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
+                       $obj->file = $fh;
+                       ApiResult::setValue( $arr, null, $obj );
                        $this->fail( 'Expected exception not thrown' );
                } catch ( InvalidArgumentException $ex ) {
                        $this->assertSame(
@@ -130,7 +151,27 @@
                        );
                }
                try {
+                       ApiResult::setValue( $arr, null, INF );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
                        ApiResult::setValue( $arr, 'nan', NAN );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
+                       ApiResult::setValue( $arr, null, NAN );
                        $this->fail( 'Expected exception not thrown' );
                } catch ( InvalidArgumentException $ex ) {
                        $this->assertSame(
@@ -155,10 +196,14 @@
                ApiResult::setValue( $arr, 'foo', "foo\x80bar" );
                ApiResult::setValue( $arr, 'bar', "a\xcc\x81" );
                ApiResult::setValue( $arr, 'baz', 74 );
+               ApiResult::setValue( $arr, null, "foo\x80bar" );
+               ApiResult::setValue( $arr, null, "a\xcc\x81" );
                $this->assertSame( array(
                        'foo' => "foo\xef\xbf\xbdbar",
                        'bar' => "\xc3\xa1",
                        'baz' => 74,
+                       0 => "foo\xef\xbf\xbdbar",
+                       1 => "\xc3\xa1",
                ), $arr );
        }
 
@@ -289,8 +334,29 @@
                        );
                }
                try {
+                       $result->addValue( null, null, $fh );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
                        $obj->file = $fh;
                        $result->addValue( null, 'sub', $obj );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
+                       $obj->file = $fh;
+                       $result->addValue( null, null, $obj );
                        $this->fail( 'Expected exception not thrown' );
                } catch ( InvalidArgumentException $ex ) {
                        $this->assertSame(
@@ -312,7 +378,27 @@
                        );
                }
                try {
+                       $result->addValue( null, null, INF );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
                        $result->addValue( null, 'nan', NAN );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
+               try {
+                       $result->addValue( null, null, NAN );
                        $this->fail( 'Expected exception not thrown' );
                } catch ( InvalidArgumentException $ex ) {
                        $this->assertSame(
@@ -374,10 +460,14 @@
                $result->addValue( null, 'foo', "foo\x80bar" );
                $result->addValue( null, 'bar', "a\xcc\x81" );
                $result->addValue( null, 'baz', 74 );
+               $result->addValue( null, null, "foo\x80bar" );
+               $result->addValue( null, null, "a\xcc\x81" );
                $this->assertSame( array(
                        'foo' => "foo\xef\xbf\xbdbar",
                        'bar' => "\xc3\xa1",
                        'baz' => 74,
+                       0 => "foo\xef\xbf\xbdbar",
+                       1 => "\xc3\xa1",
                        ApiResult::META_TYPE => 'assoc',
                ), $result->getResultData() );
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/207463
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I5301a615a992b090000a59f86e13b9f78dcd5aec
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: REL1_25
Gerrit-Owner: Chad <ch...@wikimedia.org>
Gerrit-Reviewer: Anomie <bjor...@wikimedia.org>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to