Skip to content

Commit

Permalink
Fix typehint to make null a valid enum value (#1548)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Feb 9, 2024
1 parent bdee7f5 commit e1fb191
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/Annotations/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class Schema extends AbstractAnnotation
*
* @see [JSON schema validation](http://json-schema.org/latest/json-schema-validation.html#anchor76)
*
* @var string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string
* @var array<string|int|float|bool|\UnitEnum>|class-string
*/
public $enum = Generator::UNDEFINED;

Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/ServerVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ServerVariable extends AbstractAnnotation
/**
* An enumeration of values to be used if the substitution options are from a limited set.
*
* @var string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string
* @var array<string|int|float|bool|\UnitEnum>|class-string
*/
public $enum = Generator::UNDEFINED;

Expand Down
24 changes: 12 additions & 12 deletions src/Attributes/AdditionalProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
class AdditionalProperties extends \OpenApi\Annotations\AdditionalProperties
{
/**
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param string[] $required
* @param Property[] $properties
* @param int|float $maximum
* @param int|float $minimum
* @param string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param string[] $required
* @param Property[] $properties
* @param int|float $maximum
* @param int|float $minimum
* @param array<string|int|float|bool|\UnitEnum|null>|class-string|null $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
*/
public function __construct(
// schema
Expand Down
24 changes: 12 additions & 12 deletions src/Attributes/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
class Items extends \OpenApi\Annotations\Items
{
/**
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param string[] $required
* @param Property[] $properties
* @param int|float $maximum
* @param int|float $minimum
* @param string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param string[] $required
* @param Property[] $properties
* @param int|float $maximum
* @param int|float $minimum
* @param array<string|int|float|bool|\UnitEnum|null>|class-string|null $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
*/
public function __construct(
// schema
Expand Down
26 changes: 13 additions & 13 deletions src/Attributes/JsonContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
class JsonContent extends \OpenApi\Annotations\JsonContent
{
/**
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param array<string,Examples> $examples
* @param string[] $required
* @param Property[] $properties
* @param int|float $maximum
* @param int|float $minimum
* @param string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param array<string,Examples> $examples
* @param string[] $required
* @param Property[] $properties
* @param int|float $maximum
* @param int|float $minimum
* @param array<string|int|float|bool|\UnitEnum|null>|class-string|null $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
*/
public function __construct(
?array $examples = null,
Expand Down
24 changes: 12 additions & 12 deletions src/Attributes/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
class Property extends \OpenApi\Annotations\Property
{
/**
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param string[] $required
* @param Property[] $properties
* @param int|float $maximum
* @param int|float $minimum
* @param string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param string[] $required
* @param Property[] $properties
* @param int|float $maximum
* @param int|float $minimum
* @param array<string|int|float|bool|\UnitEnum|null>|class-string|null $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
*/
public function __construct(
?string $property = null,
Expand Down
26 changes: 13 additions & 13 deletions src/Attributes/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
class Schema extends \OpenApi\Annotations\Schema
{
/**
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param string[] $required
* @param Property[] $properties
* @param int|float $maximum
* @param int|float $minimum
* @param string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param mixed $const
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param string[] $required
* @param Property[] $properties
* @param int|float $maximum
* @param int|float $minimum
* @param array<string|int|float|bool|\UnitEnum|null>|class-string|null $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param mixed $const
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
*/
public function __construct(
// schema
Expand Down
6 changes: 3 additions & 3 deletions src/Attributes/ServerVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
class ServerVariable extends \OpenApi\Annotations\ServerVariable
{
/**
* @param string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string|null $enum
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
* @param array<string|int|float|bool|\UnitEnum|null>|class-string|null $enum
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
*/
public function __construct(
?string $serverVariable = null,
Expand Down
26 changes: 13 additions & 13 deletions src/Attributes/XmlContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
class XmlContent extends \OpenApi\Annotations\XmlContent
{
/**
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param array<string,Examples> $examples
* @param string[] $required
* @param int|float $maximum
* @param int|float $minimum
* @param Property[] $properties
* @param string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
* @param string|non-empty-array<string>|null $type
* @param string|class-string|object|null $ref
* @param array<string,Examples> $examples
* @param string[] $required
* @param int|float $maximum
* @param int|float $minimum
* @param Property[] $properties
* @param array<string|int|float|bool|\UnitEnum|null>|class-string|null $enum
* @param array<Schema|\OpenApi\Annotations\Schema> $allOf
* @param array<Schema|\OpenApi\Annotations\Schema> $anyOf
* @param array<Schema|\OpenApi\Annotations\Schema> $oneOf
* @param array<string,mixed>|null $x
* @param Attachable[]|null $attachables
*/
public function __construct(
?array $examples = null,
Expand Down
6 changes: 2 additions & 4 deletions tests/Annotations/AttributesSyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ protected function propertyType(\ReflectionProperty $property): ?string
{
$var = null;
foreach ($this->prepDocComment($property->getDocComment()) as $line) {
if (substr($line, 0, 1) === '@') {
if (substr($line, 0, 5) === '@var ') {
$var = trim(substr($line, 5));
}
if (substr($line, 0, 5) === '@var ') {
$var = trim(substr($line, 5));
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Fixtures/Scratch/Nullable.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class Nullable

#[OAT\Property(type: ['string', 'null'])]
public ?string $description;

#[OAT\Property(enum: ['Choice1', 'Choice2', null], example: 'Choice1', nullable: true)]
public ?string $choice = null;
}

#[OAT\Get(
Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/Scratch/Nullable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ components:
type:
- string
- 'null'
choice:
type:
- string
- 'null'
enum:
- Choice1
- Choice2
- null
example: Choice1
type: object

0 comments on commit e1fb191

Please sign in to comment.