Skip to content

Commit d58835e

Browse files
ackintoshwing328
authored andcommitted
[PHP] Improve: Make validation strict (#7724)
* Add test case which reproduce the problem refs #7686 (comment) > 1. We should pass true as 3rd argument of in_array() * Add test case for setter * Strict validation * Update samples * Tweak expected value according to changes in #7723
1 parent 6d88d07 commit d58835e

File tree

8 files changed

+57
-26
lines changed

8 files changed

+57
-26
lines changed

modules/swagger-codegen/src/main/resources/php/model_generic.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
194194
{{#isEnum}}
195195
{{^isContainer}}
196196
$allowedValues = $this->{{getter}}AllowableValues();
197-
if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues)) {
197+
if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues, true)) {
198198
$invalidProperties[] = sprintf(
199199
"invalid value for '{{name}}', must be one of '%s'",
200200
implode("', '", $allowedValues)
@@ -274,7 +274,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
274274
{{#isEnum}}
275275
{{^isContainer}}
276276
$allowedValues = $this->{{getter}}AllowableValues();
277-
if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues)) {
277+
if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues, true)) {
278278
return false;
279279
}
280280
{{/isContainer}}
@@ -344,7 +344,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
344344
{{#isEnum}}
345345
$allowedValues = $this->{{getter}}AllowableValues();
346346
{{^isContainer}}
347-
if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowedValues)) {
347+
if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowedValues, true)) {
348348
throw new \InvalidArgumentException(
349349
sprintf(
350350
"Invalid value for '{{name}}', must be one of '%s'",

samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public function listInvalidProperties()
226226
$invalidProperties = [];
227227

228228
$allowedValues = $this->getJustSymbolAllowableValues();
229-
if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues)) {
229+
if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues, true)) {
230230
$invalidProperties[] = sprintf(
231231
"invalid value for 'just_symbol', must be one of '%s'",
232232
implode("', '", $allowedValues)
@@ -246,7 +246,7 @@ public function valid()
246246
{
247247

248248
$allowedValues = $this->getJustSymbolAllowableValues();
249-
if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues)) {
249+
if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues, true)) {
250250
return false;
251251
}
252252
return true;
@@ -273,7 +273,7 @@ public function getJustSymbol()
273273
public function setJustSymbol($just_symbol)
274274
{
275275
$allowedValues = $this->getJustSymbolAllowableValues();
276-
if (!is_null($just_symbol) && !in_array($just_symbol, $allowedValues)) {
276+
if (!is_null($just_symbol) && !in_array($just_symbol, $allowedValues, true)) {
277277
throw new \InvalidArgumentException(
278278
sprintf(
279279
"Invalid value for 'just_symbol', must be one of '%s'",

samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public function listInvalidProperties()
278278
$invalidProperties = [];
279279

280280
$allowedValues = $this->getEnumStringAllowableValues();
281-
if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues)) {
281+
if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues, true)) {
282282
$invalidProperties[] = sprintf(
283283
"invalid value for 'enum_string', must be one of '%s'",
284284
implode("', '", $allowedValues)
@@ -289,23 +289,23 @@ public function listInvalidProperties()
289289
$invalidProperties[] = "'enum_string_required' can't be null";
290290
}
291291
$allowedValues = $this->getEnumStringRequiredAllowableValues();
292-
if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues)) {
292+
if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues, true)) {
293293
$invalidProperties[] = sprintf(
294294
"invalid value for 'enum_string_required', must be one of '%s'",
295295
implode("', '", $allowedValues)
296296
);
297297
}
298298

299299
$allowedValues = $this->getEnumIntegerAllowableValues();
300-
if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues)) {
300+
if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues, true)) {
301301
$invalidProperties[] = sprintf(
302302
"invalid value for 'enum_integer', must be one of '%s'",
303303
implode("', '", $allowedValues)
304304
);
305305
}
306306

