Skip to content

Commit 1ee2611

Browse files
authored
Merge pull request #91 from negruhorea/master
Add null verification for nullable properties before validating constraints
2 parents 3a7205a + c730a17 commit 1ee2611

File tree

6 files changed

+227
-49
lines changed

6 files changed

+227
-49
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [10.1.1] - 2023.07.13
8+
### Fixed
9+
- null verification for nullable properties before validating constraints
10+
711
## [10.1.0] - 2023.03.21
812
### Added
913
- support for optional nullable scheme properties

src/Generator/MutatorAccessorClassGeneratorAbstract.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,17 @@ protected function generateConstraints(Field $root): array
147147
]
148148
);
149149

150+
$ifConditionExpr = $constraint->getIfCondition($propertyVar, $this->builder);
151+
152+
if ($root->isNullable()) {
153+
$ifConditionExpr = $this->builder->and(
154+
$this->builder->notEquals($propertyVar, $this->builder->val(null)),
155+
$ifConditionExpr
156+
);
157+
}
158+
150159
$stmts[] = $this->builder->if(
151-
$constraint->getIfCondition($propertyVar, $this->builder),
160+
$ifConditionExpr,
152161
[
153162
$this->builder->throw('RequestValidationException', $exceptionMessage),
154163
]

test/suite/functional/Generator/Schema/ItemPhp70.php

+74-16
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class Item implements SerializableInterface, JsonSerializable
6161

6262
private $mandatoryAnyOf;
6363

64+
/** @var string|null */
65+
private $mandatoryNullableStringWithMinMaxLength;
66+
6467
/** @var int|null */
6568
private $optionalInteger;
6669

@@ -118,18 +121,22 @@ class Item implements SerializableInterface, JsonSerializable
118121
/** @var EmbeddedObject|null */
119122
private $optionalObject;
120123

124+
/** @var string|null */
125+
private $optionalNullableStringWithMinMaxLength;
126+
121127
/** @var array */
122-
private $optionalPropertyChanged = ['optionalInteger' => false, 'optionalString' => false, 'optionalEnum' => false, 'optionalIntEnum' => false, 'optionalDate' => false, 'optionalFloat' => false, 'optionalBoolean' => false, 'optionalNullableBoolean' => false, 'optionalArray' => false, 'optionalNullableArray' => false, 'optionalMixedArray' => false, 'optionalArrayWithMinMaxItems' => false, 'optionalStringWithMinMaxLength' => false, 'optionalStringWithPattern' => false, 'optionalIntegerBetweenIncluded' => false, 'optionalIntegerBetweenExcluded' => false, 'optionalNumberBetweenIncluded' => false, 'optionalNumberBetweenExcluded' => false, 'optionalObject' => false];
128+
private $optionalPropertyChanged = ['optionalInteger' => false, 'optionalString' => false, 'optionalEnum' => false, 'optionalIntEnum' => false, 'optionalDate' => false, 'optionalFloat' => false, 'optionalBoolean' => false, 'optionalNullableBoolean' => false, 'optionalArray' => false, 'optionalNullableArray' => false, 'optionalMixedArray' => false, 'optionalArrayWithMinMaxItems' => false, 'optionalStringWithMinMaxLength' => false, 'optionalStringWithPattern' => false, 'optionalIntegerBetweenIncluded' => false, 'optionalIntegerBetweenExcluded' => false, 'optionalNumberBetweenIncluded' => false, 'optionalNumberBetweenExcluded' => false, 'optionalObject' => false, 'optionalNullableStringWithMinMaxLength' => false];
123129

124130
/**
125131
* @param DateTimeInterface|null $mandatoryNullableDate
126132
* @param string[] $mandatoryArray
127133
* @param string[] $mandatoryArrayWithMinItems
128134
* @param MandatoryNullableObjectWithAllOf|null $mandatoryNullableObjectWithAllOf
135+
* @param string|null $mandatoryNullableStringWithMinMaxLength
129136
*
130137
* @throws RequestValidationException
131138
*/
132-
public function __construct(int $mandatoryInteger, string $mandatoryString, string $mandatoryEnum, DateTimeInterface $mandatoryDate, $mandatoryNullableDate, float $mandatoryFloat, bool $mandatoryBoolean, array $mandatoryArray, array $mandatoryArrayWithMinItems, ItemMandatoryObject $mandatoryObject, $mandatoryNullableObjectWithAllOf, $mandatoryMixed, $mandatoryAnyOf)
139+
public function __construct(int $mandatoryInteger, string $mandatoryString, string $mandatoryEnum, DateTimeInterface $mandatoryDate, $mandatoryNullableDate, float $mandatoryFloat, bool $mandatoryBoolean, array $mandatoryArray, array $mandatoryArrayWithMinItems, ItemMandatoryObject $mandatoryObject, $mandatoryNullableObjectWithAllOf, $mandatoryMixed, $mandatoryAnyOf, $mandatoryNullableStringWithMinMaxLength)
133140
{
134141
$this->mandatoryInteger = $mandatoryInteger;
135142
$this->mandatoryString = $mandatoryString;
@@ -147,6 +154,13 @@ public function __construct(int $mandatoryInteger, string $mandatoryString, stri
147154
$this->mandatoryNullableObjectWithAllOf = $mandatoryNullableObjectWithAllOf;
148155
$this->mandatoryMixed = $mandatoryMixed;
149156
$this->mandatoryAnyOf = $mandatoryAnyOf;
157+
if ($mandatoryNullableStringWithMinMaxLength !== null && \grapheme_strlen($mandatoryNullableStringWithMinMaxLength) < 1) {
158+
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
159+
}
160+
if ($mandatoryNullableStringWithMinMaxLength !== null && \grapheme_strlen($mandatoryNullableStringWithMinMaxLength) > 5) {
161+
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Length should be less than 5.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
162+
}
163+
$this->mandatoryNullableStringWithMinMaxLength = $mandatoryNullableStringWithMinMaxLength;
150164
}
151165

152166
public function setOptionalInteger(int $optionalInteger): self
@@ -375,6 +389,25 @@ public function setOptionalObject(EmbeddedObject $optionalObject): self
375389
return $this;
376390
}
377391

392+
/**
393+
* @param string|null $optionalNullableStringWithMinMaxLength
394+
*
395+
* @throws RequestValidationException
396+
*/
397+
public function setOptionalNullableStringWithMinMaxLength($optionalNullableStringWithMinMaxLength): self
398+
{
399+
if ($optionalNullableStringWithMinMaxLength !== null && \grapheme_strlen($optionalNullableStringWithMinMaxLength) < 1) {
400+
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'optionalNullableStringWithMinMaxLength', $optionalNullableStringWithMinMaxLength));
401+
}
402+
if ($optionalNullableStringWithMinMaxLength !== null && \grapheme_strlen($optionalNullableStringWithMinMaxLength) > 5) {
403+
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Length should be less than 5.', 'optionalNullableStringWithMinMaxLength', $optionalNullableStringWithMinMaxLength));
404+
}
405+
$this->optionalNullableStringWithMinMaxLength = $optionalNullableStringWithMinMaxLength;
406+
$this->optionalPropertyChanged['optionalNullableStringWithMinMaxLength'] = true;
407+
408+
return $this;
409+
}
410+
378411
public function hasOptionalInteger(): bool
379412
{
380413
return $this->optionalPropertyChanged['optionalInteger'];
@@ -470,6 +503,11 @@ public function hasOptionalObject(): bool
470503
return $this->optionalPropertyChanged['optionalObject'];
471504
}
472505

