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

feat: Convert exclusiveMinimum/Maximum for openapi 3.1 #1503

Merged
merged 4 commits into from
Nov 30, 2023
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
8 changes: 4 additions & 4 deletions docs/reference/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -977,13 +977,13 @@ Default value is csv.</p></dd>
<dd><p>Sets a default value to the parameter. The type of the value depends on the defined type.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor101">JSON schema validation</a></p></dd>
<dt><strong>maximum</strong> : <span style="font-family: monospace;">int|float</span></dt>
<dd><p>The maximum value allowed for a numeric property. This value must be a number.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor17">JSON schema validation</a></p></dd>
<dt><strong>exclusiveMaximum</strong> : <span style="font-family: monospace;">bool</span></dt>
<dt><strong>exclusiveMaximum</strong> : <span style="font-family: monospace;">bool|int|float</span></dt>
<dd><p>A boolean indicating whether the maximum value is excluded from the set of valid values.<br />
<br />
When set to true, the maximum value is excluded, and when false or not specified, it is included.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor17">JSON schema validation</a></p></dd>
<dt><strong>minimum</strong> : <span style="font-family: monospace;">int|float</span></dt>
<dd><p>The minimum value allowed for a numeric property. This value must be a number.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor21">JSON schema validation</a></p></dd>
<dt><strong>exclusiveMinimum</strong> : <span style="font-family: monospace;">bool</span></dt>
<dt><strong>exclusiveMinimum</strong> : <span style="font-family: monospace;">bool|int|float</span></dt>
<dd><p>A boolean indicating whether the minimum value is excluded from the set of valid values.<br />
<br />
When set to true, the minimum value is excluded, and when false or not specified, it is included.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor21">JSON schema validation</a></p></dd>
Expand All @@ -1009,7 +1009,7 @@ An array instance is valid against this property if its number of items is great
<dd><p>A boolean value indicating whether all items in an array property must be unique.<br />
<br />
If this attribute is set to true, then all items in the array must be unique.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor49">JSON schema validation</a></p></dd>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|\UnitEnum[]|class-string</span></dt>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string</span></dt>
<dd><p>A collection of allowable values for a property.<br />
<br />
A property instance is valid against this attribute if its value is one of the values specified in this collection.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor76">JSON schema validation</a></p></dd>
Expand Down Expand Up @@ -1163,7 +1163,7 @@ An object representing a server variable for server URL template substitution.
<dl>
<dt><strong>serverVariable</strong> : <span style="font-family: monospace;">string</span></dt>
<dd><p>The key into Server->variables array.</p></dd>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|\UnitEnum[]|class-string</span></dt>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string</span></dt>
<dd><p>An enumeration of values to be used if the substitution options are from a limited set.</p></dd>
<dt><strong>default</strong> : <span style="font-family: monospace;">string</span></dt>
<dd><p>The default value to use for substitution, and to send, if an alternate value is not supplied.<br />
Expand Down
38 changes: 19 additions & 19 deletions docs/reference/attributes.md

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,24 @@ public function jsonSerialize()
}
unset($data->nullable);
}

if (isset($data->minimum) && isset($data->exclusiveMinimum)) {
if (true === $data->exclusiveMinimum) {
$data->exclusiveMinimum = $data->minimum;
unset($data->minimum);
} elseif (false === $data->exclusiveMinimum) {
unset($data->exclusiveMinimum);
}
}

if (isset($data->maximum) && isset($data->exclusiveMaximum)) {
if (true === $data->exclusiveMaximum) {
$data->exclusiveMaximum = $data->maximum;
unset($data->maximum);
} elseif (false === $data->exclusiveMaximum) {
unset($data->exclusiveMaximum);
}
}
}

