Skip to content
This repository

Fix JsonRpc service name #5196

Closed
wants to merge 10 commits into from

4 participants

Blanchon Vincent Marco Pivetta Abdul Malik Ikhsan Maks
Blanchon Vincent

Json Rpc service name can begin with a "_"

Marco Pivetta
Collaborator

@blanchonvincent was this the reason why __get wasn't recognized?

Additionally, I think all valid identifiers should be discovered (see https://github.com/Ocramius/ProxyManager/blob/0.4.1/src/ProxyManager/Generator/Util/UniqueIdentifierGenerator.php#L30 )

That's the regex for a valid PHP class by the way: http://php.net/manual/en/language.oop5.basic.php

Blanchon Vincent

@Ocramius hmm yes and no. Yes because there was a bug, but magic method cannot be seen by the Reflection API.

Marco Pivetta
Collaborator

@blanchonvincent seems to work with http://3v4l.org/lkCHd - is it an old bug?

Blanchon Vincent

@Ocramius hmm I'm think I'm wrong about reflection and magic method

Blanchon Vincent

@Ocramius updated! thank you

Blanchon Vincent

fixed, thank you @samsonasik

library/Zend/Json/Server/Smd/Service.php
((7 lines not shown))
46 48
      */
47  
-    protected $nameRegex = '/^[a-z][a-z0-9.\\\\_]+$/i';
  49
+    protected $nameRegex = '/^(?!^rpc)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\.\\\]*$/i';
1
Maks Collaborator
Maks3w added a note October 03, 2013

I guess that /i is not longer necessary if you add A-Z

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Maks
Collaborator

There is some definition explaining in detail the allowed characters of a method name?

Maks
Collaborator

@blanchonvincent I was expecting some kind of explanation about why did you choose that ranges of characters (7f-ff, etc)

Also reading that small paragraph about reserved method names

Method names that begin with the word rpc followed by a period character (U+002E or ASCII 46) are reserved for rpc-internal methods and extensions and MUST NOT be used for anything else.

As far as I understand rpcFoo is valid but rpc.Foo is reserved (notice the period)

Maks
Collaborator

My point is document enough the regex pattern so we can understand the criteria in the future.

Blanchon Vincent

@Maks3w right ! Look the last commit. Thank you.

Maks
Collaborator

@blanchonvincent The PR looks ok for me. Just a last thing. Why backslash is allowed?

Blanchon Vincent

@Maks3w I d'ont want to make a BC :/

Maks
Collaborator

Good catch @blanchonvincent

Maks
Collaborator

@blanchonvincent Can you remove the trailing spaces which are making Travis-CI to fail?

1) ZendTest/Json/Server/Smd/ServiceTest.php (trailing_spaces)
Coding standards check failed!

Maks Maks3w closed this October 20, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
44  library/Zend/Json/Server/Smd/Service.php
@@ -9,7 +9,6 @@
9 9
 
10 10
 namespace Zend\Json\Server\Smd;
11 11
 
12  
-use Zend\Json\Server;
13 12
 use Zend\Json\Server\Exception\InvalidArgumentException;
14 13
 use Zend\Json\Server\Smd;
15 14
 
@@ -43,8 +42,11 @@ class Service
43 42
     /**
44 43
      * Regex for names
45 44
      * @var string
  45
+     *
  46
+     * @link http://php.net/manual/en/language.oop5.basic.php
  47
+     * @link http://www.jsonrpc.org/specification#request_object
46 48
      */
47  
-    protected $nameRegex = '/^[a-z][a-z0-9.\\\\_]+$/i';
  49
+    protected $nameRegex = '/^(?!^rpc\.)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\.\\\]*$/';
48 50
 
49 51
     /**
50 52
      * Parameter option types
@@ -103,7 +105,7 @@ class Service
103 105
     /**
104 106
      * Constructor
105 107
      *
106  
-     * @param  string|array $spec
  108
+     * @param  string|array             $spec
107 109
      * @throws InvalidArgumentException if no name provided
108 110
      */
