From ebb0c87c93bd033cd140bc2718915cb315ad028a Mon Sep 17 00:00:00 2001 From: DjordyKoert Date: Fri, 17 Nov 2023 19:42:59 +0100 Subject: [PATCH 1/7] Change nullable & type for OpenApi v3.1 --- docs/reference/annotations.md | 17 +++-- docs/reference/attributes.md | 82 +++++++++++++++++-------- src/Annotations/Schema.php | 23 ++++++- src/Annotations/SecurityScheme.php | 2 +- src/Attributes/AdditionalProperties.php | 3 +- src/Attributes/Items.php | 3 +- src/Attributes/JsonContent.php | 3 +- src/Attributes/Property.php | 3 +- src/Attributes/Schema.php | 3 +- src/Attributes/SecurityScheme.php | 11 ++-- src/Attributes/XmlContent.php | 3 +- 11 files changed, 107 insertions(+), 46 deletions(-) diff --git a/docs/reference/annotations.md b/docs/reference/annotations.md index 2208ec7a..7f5620c5 100644 --- a/docs/reference/annotations.md +++ b/docs/reference/annotations.md @@ -956,10 +956,12 @@ An object instance is valid against this property if its number of properties is
required : string[]

An object instance is valid against this property if its property set contains all elements in this property's
array value.

-
type : string
+
type : string|non-empty-array<string>

The type of the schema/property.

-The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".

+OpenApi v3.0: The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".
+
+Since OpenApi v3.1 an array of types may be used.

format : string

The extending format for the previously mentioned type. See Data Type Formats for further details.

collectionFormat : string
@@ -1009,7 +1011,7 @@ An array instance is valid against this property if its number of items is great

A boolean value indicating whether all items in an array property must be unique.

If this attribute is set to true, then all items in the array must be unique.

See: JSON schema validation

-
enum : string[]|int[]|float[]|\UnitEnum[]|class-string
+
enum : string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string

A collection of allowable values for a property.

A property instance is valid against this attribute if its value is one of the values specified in this collection.

See: JSON schema validation

@@ -1040,7 +1042,10 @@ To represent examples that cannot naturally be represented in JSON or YAML, a st contain the example with escaping where necessary.

nullable : bool

Allows sending a null value for the defined schema.
-Default value is false.

+Default value is false.
+
+This must not be used when using OpenApi version 3.1,
+instead make the "type" property an array and add "null" as a possible type.

deprecated : bool

Specifies that a schema is deprecated and should be transitioned out of usage.
Default value is false.

@@ -1093,7 +1098,7 @@ defined by this property's value.

The relative or absolute path to a security scheme.

See: Using refs

securityScheme : string

The key into OpenApi->security array.

-
type : string
+
type : string|non-empty-array<string>

The type of the security scheme.

description : string

A short description for security scheme.

@@ -1163,7 +1168,7 @@ An object representing a server variable for server URL template substitution.
serverVariable : string

The key into Server->variables array.

-
enum : string[]|int[]|float[]|\UnitEnum[]|class-string
+
enum : string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string

An enumeration of values to be used if the substitution options are from a limited set.

default : string

The default value to use for substitution, and to send, if an alternate value is not supplied.
diff --git a/docs/reference/attributes.md b/docs/reference/attributes.md index 292679b0..61c219f9 100644 --- a/docs/reference/attributes.md +++ b/docs/reference/attributes.md @@ -44,10 +44,12 @@ array value.

A collection of properties to define for an object.

Each property is represented as an instance of the Property class.

-
type : string|null
+
type : string|non-empty-array<string>|null

The type of the schema/property.

-The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".

+OpenApi v3.0: The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".
+
+Since OpenApi v3.1 an array of types may be used.

format : string|null

The extending format for the previously mentioned type. See Data Type Formats for further details.

items : OpenApi\Attributes\Items|null
@@ -99,7 +101,7 @@ An array instance is valid against this property if its number of items is great If this attribute is set to true, then all items in the array must be unique.

See: JSON schema validation

pattern : string|null

A string instance is considered valid if the regular expression matches the instance successfully.

-
enum : string[]|int[]|float[]|\UnitEnum[]|class-string
+
enum : string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string

A collection of allowable values for a property.

A property instance is valid against this attribute if its value is one of the values specified in this collection.

See: JSON schema validation

@@ -139,7 +141,10 @@ To represent examples that cannot naturally be represented in JSON or YAML, a st contain the example with escaping where necessary.

