Skip to content

Commit 70ec9dd

Browse files
authored
[php-nextgen] Add Null Check to Model Param Validation (#24787)
* [php-nextgen] Add Null Check to Model Param Validation * regenerate samples
1 parent c27ee27 commit 70ec9dd

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

‎modules/openapi-generator/src/main/resources/php-nextgen/model_generic.mustache‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,44 +294,44 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
294294
{{#hasValidation}}
295295
{{#isString}}
296296
{{#maxLength}}
297-
if ({{#notRequiredOrIsNullable}}!is_null($this->container['{{name}}']) && {{/notRequiredOrIsNullable}}(mb_strlen($this->container['{{name}}']) > {{maxLength}})) {
297+
if (!is_null($this->container['{{name}}']) && (mb_strlen($this->container['{{name}}']) > {{maxLength}})) {
298298
$invalidProperties[] = "invalid value for '{{name}}', the character length must be smaller than or equal to {{{maxLength}}}.";
299299
}
300300

301301
{{/maxLength}}
302302
{{#minLength}}
303-
if ({{#notRequiredOrIsNullable}}!is_null($this->container['{{name}}']) && {{/notRequiredOrIsNullable}}(mb_strlen($this->container['{{name}}']) < {{minLength}})) {
303+
if (!is_null($this->container['{{name}}']) && (mb_strlen($this->container['{{name}}']) < {{minLength}})) {
304304
$invalidProperties[] = "invalid value for '{{name}}', the character length must be bigger than or equal to {{{minLength}}}.";
305305
}
306306

307307
{{/minLength}}
308308
{{/isString}}
309309
{{#maximum}}
310-
if ({{#notRequiredOrIsNullable}}!is_null($this->container['{{name}}']) && {{/notRequiredOrIsNullable}}($this->container['{{name}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) {
310+
if (!is_null($this->container['{{name}}']) && ($this->container['{{name}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) {
311311
$invalidProperties[] = "invalid value for '{{name}}', must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.";
312312
}
313313

314314
{{/maximum}}
315315
{{#minimum}}
316-
if ({{#notRequiredOrIsNullable}}!is_null($this->container['{{name}}']) && {{/notRequiredOrIsNullable}}($this->container['{{name}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) {
316+
if (!is_null($this->container['{{name}}']) && ($this->container['{{name}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) {
317317
$invalidProperties[] = "invalid value for '{{name}}', must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.";
318318
}
319319

320320
{{/minimum}}
321321
{{#pattern}}
322-
if ({{#notRequiredOrIsNullable}}!is_null($this->container['{{name}}']) && {{/notRequiredOrIsNullable}}!preg_match("{{{pattern}}}", $this->container['{{name}}'])) {
322+
if (!is_null($this->container['{{name}}']) && !preg_match("{{{pattern}}}", $this->container['{{name}}'])) {
323323
$invalidProperties[] = "invalid value for '{{name}}', must be conform to the pattern {{{pattern}}}.";
324324
}
325325

326326
{{/pattern}}
327327
{{#maxItems}}
328-
if ({{#notRequiredOrIsNullable}}!is_null($this->container['{{name}}']) && {{/notRequiredOrIsNullable}}(count($this->container['{{name}}']) > {{maxItems}})) {
328+
if (!is_null($this->container['{{name}}']) && (count($this->container['{{name}}']) > {{maxItems}})) {
329329
$invalidProperties[] = "invalid value for '{{name}}', number of items must be less than or equal to {{{maxItems}}}.";
330330
}
331331

332332
{{/maxItems}}
333333
{{#minItems}}
334-
if ({{#notRequiredOrIsNullable}}!is_null($this->container['{{name}}']) && {{/notRequiredOrIsNullable}}(count($this->container['{{name}}']) < {{minItems}})) {
334+
if (!is_null($this->container['{{name}}']) && (count($this->container['{{name}}']) < {{minItems}})) {
335335
$invalidProperties[] = "invalid value for '{{name}}', number of items must be greater than or equal to {{{minItems}}}.";
336336
}
337337

‎samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Model/FormatTest.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,11 @@ public function listInvalidProperties(): array
382382
if ($this->container['number'] === null) {
383383
$invalidProperties[] = "'number' can't be null";
384384
}
385-
if (($this->container['number'] > 543.2)) {
385+
if (!is_null($this->container['number']) && ($this->container['number'] > 543.2)) {
386386
$invalidProperties[] = "invalid value for 'number', must be smaller than or equal to 543.2.";
387387
}
388388

389-
if (($this->container['number'] < 32.1)) {
389+
if (!is_null($this->container['number']) && ($this->container['number'] < 32.1)) {
390390
$invalidProperties[] = "invalid value for 'number', must be bigger than or equal to 32.1.";
391391
}
392392

@@ -419,11 +419,11 @@ public function listInvalidProperties(): array
419419
if ($this->container['password'] === null) {
420420
$invalidProperties[] = "'password' can't be null";
421421
}
422-
if ((mb_strlen($this->container['password']) > 64)) {
422+
if (!is_null($this->container['password']) && (mb_strlen($this->container['password']) > 64)) {
423423
$invalidProperties[] = "invalid value for 'password', the character length must be smaller than or equal to 64.";
424424
}
425425

426-
if ((mb_strlen($this->container['password']) < 10)) {
426+
if (!is_null($this->container['password']) && (mb_strlen($this->container['password']) < 10)) {
427427
$invalidProperties[] = "invalid value for 'password', the character length must be bigger than or equal to 10.";
428428
}
429429

0 commit comments

Comments
 (0)