Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix maxItems and minItems constraint array to string conversion #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [10.9.1] - 2024.12.29
### Fixed
- Array to string conversion in maxItems in minItems constraints

## [10.9.0] - 2024.08.16
### Added
- `INCLUDE_TAGS` and `EXCLUDE_TAGS` to whitelist/blacklist client generation based on operation tags
Expand Down
34 changes: 25 additions & 9 deletions src/Generator/MutatorAccessorClassGeneratorAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use DoclerLabs\ApiClientException\RequestValidationException;
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder;
use DoclerLabs\ApiClientGenerator\Entity\Constraint\ConstraintInterface;
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MaxItemsConstraint;
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MinItemsConstraint;
use DoclerLabs\ApiClientGenerator\Entity\Field;
use DoclerLabs\ApiClientGenerator\Input\Specification;
use DoclerLabs\ApiClientGenerator\Naming\SchemaNaming;
Expand Down Expand Up @@ -137,15 +139,29 @@ protected function generateConstraints(Field $root): array
continue;
}

$propertyVar = $this->builder->var($root->getPhpVariableName());
$exceptionMessage = $this->builder->funcCall(
'sprintf',
[
'Invalid %s value. Given: `%s`. ' . $constraint->getExceptionMessage(),
$root->getName(),
$propertyVar,
]
);
$propertyVar = $this->builder->var($root->getPhpVariableName());

if (
$constraint instanceof MaxItemsConstraint
|| $constraint instanceof MinItemsConstraint
) {
$exceptionMessage = $this->builder->funcCall(
'sprintf',
[
'Invalid %s value. ' . $constraint->getExceptionMessage(),
$root->getName(),
]
);
} else {
$exceptionMessage = $this->builder->funcCall(
'sprintf',
[
'Invalid %s value. Given: `%s`. ' . $constraint->getExceptionMessage(),
$root->getName(),
$propertyVar,
]
);
}

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

Expand Down
6 changes: 3 additions & 3 deletions test/suite/functional/Generator/Schema/ItemPhp70.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Item implements SerializableInterface, JsonSerializable
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)
{
if (count($mandatoryArrayWithMinItems) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
}
if ($mandatoryNullableStringWithMinMaxLength !== null && grapheme_strlen($mandatoryNullableStringWithMinMaxLength) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
Expand Down Expand Up @@ -286,10 +286,10 @@ public function setOptionalMixedArray(array $optionalMixedArray): self
public function setOptionalArrayWithMinMaxItems(array $optionalArrayWithMinMaxItems): self
{
if (count($optionalArrayWithMinMaxItems) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'optionalArrayWithMinMaxItems'));
}
if (count($optionalArrayWithMinMaxItems) > 5) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected max items: `5`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected max items: `5`.', 'optionalArrayWithMinMaxItems'));
}
$this->optionalArrayWithMinMaxItems = $optionalArrayWithMinMaxItems;
$this->optionalPropertyChanged['optionalArrayWithMinMaxItems'] = true;
Expand Down
6 changes: 3 additions & 3 deletions test/suite/functional/Generator/Schema/ItemPhp72.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Item implements SerializableInterface, JsonSerializable
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)
{
if (count($mandatoryArrayWithMinItems) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
}
if ($mandatoryNullableStringWithMinMaxLength !== null && grapheme_strlen($mandatoryNullableStringWithMinMaxLength) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
Expand Down Expand Up @@ -277,10 +277,10 @@ public function setOptionalMixedArray(array $optionalMixedArray): self
public function setOptionalArrayWithMinMaxItems(array $optionalArrayWithMinMaxItems): self
{
if (count($optionalArrayWithMinMaxItems) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'optionalArrayWithMinMaxItems'));
}
if (count($optionalArrayWithMinMaxItems) > 5) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected max items: `5`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected max items: `5`.', 'optionalArrayWithMinMaxItems'));
}
$this->optionalArrayWithMinMaxItems = $optionalArrayWithMinMaxItems;
$this->optionalPropertyChanged['optionalArrayWithMinMaxItems'] = true;
Expand Down
6 changes: 3 additions & 3 deletions test/suite/functional/Generator/Schema/ItemPhp74.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Item implements SerializableInterface, JsonSerializable
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)
{
if (count($mandatoryArrayWithMinItems) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
}
if ($mandatoryNullableStringWithMinMaxLength !== null && grapheme_strlen($mandatoryNullableStringWithMinMaxLength) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
Expand Down Expand Up @@ -242,10 +242,10 @@ public function setOptionalMixedArray(array $optionalMixedArray): self
public function setOptionalArrayWithMinMaxItems(array $optionalArrayWithMinMaxItems): self
{
if (count($optionalArrayWithMinMaxItems) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'optionalArrayWithMinMaxItems'));
}
if (count($optionalArrayWithMinMaxItems) > 5) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected max items: `5`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected max items: `5`.', 'optionalArrayWithMinMaxItems'));
}
$this->optionalArrayWithMinMaxItems = $optionalArrayWithMinMaxItems;
$this->optionalPropertyChanged['optionalArrayWithMinMaxItems'] = true;
Expand Down
6 changes: 3 additions & 3 deletions test/suite/functional/Generator/Schema/ItemPhp80.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Item implements SerializableInterface, JsonSerializable
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)
{
if (count($mandatoryArrayWithMinItems) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
}
if ($mandatoryNullableStringWithMinMaxLength !== null && grapheme_strlen($mandatoryNullableStringWithMinMaxLength) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
Expand Down Expand Up @@ -200,10 +200,10 @@ public function setOptionalMixedArray(array $optionalMixedArray): self
public function setOptionalArrayWithMinMaxItems(array $optionalArrayWithMinMaxItems): self
{
if (count($optionalArrayWithMinMaxItems) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'optionalArrayWithMinMaxItems'));
}
if (count($optionalArrayWithMinMaxItems) > 5) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected max items: `5`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected max items: `5`.', 'optionalArrayWithMinMaxItems'));
}
$this->optionalArrayWithMinMaxItems = $optionalArrayWithMinMaxItems;
$this->optionalPropertyChanged['optionalArrayWithMinMaxItems'] = true;
Expand Down
6 changes: 3 additions & 3 deletions test/suite/functional/Generator/Schema/ItemPhp81.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Item implements SerializableInterface, JsonSerializable
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)
{
if (count($mandatoryArrayWithMinItems) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
}
if ($mandatoryNullableStringWithMinMaxLength !== null && grapheme_strlen($mandatoryNullableStringWithMinMaxLength) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Length should be greater than 1.', 'mandatoryNullableStringWithMinMaxLength', $mandatoryNullableStringWithMinMaxLength));
Expand Down Expand Up @@ -192,10 +192,10 @@ public function setOptionalMixedArray(array $optionalMixedArray): self
public function setOptionalArrayWithMinMaxItems(array $optionalArrayWithMinMaxItems): self
{
if (count($optionalArrayWithMinMaxItems) < 1) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'optionalArrayWithMinMaxItems'));
}
if (count($optionalArrayWithMinMaxItems) > 5) {
throw new RequestValidationException(sprintf('Invalid %s value. Given: `%s`. Expected max items: `5`.', 'optionalArrayWithMinMaxItems', $optionalArrayWithMinMaxItems));
throw new RequestValidationException(sprintf('Invalid %s value. Expected max items: `5`.', 'optionalArrayWithMinMaxItems'));
}
$this->optionalArrayWithMinMaxItems = $optionalArrayWithMinMaxItems;
$this->optionalPropertyChanged['optionalArrayWithMinMaxItems'] = true;
Expand Down
Loading