nullable : bool|null

Allows sending a null value for the defined schema.
-Default value is false.

+Default value is false.
+
+This must not be used when using OpenApi version 3.1,
+instead make the "type" property an array and add "null" as a possible type.

deprecated : bool|null

Specifies that a schema is deprecated and should be transitioned out of usage.
Default value is false.

@@ -1002,10 +1007,12 @@ array value.

A collection of properties to define for an object.

Each property is represented as an instance of the Property class.

-
type : string|null
+
type : string|non-empty-array<string>|null

The type of the schema/property.

-The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".

+OpenApi v3.0: The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".
+
+Since OpenApi v3.1 an array of types may be used.

format : string|null

The extending format for the previously mentioned type. See Data Type Formats for further details.

items : OpenApi\Attributes\Items|null
@@ -1057,7 +1064,7 @@ An array instance is valid against this property if its number of items is great If this attribute is set to true, then all items in the array must be unique.

See: JSON schema validation

pattern : string|null

A string instance is considered valid if the regular expression matches the instance successfully.

-
enum : string[]|int[]|float[]|\UnitEnum[]|class-string
+
enum : string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string

A collection of allowable values for a property.

A property instance is valid against this attribute if its value is one of the values specified in this collection.

See: JSON schema validation

@@ -1097,7 +1104,10 @@ To represent examples that cannot naturally be represented in JSON or YAML, a st contain the example with escaping where necessary.

nullable : bool|null

Allows sending a null value for the defined schema.
-Default value is false.

+Default value is false.
+
+This must not be used when using OpenApi version 3.1,
+instead make the "type" property an array and add "null" as a possible type.

deprecated : bool|null

Specifies that a schema is deprecated and should be transitioned out of usage.
Default value is false.

@@ -1160,10 +1170,12 @@ array value.

A collection of properties to define for an object.

Each property is represented as an instance of the Property class.

-
type : string|null
+
type : string|non-empty-array<string>|null

The type of the schema/property.

-The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".

+OpenApi v3.0: The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".
+
+Since OpenApi v3.1 an array of types may be used.

format : string|null

The extending format for the previously mentioned type. See Data Type Formats for further details.

items : OpenApi\Attributes\Items|null
@@ -1215,7 +1227,7 @@ An array instance is valid against this property if its number of items is great If this attribute is set to true, then all items in the array must be unique.

See: JSON schema validation

pattern : string|null

A string instance is considered valid if the regular expression matches the instance successfully.

-
enum : string[]|int[]|float[]|\UnitEnum[]|class-string
+
enum : string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string

A collection of allowable values for a property.

A property instance is valid against this attribute if its value is one of the values specified in this collection.

See: JSON schema validation

@@ -1255,7 +1267,10 @@ To represent examples that cannot naturally be represented in JSON or YAML, a st contain the example with escaping where necessary.

nullable : bool|null

Allows sending a null value for the defined schema.
-Default value is false.

+Default value is false.
+
+This must not be used when using OpenApi version 3.1,
+instead make the "type" property an array and add "null" as a possible type.

deprecated : bool|null

Specifies that a schema is deprecated and should be transitioned out of usage.
Default value is false.

@@ -2024,10 +2039,12 @@ array value.

A collection of properties to define for an object.

Each property is represented as an instance of the Property class.

-
type : string|null
+
type : string|non-empty-array<string>|null

The type of the schema/property.

-The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".

+OpenApi v3.0: The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".
+
+Since OpenApi v3.1 an array of types may be used.

format : string|null

The extending format for the previously mentioned type. See Data Type Formats for further details.

items : OpenApi\Attributes\Items|null
@@ -2079,7 +2096,7 @@ An array instance is valid against this property if its number of items is great If this attribute is set to true, then all items in the array must be unique.

See: JSON schema validation

pattern : string|null

A string instance is considered valid if the regular expression matches the instance successfully.

-
enum : string[]|int[]|float[]|\UnitEnum[]|class-string
+
enum : string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string

A collection of allowable values for a property.

A property instance is valid against this attribute if its value is one of the values specified in this collection.

See: JSON schema validation

@@ -2119,7 +2136,10 @@ To represent examples that cannot naturally be represented in JSON or YAML, a st contain the example with escaping where necessary.

nullable : bool|null

Allows sending a null value for the defined schema.
-Default value is false.

