Skip to content

Commit

Permalink
integer ranges support (zircote#1554)
Browse files Browse the repository at this point in the history
  • Loading branch information
bnowak authored Mar 7, 2024
1 parent 9f28de7 commit 038da8a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Processors/AugmentProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
}

Expand Down
48 changes: 48 additions & 0 deletions tests/Fixtures/Scratch/Docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<min,10> 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]
Expand Down
35 changes: 35 additions & 0 deletions tests/Fixtures/Scratch/Docblocks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 038da8a

Please sign in to comment.