109 111
     public function __construct($spec)
@@ -122,7 +124,7 @@ public function __construct($spec)
122 124
     /**
123 125
      * Set object state
124 126
      *
125  
-     * @param  array $options
  127
+     * @param  array   $options
126 128
      * @return Service
127 129
      */
128 130
     public function setOptions(array $options)
@@ -137,13 +139,14 @@ public function setOptions(array $options)
137 139
                 $this->$method($value);
138 140
             }
139 141
         }
  142
+
140 143
         return $this;
141 144
     }
142 145
 
143 146
     /**
144 147
      * Set service name
145 148
      *
146  
-     * @param  string $name
  149
+     * @param  string                   $name
147 150
      * @return Service
148 151
      * @throws InvalidArgumentException
149 152
      */
@@ -154,6 +157,7 @@ public function setName($name)
154 157
             throw new InvalidArgumentException("Invalid name '{$name} provided for service; must follow PHP method naming conventions");
155 158
         }
156 159
         $this->name = $name;
  160
+
157 161
         return $this;
158 162
     }
159 163
 
@@ -172,7 +176,7 @@ public function getName()
172 176
      *
173 177
      * Currently limited to POST
174 178
      *
175  
-     * @param  string $transport
  179
+     * @param  string                   $transport
176 180
      * @throws InvalidArgumentException
177 181
      * @return Service
178 182
      */
@@ -183,6 +187,7 @@ public function setTransport($transport)
183 187
         }
184 188
 
185 189
         $this->transport = $transport;
  190
+
186 191
         return $this;
187 192
     }
188 193
 
@@ -199,12 +204,13 @@ public function getTransport()
199 204
     /**
200 205
      * Set service target
201 206
      *
202  
-     * @param  string $target
  207
+     * @param  string  $target
203 208
      * @return Service
204 209
      */
205 210
     public function setTarget($target)
206 211
     {
207 212
         $this->target = (string) $target;
  213
+
208 214
         return $this;
209 215
     }
210 216
 
@@ -221,7 +227,7 @@ public function getTarget()
221 227
     /**
222 228
      * Set envelope type
223 229
      *
224  
-     * @param  string $envelopeType
  230
+     * @param  string                   $envelopeType
225 231
      * @throws InvalidArgumentException
226 232
      * @return Service
227 233
      */
@@ -232,6 +238,7 @@ public function setEnvelope($envelopeType)
232 238
         }
233 239
 
234 240
         $this->envelope = $envelopeType;
  241
+
235 242
         return $this;
236 243
     }
237 244
 
@@ -248,9 +255,9 @@ public function getEnvelope()
248 255
     /**
249 256
      * Add a parameter to the service
250 257
      *
251  
-     * @param  string|array $type
252  
-     * @param  array $options
253  
-     * @param  int|null $order
  258
+     * @param  string|array             $type
  259
+     * @param  array                    $options
  260
+     * @param  int|null                 $order
254 261
      * @throws InvalidArgumentException
255 262
      * @return Service
256 263
      */
@@ -293,7 +300,7 @@ public function addParam($type, array $options = array(), $order = null)
293 300
      *
294 301
      * Each param should be an array, and should include the key 'type'.
295 302
      *
296  
-     * @param  array $params
  303
+     * @param  array   $params
297 304
      * @return Service
298 305
      */
299 306
     public function addParams(array $params)
@@ -310,18 +317,20 @@ public function addParams(array $params)
310 317
             $order = (array_key_exists('order', $options)) ? $options['order'] : null;
311 318
             $this->addParam($type, $options, $order);
312 319
         }
  320
+
313 321
         return $this;
314 322
     }
315 323
 
316 324
     /**
317 325
      * Overwrite all parameters
318 326
      *
319  
-     * @param  array $params
  327
+     * @param  array   $params
320 328
      * @return Service
321 329
      */
322 330
     public function setParams(array $params)
323 331
     {
324 332
         $this->params = array();
  333
+
325 334
         return $this->addParams($params);
326 335
     }
327 336
 