+Default value is false.
+
+This must not be used when using OpenApi version 3.1,
+instead make the "type" property an array and add "null" as a possible type.

deprecated : bool|null

Specifies that a schema is deprecated and should be transitioned out of usage.
Default value is false.

@@ -2470,10 +2490,12 @@ array value.

A collection of properties to define for an object.

Each property is represented as an instance of the Property class.

-
type : string|null
+
type : string|non-empty-array<string>|null

The type of the schema/property.

-The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".

+OpenApi v3.0: The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".
+
+Since OpenApi v3.1 an array of types may be used.

format : string|null

The extending format for the previously mentioned type. See Data Type Formats for further details.

items : OpenApi\Attributes\Items|null
@@ -2525,7 +2547,7 @@ An array instance is valid against this property if its number of items is great If this attribute is set to true, then all items in the array must be unique.

See: JSON schema validation

pattern : string|null

A string instance is considered valid if the regular expression matches the instance successfully.

-
enum : string[]|int[]|float[]|\UnitEnum[]|class-string
+
enum : string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string

A collection of allowable values for a property.

A property instance is valid against this attribute if its value is one of the values specified in this collection.

See: JSON schema validation

@@ -2565,7 +2587,10 @@ To represent examples that cannot naturally be represented in JSON or YAML, a st contain the example with escaping where necessary.

nullable : bool|null

Allows sending a null value for the defined schema.
-Default value is false.

+Default value is false.
+
+This must not be used when using OpenApi version 3.1,
+instead make the "type" property an array and add "null" as a possible type.

deprecated : bool|null

Specifies that a schema is deprecated and should be transitioned out of usage.
Default value is false.

@@ -2610,7 +2635,7 @@ These will be ignored but can be used for custom processing.

The relative or absolute path to a security scheme.

See: Using refs

securityScheme : string|null

The key into OpenApi->security array.

-
type : string|null
+
type : string|non-empty-array<string>|null

The type of the security scheme.

description : string|null

A short description for security scheme.

@@ -2700,7 +2725,7 @@ CommonMark syntax MAY be used for rich text representation.

The default value to use for substitution, and to send, if an alternate value is not supplied.

Unlike the Schema Object's default, this value must be provided by the consumer.

-
enum : string[]|int[]|float[]|\UnitEnum[]|class-string|null
+
enum : string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string|null

An enumeration of values to be used if the substitution options are from a limited set.

variables : array|null

A map between a variable name and its value.
@@ -2918,10 +2943,12 @@ array value.

A collection of properties to define for an object.

Each property is represented as an instance of the Property class.

-
type : string|null
+
type : string|non-empty-array<string>|null

The type of the schema/property.

-The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".

+OpenApi v3.0: The value MUST be one of "string", "number", "integer", "boolean", "array" or "object".
+
+Since OpenApi v3.1 an array of types may be used.

format : string|null

The extending format for the previously mentioned type. See Data Type Formats for further details.

items : OpenApi\Attributes\Items|null
@@ -2973,7 +3000,7 @@ An array instance is valid against this property if its number of items is great If this attribute is set to true, then all items in the array must be unique.

See: JSON schema validation

pattern : string|null

A string instance is considered valid if the regular expression matches the instance successfully.

-
enum : string[]|int[]|float[]|\UnitEnum[]|class-string
+
enum : string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string

A collection of allowable values for a property.

A property instance is valid against this attribute if its value is one of the values specified in this collection.

See: JSON schema validation

@@ -3013,7 +3040,10 @@ To represent examples that cannot naturally be represented in JSON or YAML, a st contain the example with escaping where necessary.

nullable : bool|null

Allows sending a null value for the defined schema.
-Default value is false.

+Default value is false.
+
+This must not be used when using OpenApi version 3.1,
+instead make the "type" property an array and add "null" as a possible type.

deprecated : bool|null

Specifies that a schema is deprecated and should be transitioned out of usage.
Default value is false.