307307
$allowedValues = $this->getEnumNumberAllowableValues();
308-
if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues)) {
308+
if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues, true)) {
309309
$invalidProperties[] = sprintf(
310310
"invalid value for 'enum_number', must be one of '%s'",
311311
implode("', '", $allowedValues)
@@ -325,22 +325,22 @@ public function valid()
325325
{
326326

327327
$allowedValues = $this->getEnumStringAllowableValues();
328-
if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues)) {
328+
if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues, true)) {
329329
return false;
330330
}
331331
if ($this->container['enum_string_required'] === null) {
332332
return false;
333333
}
334334
$allowedValues = $this->getEnumStringRequiredAllowableValues();
335-
if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues)) {
335+
if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues, true)) {
336336
return false;
337337
}
338338
$allowedValues = $this->getEnumIntegerAllowableValues();
339-
if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues)) {
339+
if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues, true)) {
340340
return false;
341341
}
342342
$allowedValues = $this->getEnumNumberAllowableValues();
343-
if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues)) {
343+
if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues, true)) {
344344
return false;
345345
}
346346
return true;
@@ -367,7 +367,7 @@ public function getEnumString()
367367
public function setEnumString($enum_string)
368368
{
369369
$allowedValues = $this->getEnumStringAllowableValues();
370-
if (!is_null($enum_string) && !in_array($enum_string, $allowedValues)) {
370+
if (!is_null($enum_string) && !in_array($enum_string, $allowedValues, true)) {
371371
throw new \InvalidArgumentException(
372372
sprintf(
373373
"Invalid value for 'enum_string', must be one of '%s'",
@@ -400,7 +400,7 @@ public function getEnumStringRequired()
400400
public function setEnumStringRequired($enum_string_required)
401401
{
402402
$allowedValues = $this->getEnumStringRequiredAllowableValues();
403-
if (!in_array($enum_string_required, $allowedValues)) {
403+
if (!in_array($enum_string_required, $allowedValues, true)) {
404404
throw new \InvalidArgumentException(
405405
sprintf(
406406
"Invalid value for 'enum_string_required', must be one of '%s'",
@@ -433,7 +433,7 @@ public function getEnumInteger()
433433
public function setEnumInteger($enum_integer)
434434
{
435435
$allowedValues = $this->getEnumIntegerAllowableValues();
436-
if (!is_null($enum_integer) && !in_array($enum_integer, $allowedValues)) {
436+
if (!is_null($enum_integer) && !in_array($enum_integer, $allowedValues, true)) {
437437
throw new \InvalidArgumentException(
438438
sprintf(
439439
"Invalid value for 'enum_integer', must be one of '%s'",
@@ -466,7 +466,7 @@ public function getEnumNumber()
466466
public function setEnumNumber($enum_number)
467467
{
468468
$allowedValues = $this->getEnumNumberAllowableValues();
469-
if (!is_null($enum_number) && !in_array($enum_number, $allowedValues)) {
469+
if (!is_null($enum_number) && !in_array($enum_number, $allowedValues, true)) {
470470
throw new \InvalidArgumentException(
471471
sprintf(
472472
"Invalid value for 'enum_number', must be one of '%s'",

samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public function listInvalidProperties()
237237
$invalidProperties = [];
238238

239239
$allowedValues = $this->getStatusAllowableValues();
240-
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) {
240+
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues, true)) {
241241
$invalidProperties[] = sprintf(
242242
"invalid value for 'status', must be one of '%s'",
243243
implode("', '", $allowedValues)
@@ -257,7 +257,7 @@ public function valid()
257257
{
258258

259259
$allowedValues = $this->getStatusAllowableValues();
260-
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) {
260+
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues, true)) {
261261
return false;
262262
}
263263
return true;
@@ -380,7 +380,7 @@ public function getStatus()
380380
public function setStatus($status)
381381
{
382382
$allowedValues = $this->getStatusAllowableValues();
383-
if (!is_null($status) && !in_array($status, $allowedValues)) {
383+
if (!is_null($status) && !in_array($status, $allowedValues, true)) {
384384
throw new \InvalidArgumentException(
385385
sprintf(
386386
"Invalid value for 'status', must be one of '%s'",

samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public function listInvalidProperties()
243243
$invalidProperties[] = "'photo_urls' can't be null";
244244
}
245245
$allowedValues = $this->getStatusAllowableValues();
246-
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) {
246+
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues, true)) {
247247
$invalidProperties[] = sprintf(
248248
"invalid value for 'status', must be one of '%s'",
249249
implode("', '", $allowedValues)
@@ -269,7 +269,7 @@ public function valid()
269269
return false;
270270
}
271271
$allowedValues = $this->getStatusAllowableValues();
272-
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues)) {
272+
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues, true)) {
273273
return false;
274274
}
275275
return true;
@@ -416,7 +416,7 @@ public function getStatus()
416416
public function setStatus($status)
417417
{
418418
$allowedValues = $this->getStatusAllowableValues();
419-
if (!is_null($status) && !in_array($status, $allowedValues)) {
419+
if (!is_null($status) && !in_array($status, $allowedValues, true)) {
420420
throw new \InvalidArgumentException(
421421
sprintf(
422422
"Invalid value for 'status', must be one of '%s'",

samples/client/petstore/php/SwaggerClient-php/phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
<filter>
1616
<whitelist processUncoveredFilesFromWhitelist="true">
17-
<directory suffix=".php">./lib\Api</directory>
18-
<directory suffix=".php">./lib\Model</directory>
17+
<directory suffix=".php">./lib/Api</directory>
18+
<directory suffix=".php">./lib/Model</directory>
1919
</whitelist>
2020
</filter>
2121
</phpunit>

samples/client/petstore/php/SwaggerClient-php/test/Model/EnumTestTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ public function testPropertyEnumString()
8383
{
8484
}
8585

86+
/**
87+
* Test attribute "enum_string_required"
88+
*/
89+
public function testPropertyEnumStringRequired()
90+
{
91+
}
92+
8693
/**
8794
* Test attribute "enum_integer"
8895
*/

samples/client/petstore/php/SwaggerClient-php/tests/EnumTestTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ public function testPossibleValues()
1616
$this->assertSame(EnumTest::ENUM_NUMBER_MINUS_1_DOT_2, -1.2);
1717
}
1818

19+
public function testStrictValidation()
20+
{
21+
$enum = new EnumTest([
22+
'enum_string' => 0,
23+
]);
24+
25+
$this->assertFalse($enum->valid());
26+
27+
$expected = [
28+
"invalid value for 'enum_string', must be one of 'UPPER', 'lower', ''",
29+
"'enum_string_required' can't be null",
30+
];
31+
$this->assertSame($expected, $enum->listInvalidProperties());
32+
}
33+
34+
/**
35+
* @expectedException \InvalidArgumentException
36+
*/
37+
public function testThrowExceptionWhenInvalidAmbiguousValueHasPassed()
38+
{
39+
$enum = new EnumTest();
40+
$enum->setEnumString(0);
41+
}
42+
1943
public function testNonRequiredPropertyIsOptional()
2044
{
2145
$enum = new EnumTest([

0 commit comments

Comments
 (0)