From 038da8ad219f1e9c3f82c5d84c47da3b6f35039c Mon Sep 17 00:00:00 2001 From: bnowak Date: Fri, 8 Mar 2024 00:56:26 +0100 Subject: [PATCH] integer ranges support (#1554) --- src/Processors/AugmentProperties.php | 26 ++++++++++++++- tests/Fixtures/Scratch/Docblocks.php | 48 +++++++++++++++++++++++++++ tests/Fixtures/Scratch/Docblocks.yaml | 35 +++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/src/Processors/AugmentProperties.php b/src/Processors/AugmentProperties.php index 3fd8d7f3..793b465b 100644 --- a/src/Processors/AugmentProperties.php +++ b/src/Processors/AugmentProperties.php @@ -84,7 +84,7 @@ protected function augmentType(Analysis $analysis, OA\Property $property, Contex } $allTypes = $this->stripNull($allTypes); - preg_match('/^([^\[]+)(.*$)/', $allTypes, $typeMatches); + preg_match('/^([^\[\<]+)(.*$)/', $allTypes, $typeMatches); $type = $typeMatches[1]; // finalise property type/ref @@ -121,6 +121,30 @@ protected function augmentType(Analysis $analysis, OA\Property $property, Contex } $property->type = 'array'; } + } elseif ($property->type === 'integer' && str_starts_with($typeMatches[2], '<') && str_ends_with($typeMatches[2], '>')) { + [$min, $max] = explode(',', substr($typeMatches[2], 1, -1)); + + if (is_numeric($min)) { + $property->minimum = (int) $min; + } + if (is_numeric($max)) { + $property->maximum = (int) $max; + } + } elseif ($type === 'positive-int') { + $property->type = 'integer'; + $property->minimum = 1; + } elseif ($type === 'negative-int') { + $property->type = 'integer'; + $property->maximum = -1; + } elseif ($type === 'non-positive-int') { + $property->type = 'integer'; + $property->maximum = 0; + } elseif ($type === 'non-negative-int') { + $property->type = 'integer'; + $property->minimum = 0; + } elseif ($type === 'non-zero-int') { + $property->type = 'integer'; + $property->not = ['const' => 0]; } } diff --git a/tests/Fixtures/Scratch/Docblocks.php b/tests/Fixtures/Scratch/Docblocks.php index 6d2c6312..e8571b96 100644 --- a/tests/Fixtures/Scratch/Docblocks.php +++ b/tests/Fixtures/Scratch/Docblocks.php @@ -25,6 +25,54 @@ class DocblockSchema * @deprecated */ public $oldName; + + /** + * @OA\Property + * @var int<5,25> The range integer + */ + public $rangeInt; + + /** + * @OA\Property + * @var int<2,max> The minimum range integer + */ + public $minRangeInt; + + /** + * @OA\Property + * @var int The maximum range integer + */ + public $maxRangeInt; + + /** + * @OA\Property + * @var positive-int The positive integer + */ + public $positiveInt; + + /** + * @OA\Property + * @var negative-int The negative integer + */ + public $negativeInt; + + /** + * @OA\Property + * @var non-positive-int The non-positive integer + */ + public $nonPositiveInt; + + /** + * @OA\Property + * @var non-negative-int The non-negative integer + */ + public $nonNegativeInt; + + /** + * @OA\Property + * @var non-zero-int The non-zero integer + */ + public $nonZeroInt; } #[OAT\Schema] diff --git a/tests/Fixtures/Scratch/Docblocks.yaml b/tests/Fixtures/Scratch/Docblocks.yaml index a66ca5bc..cb8c7391 100644 --- a/tests/Fixtures/Scratch/Docblocks.yaml +++ b/tests/Fixtures/Scratch/Docblocks.yaml @@ -20,6 +20,41 @@ components: description: 'The name (old)' type: string deprecated: true + rangeInt: + description: 'The range integer' + type: integer + minimum: 5 + maximum: 25 + minRangeInt: + description: 'The minimum range integer' + type: integer + minimum: 2 + maxRangeInt: + description: 'The maximum range integer' + type: integer + maximum: 10 + positiveInt: + description: 'The positive integer' + type: integer + minimum: 1 + negativeInt: + description: 'The negative integer' + type: integer + maximum: -1 + nonPositiveInt: + description: 'The non-positive integer' + type: integer + maximum: 0 + nonNegativeInt: + description: 'The non-negative integer' + type: integer + minimum: 0 + nonZeroInt: + description: 'The non-zero integer' + type: integer + not: + const: 0 + type: object DocblockSchemaChild: type: object