506+
public function hasOptionalNullableStringWithMinMaxLength(): bool
507+
{
508+
return $this->optionalPropertyChanged['optionalNullableStringWithMinMaxLength'];
509+
}
510+
473511
public function getMandatoryInteger(): int
474512
{
475513
return $this->mandatoryInteger;
@@ -547,6 +585,14 @@ public function getMandatoryAnyOf()
547585
return $this->mandatoryAnyOf;
548586
}
549587

588+
/**
589+
* @return string|null
590+
*/
591+
public function getMandatoryNullableStringWithMinMaxLength()
592+
{
593+
return $this->mandatoryNullableStringWithMinMaxLength;
594+
}
595+
550596
/**
551597
* @return int|null
552598
*/
@@ -699,22 +745,31 @@ public function getOptionalObject()
699745
return $this->optionalObject;
700746
}
701747

748+
/**
749+
* @return string|null
750+
*/
751+
public function getOptionalNullableStringWithMinMaxLength()
752+
{
753+
return $this->optionalNullableStringWithMinMaxLength;
754+
}
755+
702756
public function toArray(): array
703757
{
704-
$fields = [];
705-
$fields['mandatoryInteger'] = $this->mandatoryInteger;
706-
$fields['mandatoryString'] = $this->mandatoryString;
707-
$fields['mandatoryEnum'] = $this->mandatoryEnum;
708-
$fields['mandatoryDate'] = $this->mandatoryDate->format(DATE_RFC3339);
709-
$fields['mandatoryNullableDate'] = $this->mandatoryNullableDate !== null ? $this->mandatoryNullableDate->format(DATE_RFC3339) : null;
710-
$fields['mandatoryFloat'] = $this->mandatoryFloat;
711-
$fields['mandatoryBoolean'] = $this->mandatoryBoolean;
712-
$fields['mandatoryArray'] = $this->mandatoryArray;
713-
$fields['mandatoryArrayWithMinItems'] = $this->mandatoryArrayWithMinItems;
714-
$fields['mandatoryObject'] = $this->mandatoryObject->toArray();
715-
$fields['mandatoryNullableObjectWithAllOf'] = $this->mandatoryNullableObjectWithAllOf !== null ? $this->mandatoryNullableObjectWithAllOf->toArray() : null;
716-
$fields['mandatoryMixed'] = $this->mandatoryMixed;
717-
$fields['mandatoryAnyOf'] = $this->mandatoryAnyOf;
758+
$fields = [];
759+
$fields['mandatoryInteger'] = $this->mandatoryInteger;
760+
$fields['mandatoryString'] = $this->mandatoryString;
761+
$fields['mandatoryEnum'] = $this->mandatoryEnum;
762+
$fields['mandatoryDate'] = $this->mandatoryDate->format(DATE_RFC3339);
763+
$fields['mandatoryNullableDate'] = $this->mandatoryNullableDate !== null ? $this->mandatoryNullableDate->format(DATE_RFC3339) : null;
764+
$fields['mandatoryFloat'] = $this->mandatoryFloat;
765+
$fields['mandatoryBoolean'] = $this->mandatoryBoolean;
766+
$fields['mandatoryArray'] = $this->mandatoryArray;
767+
$fields['mandatoryArrayWithMinItems'] = $this->mandatoryArrayWithMinItems;
768+
$fields['mandatoryObject'] = $this->mandatoryObject->toArray();
769+
$fields['mandatoryNullableObjectWithAllOf'] = $this->mandatoryNullableObjectWithAllOf !== null ? $this->mandatoryNullableObjectWithAllOf->toArray() : null;
770+
$fields['mandatoryMixed'] = $this->mandatoryMixed;
771+
$fields['mandatoryAnyOf'] = $this->mandatoryAnyOf;
772+
$fields['mandatoryNullableStringWithMinMaxLength'] = $this->mandatoryNullableStringWithMinMaxLength;
718773
if ($this->hasOptionalInteger()) {
719774
$fields['optionalInteger'] = $this->optionalInteger;
720775
}
@@ -772,6 +827,9 @@ public function toArray(): array
772827
if ($this->hasOptionalObject()) {
773828
$fields['optionalObject'] = $this->optionalObject->toArray();
774829
}
830+
if ($this->hasOptionalNullableStringWithMinMaxLength()) {
831+
$fields['optionalNullableStringWithMinMaxLength'] = $this->optionalNullableStringWithMinMaxLength;
832+
}
775833

776834
return $fields;
777835
}

0 commit comments

Comments
 (0)