@@ -348,13 +357,14 @@ public function getParams()
348 357
             }
349 358
         }
350 359
         ksort($params);
  360
+
351 361
         return $params;
352 362
     }
353 363
 
354 364
     /**
355 365
      * Set return type
356 366
      *
357  
-     * @param  string|array $type
  367
+     * @param  string|array             $type
358 368
      * @throws InvalidArgumentException
359 369
      * @return Service
360 370
      */
@@ -370,6 +380,7 @@ public function setReturn($type)
370 380
             throw new InvalidArgumentException("Invalid param type provided ('" . gettype($type) . "')");
371 381
         }
372 382
         $this->return = $type;
  383
+
373 384
         return $this;
374 385
     }
375 386
 
@@ -411,6 +422,7 @@ public function toArray()
411 422
     public function toJson()
412 423
     {
413 424
         $service = array($this->getName() => $this->toArray());
  425
+
414 426
         return \Zend\Json\Json::encode($service);
415 427
     }
416 428
 
@@ -427,8 +439,8 @@ public function __toString()
427 439
     /**
428 440
      * Validate parameter type
429 441
      *
430  
-     * @param string  $type
431  
-     * @param  bool $isReturn
  442
+     * @param  string                   $type
  443
+     * @param  bool                     $isReturn
432 444
      * @return string
433 445
      * @throws InvalidArgumentException
434 446
      */
33  tests/ZendTest/Json/Server/Smd/ServiceTest.php
@@ -47,16 +47,43 @@ public function testConstructorShouldThrowExceptionWhenNoNameSetWhenArrayProvide
47 47
         $service = new Service(null);
48 48
     }
49 49
 
50  
-    public function testSettingNameShouldThrowExceptionWhenContainingInvalidFormat()
  50
+    public function testSettingNameShouldThrowExceptionWhenContainingInvalidFormatStartingWithInt()
51 51
     {
52 52
         $this->setExpectedException('Zend\Json\Server\Exception\InvalidArgumentException', 'Invalid name');
53 53
         $this->service->setName('0ab-?');
54 54
     }
55 55
 
56  
-    public function testSettingNameShouldThrowExceptionWhenContainingInvalidFormatStartingWithInt()
  56
+    public function testSettingNameShouldNotThrowExceptionWhenContainingValidFormatStartingWithUnderscore()
  57
+    {
  58
+        $this->service->setName('_getMyProperty');
  59
+        $this->assertEquals('_getMyProperty', $this->service->getName());
  60
+    }
  61
+
  62
+    public function testSettingNameShouldThrowExceptionWhenContainingInvalidFormatStartingWithRpc()
57 63
     {
58 64
         $this->setExpectedException('Zend\Json\Server\Exception\InvalidArgumentException', 'Invalid name');
59  
-        $this->service->setName('0ab-?');
  65
+        $this->service->setName('rpc.Foo');
  66
+    }
  67
+
  68
+    public function testSettingNameShouldThrowExceptionWhenContainingInvalidFormatStartingWithRpcWithoutPeriodChar()
  69
+    {
  70
+        $this->service->setName('rpcFoo');
  71
+        $this->assertEquals('rpcFoo', $this->service->getName());
  72
+    }
  73
+
  74
+    public function testSettingNameShouldNotThrowExceptionWhenContainingInvalidFormatStartingWithRpcInsensitiveCase()
  75
+    {
  76
+        $this->service->setName('RpcFoo');
  77
+        $this->assertEquals('RpcFoo', $this->service->getName());
  78
+    }
  79
+
  80
+    public function testSettingNameShouldNotThrowExceptionWhenContainingValidFormatContainingRpc()
  81
+    {
  82
+        $this->service->setName('_rpcFoo');
  83
+        $this->assertEquals('_rpcFoo', $this->service->getName());
  84
+
  85
+        $this->service->setName('MyRpcFoo');
  86
+        $this->assertEquals('MyRpcFoo', $this->service->getName());
60 87
     }
61 88
 
62 89
     public function testNameAccessorsShouldWorkWithNormalInput()
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.