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 wrong detection of OA min/max for symfony contraints with datetime strings #2148

Merged
merged 3 commits into from
Jan 2, 2024
Merged
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
28 changes: 18 additions & 10 deletions ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,30 @@ private function processPropertyAnnotations($reflection, OA\Property $property,
} elseif ($annotation instanceof Assert\Choice) {
$this->applyEnumFromChoiceConstraint($property, $annotation, $reflection);
} elseif ($annotation instanceof Assert\Range) {
if (isset($annotation->min)) {
$property->minimum = (int) $annotation->min;
if (\is_int($annotation->min)) {
$property->minimum = $annotation->min;
}
if (isset($annotation->max)) {
$property->maximum = (int) $annotation->max;
if (\is_int($annotation->max)) {
$property->maximum = $annotation->max;
}
} elseif ($annotation instanceof Assert\LessThan) {
$property->exclusiveMaximum = true;
$property->maximum = (int) $annotation->value;
if (\is_int($annotation->value)) {
$property->exclusiveMaximum = true;
$property->maximum = $annotation->value;
}
} elseif ($annotation instanceof Assert\LessThanOrEqual) {
$property->maximum = (int) $annotation->value;
if (\is_int($annotation->value)) {
$property->maximum = $annotation->value;
}
} elseif ($annotation instanceof Assert\GreaterThan) {
$property->exclusiveMinimum = true;
$property->minimum = (int) $annotation->value;
if (\is_int($annotation->value)) {
$property->exclusiveMinimum = true;
$property->minimum = $annotation->value;
}
} elseif ($annotation instanceof Assert\GreaterThanOrEqual) {
$property->minimum = (int) $annotation->value;
if (\is_int($annotation->value)) {
$property->minimum = $annotation->value;
}
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions Tests/Functional/Entity/SymfonyConstraints80.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,69 @@ class SymfonyConstraints80
*/
private $propertyRange;

/**
* @var \DateTimeImmutable
*
* @Assert\Range(min="now", max="+5 hours")
*/
public $propertyRangeDate;

/**
* @var int
*
* @Assert\LessThan(42)
*/
private $propertyLessThan;

/**
* @var \DateTimeImmutable
*
* @Assert\LessThan("now")
*/
public $propertyLessThanDate;

/**
* @var int
*
* @Assert\LessThanOrEqual(23)
*/
private $propertyLessThanOrEqual;

/**
* @var \DateTimeImmutable
*
* @Assert\LessThanOrEqual("now")
*/
public $propertyLessThanOrEqualDate;

/**
* @var int
*
* @Assert\GreaterThan(42)
*/
public $propertyGreaterThan;

/**
* @var \DateTimeImmutable
*
* @Assert\GreaterThan("now")
*/
public $propertyGreaterThanDate;

/**
* @var int
*
* @Assert\GreaterThanOrEqual(23)
*/
public $propertyGreaterThanOrEqual;

/**
* @var \DateTimeImmutable
*
* @Assert\GreaterThanOrEqual("now")
*/
public $propertyGreaterThanOrEqualDate;

/**
* @var int
*
Expand Down
42 changes: 42 additions & 0 deletions Tests/Functional/Entity/SymfonyConstraints81.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,60 @@ class SymfonyConstraints81
#[Assert\Range(min: 1, max: 5)]
private $propertyRange;

/**
* @var \DateTimeImmutable
*/
#[Assert\Range(min: 'now', max: '+5 hours')]
public $propertyRangeDate;

/**
* @var int
*/
#[Assert\LessThan(42)]
private $propertyLessThan;

/**
* @var \DateTimeImmutable
*/
#[Assert\LessThan('now')]
public $propertyLessThanDate;

/**
* @var int
*/
#[Assert\LessThanOrEqual(23)]
private $propertyLessThanOrEqual;

/**
* @var \DateTimeImmutable
*/
#[Assert\LessThanOrEqual('now')]
public $propertyLessThanOrEqualDate;

/**
* @var int
*/
#[Assert\GreaterThan(42)]
public $propertyGreaterThan;

/**
* @var \DateTimeImmutable
*/
#[Assert\GreaterThan('now')]
public $propertyGreaterThanDate;

/**
* @var int
*/
#[Assert\GreaterThanOrEqual(23)]
public $propertyGreaterThanOrEqual;

/**
* @var \DateTimeImmutable
*/
#[Assert\GreaterThanOrEqual('now')]
public $propertyGreaterThanOrEqualDate;

/**
* @var int
*/
Expand Down
36 changes: 36 additions & 0 deletions Tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,15 @@ public function testSymfonyConstraintDocumentation()
'propertyChoiceWithMultiple',
'propertyExpression',
'propertyRange',
'propertyRangeDate',
'propertyLessThan',
'propertyLessThanDate',
'propertyLessThanOrEqual',
'propertyLessThanOrEqualDate',
'propertyGreaterThan',
'propertyGreaterThanDate',
'propertyGreaterThanOrEqual',
'propertyGreaterThanOrEqualDate',
],
'properties' => [
'propertyNotBlank' => [
Expand Down Expand Up @@ -510,18 +517,47 @@ public function testSymfonyConstraintDocumentation()
'maximum' => 5,
'minimum' => 1,
],
'propertyRangeDate' => [
'type' => 'string',
'format' => 'date-time',
],
'propertyLessThan' => [
'type' => 'integer',
'exclusiveMaximum' => true,
'maximum' => 42,
],
'propertyLessThanDate' => [
'type' => 'string',
'format' => 'date-time',
],
'propertyLessThanOrEqual' => [
'type' => 'integer',
'maximum' => 23,
],
'propertyLessThanOrEqualDate' => [
'type' => 'string',
'format' => 'date-time',
],
'propertyWithCompoundValidationRule' => [
'type' => 'integer',
],
'propertyGreaterThan' => [
'type' => 'integer',
'exclusiveMinimum' => true,
'minimum' => 42,
],
'propertyGreaterThanDate' => [
'type' => 'string',
'format' => 'date-time',
],
'propertyGreaterThanOrEqual' => [
'type' => 'integer',
'minimum' => 23,
],
'propertyGreaterThanOrEqualDate' => [
'type' => 'string',
'format' => 'date-time',
],
],
'type' => 'object',
'schema' => $modelName,
Expand Down