Skip to content

Commit 23fa247

Browse files
authored
Merge pull request #3 from ovr/nullable-checks
Fix: Null checks must be done on nullable === false
2 parents 701f00a + ae8415c commit 23fa247

File tree

1 file changed

+69
-28
lines changed

1 file changed

+69
-28
lines changed

src/SwaggerWrapper.php

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,10 @@ protected function validateScheme(Definition $scheme, JSONPath $jsonPath)
352352
*/
353353
protected function validateProperty(Property $property, $value)
354354
{
355-
if ($property->required && $value === null) {
355+
// Supported inside Swagger 3+ for Property, now will be null as default
356+
$nullable = true;
357+
358+
if ($nullable === false && $value === null) {
356359
throw new RuntimeException(
357360
sprintf(
358361
'Property %s is required and cannot be null',
@@ -365,14 +368,27 @@ protected function validateProperty(Property $property, $value)
365368
case 'array':
366369
if ($property->required) {
367370
if (gettype($value) != $property->type) {
368-
throw new RuntimeException(
369-
sprintf(
370-
'Type of the property %s must be %s instead of %s actual',
371-
$property->property,
372-
$property->type,
373-
gettype($value)
374-
)
375-
);
371+
if ($nullable) {
372+
if ($value !== null) {
373+
throw new RuntimeException(
374+
sprintf(
375+
'Type of the property %s must be %s|null (because nullable) instead of %s actual',
376+
$property->property,
377+
$property->type,
378+
gettype($value)
379+
)
380+
);
381+
}
382+
} else {
383+
throw new RuntimeException(
384+
sprintf(
385+
'Type of the property %s must be %s instead of %s actual',
386+
$property->property,
387+
$property->type,
388+
gettype($value)
389+
)
390+
);
391+
}
376392
}
377393

378394
if ($property->minItems) {
@@ -407,18 +423,30 @@ protected function validateProperty(Property $property, $value)
407423
case 'boolean':
408424
case 'string':
409425
case 'integer':
410-
if ($value !== null && gettype($value) != $property->type) {
411-
throw new RuntimeException(
412-
sprintf(
413-
'Type of the property %s must be %s instead of %s',
414-
$property->property,
415-
$property->type,
416-
gettype($value)
417-
)
418-
);
426+
if (gettype($value) != $property->type) {
427+
if ($nullable) {
428+
if ($value !== null) {
429+
throw new RuntimeException(
430+
sprintf(
431+
'Type of the property %s must be %s|null (because nullable) instead of %s',
432+
$property->property,
433+
$property->type,
434+
gettype($value)
435+
)
436+
);
437+
}
438+
} else {
439+
throw new RuntimeException(
440+
sprintf(
441+
'Type of the property %s must be %s instead of %s',
442+
$property->property,
443+
$property->type,
444+
gettype($value)
445+
)
446+
);
447+
}
419448
}
420449

421-
422450
if ($value !== null) {
423451
if (!$this->checkFormat($property, $value)) {
424452
throw new RuntimeException(
@@ -479,15 +507,28 @@ protected function validateProperty(Property $property, $value)
479507
}
480508
break;
481509
case 'number':
482-
if ($value !== null && gettype($value) != 'double') {
483-
throw new RuntimeException(
484-
sprintf(
485-
'Type of the property %s must be %s instead of %s',
486-
$property->property,
487-
$property->type,
488-
gettype($value)
489-
)
490-
);
510+
if (gettype($value) != 'double') {
511+
if ($nullable) {
512+
if ($value !== null) {
513+
throw new RuntimeException(
514+
sprintf(
515+
'Type of the property %s must be %s|null (because nullable) instead of %s',
516+
$property->property,
517+
$property->type,
518+
gettype($value)
519+
)
520+
);
521+
}
522+
} else {
523+
throw new RuntimeException(
524+
sprintf(
525+
'Type of the property %s must be %s instead of %s',
526+
$property->property,
527+
$property->type,
528+
gettype($value)
529+
)
530+
);
531+
}
491532
}
492533
}
493534

0 commit comments

Comments
 (0)