Skip to content

Commit f7890c7

Browse files
authored
Merge pull request #120 from DoclerLabs/fix-array-to-string-conversion
fix maxItems and minItems constraint array to string conversion
2 parents ce591a3 + e7b1c73 commit f7890c7

File tree

8 files changed

+46
-24
lines changed

8 files changed

+46
-24
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.9.1] - 2024.12.29
8+
### Fixed
9+
- Array to string conversion in maxItems in minItems constraints
10+
711
## [10.9.0] - 2024.08.16
812
### Added
913
- `INCLUDE_TAGS` and `EXCLUDE_TAGS` to whitelist/blacklist client generation based on operation tags

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ $ docker run -it \
5757
dhlabs/api-client-generator
5858
```
5959

60+
> if you're running this command on Windows you might need to use single backslash instead in -e NAMESPACE
61+
6062
### Without Docker
6163
Preconditions: PHP 7.4
6264

src/Generator/MutatorAccessorClassGeneratorAbstract.php

+25-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use DoclerLabs\ApiClientException\RequestValidationException;
88
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
99
use DoclerLabs\ApiClientGenerator\Entity\Constraint\ConstraintInterface;
10+
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MaxItemsConstraint;
11+
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MinItemsConstraint;
1012
use DoclerLabs\ApiClientGenerator\Entity\Field;
1113
use DoclerLabs\ApiClientGenerator\Input\Specification;
1214
use DoclerLabs\ApiClientGenerator\Naming\SchemaNaming;
@@ -137,15 +139,29 @@ protected function generateConstraints(Field $root): array
137139
continue;
138140
}
139141

140-
$propertyVar = $this->builder->var($root->getPhpVariableName());
141-
$exceptionMessage = $this->builder->funcCall(
142-
'sprintf',
143-
[
144-
'Invalid %s value. Given: `%s`. ' . $constraint->getExceptionMessage(),
145-
$root->getName(),
146-
$propertyVar,
147-
]
148-
);
142+
$propertyVar = $this->builder->var($root->getPhpVariableName());
143+
144+
if (
145+
$constraint instanceof MaxItemsConstraint
146+
|| $constraint instanceof MinItemsConstraint
147+
) {
148+
$exceptionMessage = $this->builder->funcCall(
149+
'sprintf',
150+
[
151+
'Invalid %s value. ' . $constraint->getExceptionMessage(),
152+
$root->getName(),
153+
]
154+
);
155+
} else {
156+
$exceptionMessage = $this->builder->funcCall(
157+
'sprintf',
158+
[
159+
'Invalid %s value. Given: `%s`. ' . $constraint->getExceptionMessage(),
160+
$root->getName(),
161+
$propertyVar,
162+
]
163+
);
164+
}
149165

150166
$ifConditionExpr = $constraint->getIfCondition($propertyVar, $this->builder);
151167

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class Item implements SerializableInterface, JsonSerializable
143143
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 $mandatoryAnyOf, $mandatoryNullableStringWithMinMaxLength)
144144
{
145145
if (count($mandatoryArrayWithMinItems) < 1) {
146-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
146+
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
147147
}
148148
if ($mandatoryNullableStringWithMinMaxLength !== null && grapheme_strlen($mandatoryNullableStringWithMinMaxLength) < 1) {
149149
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
@@ -286,10 +286,10 @@ public function setOptionalMixedArray(array $optionalMixedArray): self
286286
public function setOptionalArrayWithMinMaxItems(array $optionalArrayWithMinMaxItems): self
287287
{
288288
if (count($optionalArrayWithMinMaxItems) < 1) {
289-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
289+
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'optionalArrayWithMinMaxItems'));
290290
}
291291
if (count($optionalArrayWithMinMaxItems) > 5) {
292-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected max items: `5`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
292+
throw new RequestValidationException(sprintf('Invalid %s value. Expected max items: `5`.', 'optionalArrayWithMinMaxItems'));
293293
}
294294
$this->optionalArrayWithMinMaxItems = $optionalArrayWithMinMaxItems;
295295
$this->optionalPropertyChanged['optionalArrayWithMinMaxItems'] = true;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class Item implements SerializableInterface, JsonSerializable
140140
public function __construct(int $mandatoryInteger, string $mandatoryString, string $mandatoryEnum, DateTimeInterface $mandatoryDate, ?DateTimeInterface $mandatoryNullableDate, float $mandatoryFloat, bool $mandatoryBoolean, array $mandatoryArray, array $mandatoryArrayWithMinItems, ItemMandatoryObject $mandatoryObject, ?MandatoryNullableObjectWithAllOf $mandatoryNullableObjectWithAllOf, $mandatoryMixed, MandatoryAnyOf $mandatoryAnyOf, ?string $mandatoryNullableStringWithMinMaxLength)
141141
{
142142
if (count($mandatoryArrayWithMinItems) < 1) {
143-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
143+
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
144144
}
145145
if ($mandatoryNullableStringWithMinMaxLength !== null && grapheme_strlen($mandatoryNullableStringWithMinMaxLength) < 1) {
146146
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
@@ -277,10 +277,10 @@ public function setOptionalMixedArray(array $optionalMixedArray): self
277277
public function setOptionalArrayWithMinMaxItems(array $optionalArrayWithMinMaxItems): self
278278
{
279279
if (count($optionalArrayWithMinMaxItems) < 1) {
280-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
280+
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'optionalArrayWithMinMaxItems'));
281281
}
282282
if (count($optionalArrayWithMinMaxItems) > 5) {
283-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected max items: `5`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
283+
throw new RequestValidationException(sprintf('Invalid %s value. Expected max items: `5`.', 'optionalArrayWithMinMaxItems'));
284284
}
285285
$this->optionalArrayWithMinMaxItems = $optionalArrayWithMinMaxItems;
286286
$this->optionalPropertyChanged['optionalArrayWithMinMaxItems'] = true;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class Item implements SerializableInterface, JsonSerializable
105105
public function __construct(int $mandatoryInteger, string $mandatoryString, string $mandatoryEnum, DateTimeInterface $mandatoryDate, ?DateTimeInterface $mandatoryNullableDate, float $mandatoryFloat, bool $mandatoryBoolean, array $mandatoryArray, array $mandatoryArrayWithMinItems, ItemMandatoryObject $mandatoryObject, ?MandatoryNullableObjectWithAllOf $mandatoryNullableObjectWithAllOf, $mandatoryMixed, MandatoryAnyOf $mandatoryAnyOf, ?string $mandatoryNullableStringWithMinMaxLength)
106106
{
107107
if (count($mandatoryArrayWithMinItems) < 1) {
108-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
108+
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
109109
}
110110
if ($mandatoryNullableStringWithMinMaxLength !== null && grapheme_strlen($mandatoryNullableStringWithMinMaxLength) < 1) {
111111
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
@@ -242,10 +242,10 @@ public function setOptionalMixedArray(array $optionalMixedArray): self
242242
public function setOptionalArrayWithMinMaxItems(array $optionalArrayWithMinMaxItems): self
243243
{
244244
if (count($optionalArrayWithMinMaxItems) < 1) {
245-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
245+
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'optionalArrayWithMinMaxItems'));
246246
}
247247
if (count($optionalArrayWithMinMaxItems) > 5) {
248-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected max items: `5`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
248+
throw new RequestValidationException(sprintf('Invalid %s value. Expected max items: `5`.', 'optionalArrayWithMinMaxItems'));
249249
}
250250
$this->optionalArrayWithMinMaxItems = $optionalArrayWithMinMaxItems;
251251
$this->optionalPropertyChanged['optionalArrayWithMinMaxItems'] = true;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Item implements SerializableInterface, JsonSerializable
7777
public function __construct(private int $mandatoryInteger, private string $mandatoryString, private string $mandatoryEnum, private DateTimeInterface $mandatoryDate, private ?DateTimeInterface $mandatoryNullableDate, private float $mandatoryFloat, private bool $mandatoryBoolean, private array $mandatoryArray, private array $mandatoryArrayWithMinItems, private ItemMandatoryObject $mandatoryObject, private ?MandatoryNullableObjectWithAllOf $mandatoryNullableObjectWithAllOf, private mixed $mandatoryMixed, private MandatoryAnyOf $mandatoryAnyOf, private ?string $mandatoryNullableStringWithMinMaxLength)
7878
{
7979
if (count($mandatoryArrayWithMinItems) < 1) {
80-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
80+
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
8181
}
8282
if ($mandatoryNullableStringWithMinMaxLength !== null && grapheme_strlen($mandatoryNullableStringWithMinMaxLength) < 1) {
8383
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
@@ -200,10 +200,10 @@ public function setOptionalMixedArray(array $optionalMixedArray): self
200200
public function setOptionalArrayWithMinMaxItems(array $optionalArrayWithMinMaxItems): self
201201
{
202202
if (count($optionalArrayWithMinMaxItems) < 1) {
203-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
203+
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'optionalArrayWithMinMaxItems'));
204204
}
205205
if (count($optionalArrayWithMinMaxItems) > 5) {
206-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected max items: `5`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
206+
throw new RequestValidationException(sprintf('Invalid %s value. Expected max items: `5`.', 'optionalArrayWithMinMaxItems'));
207207
}
208208
$this->optionalArrayWithMinMaxItems = $optionalArrayWithMinMaxItems;
209209
$this->optionalPropertyChanged['optionalArrayWithMinMaxItems'] = true;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Item implements SerializableInterface, JsonSerializable
6969
public function __construct(private readonly int $mandatoryInteger, private readonly string $mandatoryString, private readonly ItemMandatoryEnum $mandatoryEnum, private readonly DateTimeInterface $mandatoryDate, private readonly ?DateTimeInterface $mandatoryNullableDate, private readonly float $mandatoryFloat, private readonly bool $mandatoryBoolean, private readonly array $mandatoryArray, private readonly array $mandatoryArrayWithMinItems, private readonly ItemMandatoryObject $mandatoryObject, private readonly ?MandatoryNullableObjectWithAllOf $mandatoryNullableObjectWithAllOf, private readonly mixed $mandatoryMixed, private readonly MandatoryAnyOf $mandatoryAnyOf, private readonly ?string $mandatoryNullableStringWithMinMaxLength)
7070
{
7171
if (count($mandatoryArrayWithMinItems) < 1) {
72-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
72+
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
7373
}
7474
if ($mandatoryNullableStringWithMinMaxLength !== null && grapheme_strlen($mandatoryNullableStringWithMinMaxLength) < 1) {
7575
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
@@ -192,10 +192,10 @@ public function setOptionalMixedArray(array $optionalMixedArray): self
192192
public function setOptionalArrayWithMinMaxItems(array $optionalArrayWithMinMaxItems): self
193193
{
194194
if (count($optionalArrayWithMinMaxItems) < 1) {
195-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
195+
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'optionalArrayWithMinMaxItems'));
196196
}
197197
if (count($optionalArrayWithMinMaxItems) > 5) {
198-
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected max items: `5`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
198+
throw new RequestValidationException(sprintf('Invalid %s value. Expected max items: `5`.', 'optionalArrayWithMinMaxItems'));
199199
}
200200
$this->optionalArrayWithMinMaxItems = $optionalArrayWithMinMaxItems;
201201
$this->optionalPropertyChanged['optionalArrayWithMinMaxItems'] = true;

0 commit comments

Comments
 (0)