diff --git a/src/Annotations/Schema.php b/src/Annotations/Schema.php index 77c81853..cba563bc 100644 --- a/src/Annotations/Schema.php +++ b/src/Annotations/Schema.php @@ -91,9 +91,11 @@ class Schema extends AbstractAnnotation /** * The type of the schema/property. * - * The value MUST be one of "string", "number", "integer", "boolean", "array" or "object". + * OpenApi v3.0: The value MUST be one of "string", "number", "integer", "boolean", "array" or "object". * - * @var string + * Since OpenApi v3.1 an array of types may be used. + * + * @var string|non-empty-array */ public $type = Generator::UNDEFINED; @@ -325,7 +327,12 @@ class Schema extends AbstractAnnotation * Allows sending a null value for the defined schema. * Default value is false. * + * This must not be used when using OpenApi version 3.1, + * instead make the "type" property an array and add "null" as a possible type. + * * @var bool + * + * @see https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0 */ public $nullable = Generator::UNDEFINED; @@ -483,6 +490,18 @@ public function validate(array $stack = [], array $skip = [], string $ref = '', return false; } + if ($this->nullable !== Generator::UNDEFINED && $this->_context->isVersion(OpenApi::VERSION_3_1_0)) { + $this->_context->logger->error($this->identity() . ' must not have the "nullable" property when using OpenApi version ' . OpenApi::VERSION_3_1_0 . ' in ' . $this->_context); + + return false; + } + + if (is_array($this->type) && $this->_context->isVersion(OpenApi::VERSION_3_0_0)) { + $this->_context->logger->error($this->identity() . ' "type" must be of type string in OpenApi version ' . OpenApi::VERSION_3_0_0 . ', array given in ' . $this->_context); + + return false; + } + return parent::validate($stack, $skip, $ref, $context); } } diff --git a/src/Annotations/SecurityScheme.php b/src/Annotations/SecurityScheme.php index 2d30e5bc..631fcec8 100644 --- a/src/Annotations/SecurityScheme.php +++ b/src/Annotations/SecurityScheme.php @@ -34,7 +34,7 @@ class SecurityScheme extends AbstractAnnotation /** * The type of the security scheme. * - * @var string + * @var string|non-empty-array */ public $type = Generator::UNDEFINED; diff --git a/src/Attributes/AdditionalProperties.php b/src/Attributes/AdditionalProperties.php index ca0ab3f8..b79e996b 100644 --- a/src/Attributes/AdditionalProperties.php +++ b/src/Attributes/AdditionalProperties.php @@ -12,6 +12,7 @@ class AdditionalProperties extends \OpenApi\Annotations\AdditionalProperties { /** + * @param string|non-empty-array|null $type * @param string|class-string|object|null $ref * @param string[] $required * @param Property[] $properties @@ -34,7 +35,7 @@ public function __construct( ?int $minProperties = null, ?array $required = null, ?array $properties = null, - ?string $type = null, + string|array|null $type = null, ?string $format = null, ?Items $items = null, ?string $collectionFormat = null, diff --git a/src/Attributes/Items.php b/src/Attributes/Items.php index ad8f3606..ca1190f6 100644 --- a/src/Attributes/Items.php +++ b/src/Attributes/Items.php @@ -12,6 +12,7 @@ class Items extends \OpenApi\Annotations\Items { /** + * @param string|non-empty-array|null $type * @param string|class-string|object|null $ref * @param string[] $required * @param Property[] $properties @@ -34,7 +35,7 @@ public function __construct( ?int $minProperties = null, ?array $required = null, ?array $properties = null, - ?string $type = null, + string|array|null $type = null, ?string $format = null, ?Items $items = null, ?string $collectionFormat = null, diff --git a/src/Attributes/JsonContent.php b/src/Attributes/JsonContent.php index e10c605d..e65a50da 100644 --- a/src/Attributes/JsonContent.php +++ b/src/Attributes/JsonContent.php @@ -12,6 +12,7 @@ class JsonContent extends \OpenApi\Annotations\JsonContent { /** + * @param string|non-empty-array|null $type * @param string|class-string|object|null $ref * @param array $examples * @param string[] $required @@ -36,7 +37,7 @@ public function __construct( ?int $minProperties = null, ?array $required = null, ?array $properties = null, - ?string $type = null, + string|array|null $type = null, ?string $format = null, ?Items $items = null, ?string $collectionFormat = null, diff --git a/src/Attributes/Property.php b/src/Attributes/Property.php index a3e2916c..25c5c87d 100644 --- a/src/Attributes/Property.php +++ b/src/Attributes/Property.php @@ -12,6 +12,7 @@ class Property extends \OpenApi\Annotations\Property { /** + * @param string|non-empty-array|null $type * @param string|class-string|object|null $ref * @param string[] $required * @param Property[] $properties @@ -35,7 +36,7 @@ public function __construct( ?int $minProperties = null, ?array $required = null, ?array $properties = null, - ?string $type = null, + string|array|null $type = null, ?string $format = null, ?Items $items = null, ?string $collectionFormat = null, diff --git a/src/Attributes/Schema.php b/src/Attributes/Schema.php index 3437bbc5..84b1a147 100644 --- a/src/Attributes/Schema.php +++ b/src/Attributes/Schema.php @@ -12,6 +12,7 @@ class Schema extends \OpenApi\Annotations\Schema { /** + * @param string|non-empty-array|null $type * @param string|class-string|object|null $ref * @param string[] $required * @param Property[] $properties @@ -35,7 +36,7 @@ public function __construct( ?int $minProperties = null, ?array $required = null, ?array $properties = null, - ?string $type = null, + string|array|null $type = null, ?string $format = null, ?Items $items = null, ?string $collectionFormat = null, diff --git a/src/Attributes/SecurityScheme.php b/src/Attributes/SecurityScheme.php index ea98cab9..e43c274b 100644 --- a/src/Attributes/SecurityScheme.php +++ b/src/Attributes/SecurityScheme.php @@ -12,15 +12,16 @@ class SecurityScheme extends \OpenApi\Annotations\SecurityScheme { /** - * @param string|class-string|object|null $ref - * @param Flow[] $flows - * @param array|null $x - * @param Attachable[]|null $attachables + * @param string|non-empty-array|null $type + * @param string|class-string|object|null $ref + * @param Flow[] $flows + * @param array|null $x + * @param Attachable[]|null $attachables */ public function __construct( string|object|null $ref = null, ?string $securityScheme = null, - ?string $type = null, + string|array|null $type = null, ?string $description = null, ?string $name = null, ?string $in = null, diff --git a/src/Attributes/XmlContent.php b/src/Attributes/XmlContent.php index b4035994..45f03590 100644 --- a/src/Attributes/XmlContent.php +++ b/src/Attributes/XmlContent.php @@ -12,6 +12,7 @@ class XmlContent extends \OpenApi\Annotations\XmlContent { /** + * @param string|non-empty-array|null $type * @param string|class-string|object|null $ref * @param array $examples * @param string[] $required @@ -36,7 +37,7 @@ public function __construct( ?int $minProperties = null, ?array $required = null, ?array $properties = null, - ?string $type = null, + string|array|null $type = null, ?string $format = null, ?Items $items = null, ?string $collectionFormat = null, From b5022e2a4bec0eb45f8dc6a7f2d10585c42fdf0e Mon Sep 17 00:00:00 2001 From: DjordyKoert Date: Fri, 17 Nov 2023 20:34:57 +0100 Subject: [PATCH 2/7] Add validate schema tests --- tests/Annotations/NullableTest.php | 24 +++++++++++++++++++++ tests/Annotations/TypeTest.php | 34 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/Annotations/NullableTest.php create mode 100644 tests/Annotations/TypeTest.php diff --git a/tests/Annotations/NullableTest.php b/tests/Annotations/NullableTest.php new file mode 100644 index 00000000..46740cb8 --- /dev/null +++ b/tests/Annotations/NullableTest.php @@ -0,0 +1,24 @@ +annotationsFromDocBlockParser('@OA\Schema(type="string", nullable=true)'); + $annotations[0]->validate(); + + $annotations = $this->annotationsFromDocBlockParser('@OA\Schema(type="string", nullable=true)'); + $annotations[0]->_context = $this->getContext([], OA\OpenApi::VERSION_3_1_0); + $this->assertOpenApiLogEntryContains('@OA\Schema() must not have the "nullable" property when using OpenApi version 3.1.0 in '); + $annotations[0]->validate(); + } +} diff --git a/tests/Annotations/TypeTest.php b/tests/Annotations/TypeTest.php new file mode 100644 index 00000000..c6a888e8 --- /dev/null +++ b/tests/Annotations/TypeTest.php @@ -0,0 +1,34 @@ +annotationsFromDocBlockParser('@OA\Schema(type="string")'); + $annotations[0]->validate(); + + $annotations = $this->annotationsFromDocBlockParser('@OA\Schema(type="string")'); + $annotations[0]->_context = $this->getContext([], OA\OpenApi::VERSION_3_1_0); + $annotations[0]->validate(); + } + + public function testTypeOfArray(): void + { + $annotations = $this->annotationsFromDocBlockParser('@OA\Schema(type={"string", "null"})'); + $this->assertOpenApiLogEntryContains('@OA\Schema() "type" must be of type string in OpenApi version 3.0.0, array given in '); + $annotations[0]->validate(); + + $annotations = $this->annotationsFromDocBlockParser('@OA\Schema(type={"string", "null"})'); + $annotations[0]->_context = $this->getContext([], OA\OpenApi::VERSION_3_1_0); + $annotations[0]->validate(); + } +} From 2b61fdbb9af1afb600cadf2d2529ca49345e1fa2 Mon Sep 17 00:00:00 2001 From: DjordyKoert Date: Thu, 23 Nov 2023 09:42:14 +0100 Subject: [PATCH 3/7] Revert "Add validate schema tests" This reverts commit b5022e2a4bec0eb45f8dc6a7f2d10585c42fdf0e. --- tests/Annotations/NullableTest.php | 24 --------------------- tests/Annotations/TypeTest.php | 34 ------------------------------ 2 files changed, 58 deletions(-) delete mode 100644 tests/Annotations/NullableTest.php delete mode 100644 tests/Annotations/TypeTest.php diff --git a/tests/Annotations/NullableTest.php b/tests/Annotations/NullableTest.php deleted file mode 100644 index 46740cb8..00000000 --- a/tests/Annotations/NullableTest.php +++ /dev/null @@ -1,24 +0,0 @@ -annotationsFromDocBlockParser('@OA\Schema(type="string", nullable=true)'); - $annotations[0]->validate(); - - $annotations = $this->annotationsFromDocBlockParser('@OA\Schema(type="string", nullable=true)'); - $annotations[0]->_context = $this->getContext([], OA\OpenApi::VERSION_3_1_0); - $this->assertOpenApiLogEntryContains('@OA\Schema() must not have the "nullable" property when using OpenApi version 3.1.0 in '); - $annotations[0]->validate(); - } -} diff --git a/tests/Annotations/TypeTest.php b/tests/Annotations/TypeTest.php deleted file mode 100644 index c6a888e8..00000000 --- a/tests/Annotations/TypeTest.php +++ /dev/null @@ -1,34 +0,0 @@ -annotationsFromDocBlockParser('@OA\Schema(type="string")'); - $annotations[0]->validate(); - - $annotations = $this->annotationsFromDocBlockParser('@OA\Schema(type="string")'); - $annotations[0]->_context = $this->getContext([], OA\OpenApi::VERSION_3_1_0); - $annotations[0]->validate(); - } - - public function testTypeOfArray(): void - { - $annotations = $this->annotationsFromDocBlockParser('@OA\Schema(type={"string", "null"})'); - $this->assertOpenApiLogEntryContains('@OA\Schema() "type" must be of type string in OpenApi version 3.0.0, array given in '); - $annotations[0]->validate(); - - $annotations = $this->annotationsFromDocBlockParser('@OA\Schema(type={"string", "null"})'); - $annotations[0]->_context = $this->getContext([], OA\OpenApi::VERSION_3_1_0); - $annotations[0]->validate(); - } -} From e3edbbe9d7835aa1899fc5996bdd4d734b6b33f7 Mon Sep 17 00:00:00 2001 From: DjordyKoert Date: Thu, 23 Nov 2023 09:43:23 +0100 Subject: [PATCH 4/7] Remove validation --- src/Annotations/Schema.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Annotations/Schema.php b/src/Annotations/Schema.php index cba563bc..fef02181 100644 --- a/src/Annotations/Schema.php +++ b/src/Annotations/Schema.php @@ -490,18 +490,6 @@ public function validate(array $stack = [], array $skip = [], string $ref = '', return false; } - if ($this->nullable !== Generator::UNDEFINED && $this->_context->isVersion(OpenApi::VERSION_3_1_0)) { - $this->_context->logger->error($this->identity() . ' must not have the "nullable" property when using OpenApi version ' . OpenApi::VERSION_3_1_0 . ' in ' . $this->_context); - - return false; - } - - if (is_array($this->type) && $this->_context->isVersion(OpenApi::VERSION_3_0_0)) { - $this->_context->logger->error($this->identity() . ' "type" must be of type string in OpenApi version ' . OpenApi::VERSION_3_0_0 . ', array given in ' . $this->_context); - - return false; - } - return parent::validate($stack, $skip, $ref, $context); } } From a3e006755207d69e570253321b320a6c192bab2d Mon Sep 17 00:00:00 2001 From: DjordyKoert Date: Fri, 24 Nov 2023 10:00:39 +0100 Subject: [PATCH 5/7] Add Nullable test for multiple types --- tests/Fixtures/Scratch/Nullable.php | 3 +++ tests/Fixtures/Scratch/Nullable.yaml | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/tests/Fixtures/Scratch/Nullable.php b/tests/Fixtures/Scratch/Nullable.php index f8628928..f840e646 100644 --- a/tests/Fixtures/Scratch/Nullable.php +++ b/tests/Fixtures/Scratch/Nullable.php @@ -44,6 +44,9 @@ class Nullable #[OAT\Property] public MyDateTime|null $anotherdate; + + #[OAT\Property(type: ['string', 'null'])] + public ?string $description; } #[OAT\Get( diff --git a/tests/Fixtures/Scratch/Nullable.yaml b/tests/Fixtures/Scratch/Nullable.yaml index 0971e208..b6c546ce 100644 --- a/tests/Fixtures/Scratch/Nullable.yaml +++ b/tests/Fixtures/Scratch/Nullable.yaml @@ -47,4 +47,8 @@ components: $ref: '#/components/schemas/MyDateTime' - type: 'null' + description: + type: + - string + - 'null' type: object From d4aab3dda9bec2242be0f2f9a74d1f30eefdd185 Mon Sep 17 00:00:00 2001 From: DjordyKoert Date: Fri, 24 Nov 2023 10:01:26 +0100 Subject: [PATCH 6/7] Fix TypeError when type is array --- src/Processors/AugmentProperties.php | 6 ++++-- src/Processors/AugmentSchemas.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Processors/AugmentProperties.php b/src/Processors/AugmentProperties.php index 395ac8c2..68e575c0 100644 --- a/src/Processors/AugmentProperties.php +++ b/src/Processors/AugmentProperties.php @@ -51,7 +51,9 @@ public function __invoke(Analysis $analysis) if (Generator::isDefault($property->type)) { $this->augmentType($analysis, $property, $context, $refs, $typeAndDescription['type']); } else { - $this->mapNativeType($property, $property->type); + if (!is_array($property->type)) { + $this->mapNativeType($property, $property->type); + } } if (Generator::isDefault($property->description) && $typeAndDescription['description']) { @@ -129,7 +131,7 @@ protected function augmentType(Analysis $analysis, OA\Property $property, Contex if (Generator::isDefault($property->ref) && array_key_exists($refKey, $refs)) { $this->applyRef($analysis, $property, $refs[$refKey]); } else { - if ($typeSchema = $analysis->getSchemaForSource($context->type)) { + if (is_string($context->type) && $typeSchema = $analysis->getSchemaForSource($context->type)) { if (Generator::isDefault($property->format)) { $property->ref = OA\Components::ref($typeSchema); $property->type = Generator::UNDEFINED; diff --git a/src/Processors/AugmentSchemas.php b/src/Processors/AugmentSchemas.php index 47952b0c..a07b2aa3 100644 --- a/src/Processors/AugmentSchemas.php +++ b/src/Processors/AugmentSchemas.php @@ -103,7 +103,7 @@ protected function augmentType(Analysis $analysis, array $schemas): void $schema->type = 'object'; } } else { - if ($typeSchema = $analysis->getSchemaForSource($schema->type)) { + if (is_string($schema->type) && $typeSchema = $analysis->getSchemaForSource($schema->type)) { if (Generator::isDefault($schema->format)) { $schema->ref = OA\Components::ref($typeSchema); $schema->type = Generator::UNDEFINED; From f712c8a54d3b5fa36eed5feb0299baac4b30ca97 Mon Sep 17 00:00:00 2001 From: DjordyKoert Date: Fri, 24 Nov 2023 10:11:37 +0100 Subject: [PATCH 7/7] Add Types31 test --- tests/Fixtures/Scratch/Types31.php | 33 +++++++++++++++++++++++++++++ tests/Fixtures/Scratch/Types31.yaml | 30 ++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/Fixtures/Scratch/Types31.php create mode 100644 tests/Fixtures/Scratch/Types31.yaml diff --git a/tests/Fixtures/Scratch/Types31.php b/tests/Fixtures/Scratch/Types31.php new file mode 100644 index 00000000..04a473f0 --- /dev/null +++ b/tests/Fixtures/Scratch/Types31.php @@ -0,0 +1,33 @@ +