return $data;
Expand Down Expand Up @@ -686,6 +704,17 @@ private function validateType(string $type, $value): bool
*/
private function validateDefaultTypes(string $type, $value): bool
{
if (str_contains($type, '|')) {
$types = explode('|', $type);
foreach ($types as $type) {
if ($this->validateDefaultTypes($type, $value)) {
return true;
}
}

return false;
}

switch ($type) {
case 'string':
return is_string($value);
Expand Down
8 changes: 4 additions & 4 deletions src/Annotations/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Schema extends AbstractAnnotation
*
* @see [JSON schema validation](http://json-schema.org/latest/json-schema-validation.html#anchor17)
*
* @var bool
* @var bool|int|float
*/
public $exclusiveMaximum = Generator::UNDEFINED;

Expand All @@ -172,7 +172,7 @@ class Schema extends AbstractAnnotation
*
* @see [JSON schema validation](http://json-schema.org/latest/json-schema-validation.html#anchor21)
*
* @var bool
* @var bool|int|float
*/
public $exclusiveMinimum = Generator::UNDEFINED;

Expand Down Expand Up @@ -415,9 +415,9 @@ class Schema extends AbstractAnnotation
'format' => 'string',
'collectionFormat' => ['csv', 'ssv', 'tsv', 'pipes', 'multi'],
'maximum' => 'number',
'exclusiveMaximum' => 'boolean',
'exclusiveMaximum' => 'boolean|integer|number',
'minimum' => 'number',
'exclusiveMinimum' => 'boolean',
'exclusiveMinimum' => 'boolean|integer|number',
'maxLength' => 'integer',
'minLength' => 'integer',
'pattern' => 'string',
Expand Down
4 changes: 2 additions & 2 deletions src/Attributes/AdditionalProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function __construct(
?string $collectionFormat = null,
mixed $default = Generator::UNDEFINED,
$maximum = null,
?bool $exclusiveMaximum = null,
bool|int|float|null $exclusiveMaximum = null,
$minimum = null,
?bool $exclusiveMinimum = null,
bool|int|float|null $exclusiveMinimum = null,
?int $maxLength = null,
?int $minLength = null,
?int $maxItems = null,
Expand Down
4 changes: 2 additions & 2 deletions src/Attributes/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function __construct(
?string $collectionFormat = null,
mixed $default = Generator::UNDEFINED,
$maximum = null,
?bool $exclusiveMaximum = null,
bool|int|float|null $exclusiveMaximum = null,
$minimum = null,
?bool $exclusiveMinimum = null,
bool|int|float|null $exclusiveMinimum = null,
?int $maxLength = null,
?int $minLength = null,
?int $maxItems = null,
Expand Down
4 changes: 2 additions & 2 deletions src/Attributes/JsonContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function __construct(
?string $collectionFormat = null,
mixed $default = Generator::UNDEFINED,
$maximum = null,
?bool $exclusiveMaximum = null,
bool|int|float|null $exclusiveMaximum = null,
$minimum = null,
?bool $exclusiveMinimum = null,
bool|int|float|null $exclusiveMinimum = null,
?int $maxLength = null,
?int $minLength = null,
?int $maxItems = null,
Expand Down
4 changes: 2 additions & 2 deletions src/Attributes/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public function __construct(
?string $collectionFormat = null,
mixed $default = Generator::UNDEFINED,
$maximum = null,
?bool $exclusiveMaximum = null,
bool|int|float|null $exclusiveMaximum = null,
$minimum = null,
?bool $exclusiveMinimum = null,
bool|int|float|null $exclusiveMinimum = null,
?int $maxLength = null,
?int $minLength = null,
?int $maxItems = null,
Expand Down
4 changes: 2 additions & 2 deletions src/Attributes/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public function __construct(
?string $collectionFormat = null,
mixed $default = Generator::UNDEFINED,
$maximum = null,
?bool $exclusiveMaximum = null,
bool|int|float|null $exclusiveMaximum = null,
$minimum = null,
?bool $exclusiveMinimum = null,
bool|int|float|null $exclusiveMinimum = null,
?int $maxLength = null,
?int $minLength = null,
?int $maxItems = null,
Expand Down
4 changes: 2 additions & 2 deletions src/Attributes/XmlContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public function __construct(
?string $collectionFormat = null,
mixed $default = Generator::UNDEFINED,
$maximum = null,
?bool $exclusiveMaximum = null,
bool|int|float|null $exclusiveMaximum = null,
$minimum = null,
?bool $exclusiveMinimum = null,
bool|int|float|null $exclusiveMinimum = null,
?int $maxLength = null,
?int $minLength = null,
?int $maxItems = null,
Expand Down
43 changes: 43 additions & 0 deletions tests/Fixtures/Scratch/ExclusiveMinMax31.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

use OpenApi\Attributes as OAT;

#[OAT\Schema(schema: 'minMaxClass31')]
class MinMaxClass31
{
#[OAT\Property(minimum: 10)]
private int $min = 10;
#[OAT\Property(minimum: 20, exclusiveMinimum: true)]
private int $exclusiveMin = 21;
#[OAT\Property(maximum: 30)]
private int $max = 30;
#[OAT\Property(maximum: 40, exclusiveMaximum: true)]
private int $exclusiveMax = 41;

#[OAT\Property(minimum: 50, exclusiveMinimum: true, maximum: 60, exclusiveMaximum: true)]
private int $exclusiveMinMax = 51;

#[OAT\Property(exclusiveMinimum: 60, exclusiveMaximum: 70)]
private int $exclusiveMinMaxNumber = 61;
}

#[OAT\OpenApi(openapi: '3.1.0')]
#[OAT\Info(
title: 'Exclusive minimum and maximum',
version: '1.0'
)]
class ExclusiveMinMax31
{
#[OAT\Get(
path: '/api/endpoint',
description: 'An endpoint',
responses: [new OAT\Response(response: 200, description: 'OK')]
)]
public function exclusiveMinMax()
{
}
}
37 changes: 37 additions & 0 deletions tests/Fixtures/Scratch/ExclusiveMinMax31.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
openapi: 3.1.0
info:
title: 'Exclusive minimum and maximum'
version: '1.0'
paths:
/api/endpoint:
get:
description: 'An endpoint'
operationId: da2bbb06428a6f5ae9199a22a80436d7
responses:
'200':
description: OK
components:
schemas:
minMaxClass31:
properties:
min:
type: integer
minimum: 10
exclusiveMin:
type: integer
exclusiveMinimum: 20
max:
type: integer
maximum: 30
exclusiveMax:
type: integer
exclusiveMaximum: 40
exclusiveMinMax:
type: integer
exclusiveMinimum: 50
exclusiveMaximum: 60
exclusiveMinMaxNumber:
type: integer
exclusiveMinimum: 60
exclusiveMaximum: 70
type: object
Loading