From e33b4103be89baab7a69585758361e8e0e8f3297 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 5 Feb 2025 19:42:29 +0530 Subject: [PATCH 01/25] Create PR --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ea32da82..a077b532 100644 --- a/README.md +++ b/README.md @@ -298,3 +298,4 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). + From e5751c286c8a884ee55ec117ff6e6874f6ccbfa2 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 7 Feb 2025 14:51:43 +0530 Subject: [PATCH 02/25] Implementation in progress --- Makefile | 6 ++++++ README.md | 1 - src/spec/OpenApi.php | 3 +-- src/spec/Operation.php | 2 +- tests/WriterTest.php | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index acfca9e2..0d7ca4cf 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,12 @@ fix-style: php-cs-fixer.phar $(DOCKER_PHP) vendor/bin/indent --spaces .php_cs.dist $(DOCKER_PHP) ./php-cs-fixer.phar fix src/ --diff +cli: + docker-compose run --rm php bash + +#cli_root: +# docker-compose exec --user="root" php bash + install: $(DOCKER_PHP) composer install --prefer-dist --no-interaction --no-progress --ansi $(DOCKER_NODE) yarn install diff --git a/README.md b/README.md index a077b532..ea32da82 100644 --- a/README.md +++ b/README.md @@ -298,4 +298,3 @@ Professional support, consulting as well as software development services are av https://www.cebe.cc/en/contact Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud). - diff --git a/src/spec/OpenApi.php b/src/spec/OpenApi.php index 29d38b38..4f62be28 100644 --- a/src/spec/OpenApi.php +++ b/src/spec/OpenApi.php @@ -7,7 +7,6 @@ namespace cebe\openapi\spec; -use cebe\openapi\exceptions\TypeErrorException; use cebe\openapi\SpecBaseObject; /** @@ -38,7 +37,7 @@ protected function attributes(): array 'servers' => [Server::class], 'paths' => Paths::class, 'components' => Components::class, - 'security' => [SecurityRequirement::class], + 'security' => [Type::STRING, SecurityRequirement::class], 'tags' => [Tag::class], 'externalDocs' => ExternalDocumentation::class, ]; diff --git a/src/spec/Operation.php b/src/spec/Operation.php index b0525327..f306a571 100644 --- a/src/spec/Operation.php +++ b/src/spec/Operation.php @@ -46,7 +46,7 @@ protected function attributes(): array 'responses' => Responses::class, 'callbacks' => [Type::STRING, Callback::class], 'deprecated' => Type::BOOLEAN, - 'security' => [SecurityRequirement::class], + 'security' => [Type::STRING, SecurityRequirement::class], 'servers' => [Server::class], ]; } diff --git a/tests/WriterTest.php b/tests/WriterTest.php index fc415b51..30c1fd82 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -1,6 +1,11 @@ new Components([ + 'securitySchemes' => [ + 'BearerAuth' => new SecurityScheme([ + 'type' => 'http', + 'scheme' => 'bearer', + 'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes + ]) + ], + ]), + 'paths' => [ + '/test' => new PathItem([ + 'get' => new Operation([ + 'security' => [ + 'BearerAuth' => new SecurityRequirement([]) + ], + ]) + ]) + ] + ]); + + $result = json_decode(json_encode($openapi->getSerializableData()), true); + $this->assertTrue($result); + } } From 2f65031383982d09994375d49f7e7d9eb2b09c49 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 7 Feb 2025 16:52:18 +0530 Subject: [PATCH 03/25] In progress... --- composer.json | 2 -- src/Writer.php | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index a3054a52..edabaf1e 100644 --- a/composer.json +++ b/composer.json @@ -28,8 +28,6 @@ "phpunit/phpunit": "^6.5 || ^7.5 || ^8.5 || ^9.4", "oai/openapi-specification": "3.0.3", "mermade/openapi3-examples": "1.0.0", - "apis-guru/openapi-directory": "1.0.0", - "nexmo/api-specification": "1.0.0", "phpstan/phpstan": "^0.12.0" }, "conflict": { diff --git a/src/Writer.php b/src/Writer.php index faa08f46..f598ffca 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -36,6 +36,7 @@ public static function writeToJson(SpecObjectInterface $object, int $flags = JSO public static function writeToYaml(SpecObjectInterface $object): string { return Yaml::dump($object->getSerializableData(), 256, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE); +// return Yaml::dump(json_decode(json_encode($object->getSerializableData()), true), 256, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE); } /** From 0c532a756b6080ab51c8a27e6b738879090047f6 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 11 Feb 2025 14:53:30 +0530 Subject: [PATCH 04/25] Remove comments --- src/Writer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Writer.php b/src/Writer.php index f598ffca..faa08f46 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -36,7 +36,6 @@ public static function writeToJson(SpecObjectInterface $object, int $flags = JSO public static function writeToYaml(SpecObjectInterface $object): string { return Yaml::dump($object->getSerializableData(), 256, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE); -// return Yaml::dump(json_decode(json_encode($object->getSerializableData()), true), 256, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE); } /** From 2876e4af6e5ae2d5943c320bc46f8cc450dccced Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 11 Feb 2025 15:06:06 +0530 Subject: [PATCH 05/25] Finish the test --- tests/WriterTest.php | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/tests/WriterTest.php b/tests/WriterTest.php index 30c1fd82..7e083404 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -195,7 +195,7 @@ public function testWriteEmptySecurityPartYaml() public function testSecurity() { - $openapi = new OpenApi([ + $openapi = $this->createOpenAPI([ 'components' => new Components([ 'securitySchemes' => [ 'BearerAuth' => new SecurityScheme([ @@ -216,7 +216,29 @@ public function testSecurity() ] ]); - $result = json_decode(json_encode($openapi->getSerializableData()), true); - $this->assertTrue($result); + $yaml = \cebe\openapi\Writer::writeToYaml($openapi); + + + $this->assertEquals(preg_replace('~\R~', "\n", << Date: Tue, 11 Feb 2025 16:08:57 +0530 Subject: [PATCH 06/25] Changes --- src/spec/OpenApi.php | 2 +- src/spec/Operation.php | 2 +- tests/WriterTest.php | 57 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/spec/OpenApi.php b/src/spec/OpenApi.php index 4f62be28..d22cd579 100644 --- a/src/spec/OpenApi.php +++ b/src/spec/OpenApi.php @@ -37,7 +37,7 @@ protected function attributes(): array 'servers' => [Server::class], 'paths' => Paths::class, 'components' => Components::class, - 'security' => [Type::STRING, SecurityRequirement::class], + 'security' => [SecurityRequirement::class], 'tags' => [Tag::class], 'externalDocs' => ExternalDocumentation::class, ]; diff --git a/src/spec/Operation.php b/src/spec/Operation.php index f306a571..b0525327 100644 --- a/src/spec/Operation.php +++ b/src/spec/Operation.php @@ -46,7 +46,7 @@ protected function attributes(): array 'responses' => Responses::class, 'callbacks' => [Type::STRING, Callback::class], 'deprecated' => Type::BOOLEAN, - 'security' => [Type::STRING, SecurityRequirement::class], + 'security' => [SecurityRequirement::class], 'servers' => [Server::class], ]; } diff --git a/tests/WriterTest.php b/tests/WriterTest.php index 7e083404..b678cd2d 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -4,6 +4,8 @@ use cebe\openapi\spec\OpenApi; use cebe\openapi\spec\Operation; use cebe\openapi\spec\PathItem; +use cebe\openapi\spec\Response; +use cebe\openapi\spec\Responses; use cebe\openapi\spec\SecurityRequirement; use cebe\openapi\spec\SecurityScheme; @@ -193,7 +195,7 @@ public function testWriteEmptySecurityPartYaml() ); } - public function testSecurity() + public function testSecurityAtPathOperationLevel() { $openapi = $this->createOpenAPI([ 'components' => new Components([ @@ -209,8 +211,11 @@ public function testSecurity() '/test' => new PathItem([ 'get' => new Operation([ 'security' => [ - 'BearerAuth' => new SecurityRequirement([]) + new SecurityRequirement(['BearerAuth' => []]) ], + 'responses' => new Responses([ + 200 => new Response(['description' => 'OK']), + ]) ]) ]) ] @@ -227,8 +232,12 @@ public function testSecurity() paths: /test: get: + responses: + '200': + description: OK security: - BearerAuth: { } + - + BearerAuth: [] components: securitySchemes: BearerAuth: @@ -236,6 +245,48 @@ public function testSecurity() scheme: bearer bearerFormat: 'AuthToken and JWT Format' +YAML + ), + $yaml + ); + } + + public function testSecurityAtGlobalLevel() + { + $openapi = $this->createOpenAPI([ + 'components' => new Components([ + 'securitySchemes' => [ + 'BearerAuth' => new SecurityScheme([ + 'type' => 'http', + 'scheme' => 'bearer', + 'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes + ]) + ], + ]), + 'security' => [ + 'BearerAuth' => new SecurityRequirement([]) + ], + 'paths' => [], + ]); + + $yaml = \cebe\openapi\Writer::writeToYaml($openapi); + + + $this->assertEquals(preg_replace('~\R~', "\n", << Date: Wed, 12 Feb 2025 10:45:33 +0530 Subject: [PATCH 07/25] Add `SecurityRequirements` class and more --- src/spec/OpenApi.php | 2 +- src/spec/Operation.php | 2 +- src/spec/SecurityRequirements.php | 37 +++++++++++++ tests/WriterTest.php | 89 ++++++++++++++++--------------- 4 files changed, 84 insertions(+), 46 deletions(-) create mode 100644 src/spec/SecurityRequirements.php diff --git a/src/spec/OpenApi.php b/src/spec/OpenApi.php index d22cd579..34af5587 100644 --- a/src/spec/OpenApi.php +++ b/src/spec/OpenApi.php @@ -37,7 +37,7 @@ protected function attributes(): array 'servers' => [Server::class], 'paths' => Paths::class, 'components' => Components::class, - 'security' => [SecurityRequirement::class], + 'security' => [SecurityRequirements::class], 'tags' => [Tag::class], 'externalDocs' => ExternalDocumentation::class, ]; diff --git a/src/spec/Operation.php b/src/spec/Operation.php index b0525327..d5a33d43 100644 --- a/src/spec/Operation.php +++ b/src/spec/Operation.php @@ -46,7 +46,7 @@ protected function attributes(): array 'responses' => Responses::class, 'callbacks' => [Type::STRING, Callback::class], 'deprecated' => Type::BOOLEAN, - 'security' => [SecurityRequirement::class], + 'security' => [SecurityRequirements::class], 'servers' => [Server::class], ]; } diff --git a/src/spec/SecurityRequirements.php b/src/spec/SecurityRequirements.php new file mode 100644 index 00000000..b234675a --- /dev/null +++ b/src/spec/SecurityRequirements.php @@ -0,0 +1,37 @@ + and contributors + * @license https://github.com/cebe/php-openapi/blob/master/LICENSE + */ + +namespace cebe\openapi\spec; + +use cebe\openapi\SpecBaseObject; + +/** + * Lists the required security schemes to execute this operation. + * + * @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#securityRequirementObject + * TODO docs + */ +class SecurityRequirements extends SpecBaseObject +{ + /** + * @return array array of attributes available in this object. + */ + protected function attributes(): array + { + // this object does not have a fixed set of attribute names + return [Type::STRING, SecurityRequirement::class]; + } + + /** + * Perform validation on this object, check data against OpenAPI Specification rules. + * + * Call `addError()` in case of validation errors. + */ + protected function performValidation() + { + } +} diff --git a/tests/WriterTest.php b/tests/WriterTest.php index b678cd2d..51f602ad 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -7,6 +7,7 @@ use cebe\openapi\spec\Response; use cebe\openapi\spec\Responses; use cebe\openapi\spec\SecurityRequirement; +use cebe\openapi\spec\SecurityRequirements; use cebe\openapi\spec\SecurityScheme; class WriterTest extends \PHPUnit\Framework\TestCase @@ -210,9 +211,9 @@ public function testSecurityAtPathOperationLevel() 'paths' => [ '/test' => new PathItem([ 'get' => new Operation([ - 'security' => [ - new SecurityRequirement(['BearerAuth' => []]) - ], + 'security' => new SecurityRequirements([ + 'BearerAuth' => new SecurityRequirement([]) + ]), 'responses' => new Responses([ 200 => new Response(['description' => 'OK']), ]) @@ -251,45 +252,45 @@ public function testSecurityAtPathOperationLevel() ); } - public function testSecurityAtGlobalLevel() - { - $openapi = $this->createOpenAPI([ - 'components' => new Components([ - 'securitySchemes' => [ - 'BearerAuth' => new SecurityScheme([ - 'type' => 'http', - 'scheme' => 'bearer', - 'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes - ]) - ], - ]), - 'security' => [ - 'BearerAuth' => new SecurityRequirement([]) - ], - 'paths' => [], - ]); - - $yaml = \cebe\openapi\Writer::writeToYaml($openapi); - - - $this->assertEquals(preg_replace('~\R~', "\n", <<createOpenAPI([ +// 'components' => new Components([ +// 'securitySchemes' => [ +// 'BearerAuth' => new SecurityScheme([ +// 'type' => 'http', +// 'scheme' => 'bearer', +// 'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes +// ]) +// ], +// ]), +// 'security' => [ +// 'BearerAuth' => new SecurityRequirement([]) +// ], +// 'paths' => [], +// ]); +// +// $yaml = \cebe\openapi\Writer::writeToYaml($openapi); +// +// +// $this->assertEquals(preg_replace('~\R~', "\n", << Date: Wed, 12 Feb 2025 12:05:53 +0530 Subject: [PATCH 08/25] In Progress --- src/spec/Operation.php | 2 +- src/spec/SecurityRequirements.php | 5 ++++- tests/WriterTest.php | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/spec/Operation.php b/src/spec/Operation.php index d5a33d43..8becca10 100644 --- a/src/spec/Operation.php +++ b/src/spec/Operation.php @@ -46,7 +46,7 @@ protected function attributes(): array 'responses' => Responses::class, 'callbacks' => [Type::STRING, Callback::class], 'deprecated' => Type::BOOLEAN, - 'security' => [SecurityRequirements::class], + 'security' => SecurityRequirements::class, 'servers' => [Server::class], ]; } diff --git a/src/spec/SecurityRequirements.php b/src/spec/SecurityRequirements.php index b234675a..718f20d0 100644 --- a/src/spec/SecurityRequirements.php +++ b/src/spec/SecurityRequirements.php @@ -22,8 +22,11 @@ class SecurityRequirements extends SpecBaseObject */ protected function attributes(): array { +// (Type::STRING => Type::ANY)[] + // this object does not have a fixed set of attribute names - return [Type::STRING, SecurityRequirement::class]; + return []; +// return [Type::STRING, SecurityRequirement::class]; } /** diff --git a/tests/WriterTest.php b/tests/WriterTest.php index 51f602ad..a39e49f4 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -212,7 +212,7 @@ public function testSecurityAtPathOperationLevel() '/test' => new PathItem([ 'get' => new Operation([ 'security' => new SecurityRequirements([ - 'BearerAuth' => new SecurityRequirement([]) + 'BearerAuth' => new SecurityRequirement([]), ]), 'responses' => new Responses([ 200 => new Response(['description' => 'OK']), From 6fd4971e3e793e0e4b1908209a7f2542693cd76d Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 12 Feb 2025 12:07:26 +0530 Subject: [PATCH 09/25] Apply same changes to other file --- src/spec/OpenApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spec/OpenApi.php b/src/spec/OpenApi.php index 34af5587..4a8aa243 100644 --- a/src/spec/OpenApi.php +++ b/src/spec/OpenApi.php @@ -37,7 +37,7 @@ protected function attributes(): array 'servers' => [Server::class], 'paths' => Paths::class, 'components' => Components::class, - 'security' => [SecurityRequirements::class], + 'security' => SecurityRequirements::class, 'tags' => [Tag::class], 'externalDocs' => ExternalDocumentation::class, ]; From b7c0a6d3011a65bfa42ab142206b83089023c484 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 14 Mar 2025 15:51:00 +0530 Subject: [PATCH 10/25] Revert changes --- src/spec/OpenApi.php | 2 +- src/spec/Operation.php | 2 +- tests/WriterTest.php | 4 +--- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/spec/OpenApi.php b/src/spec/OpenApi.php index 4a8aa243..d22cd579 100644 --- a/src/spec/OpenApi.php +++ b/src/spec/OpenApi.php @@ -37,7 +37,7 @@ protected function attributes(): array 'servers' => [Server::class], 'paths' => Paths::class, 'components' => Components::class, - 'security' => SecurityRequirements::class, + 'security' => [SecurityRequirement::class], 'tags' => [Tag::class], 'externalDocs' => ExternalDocumentation::class, ]; diff --git a/src/spec/Operation.php b/src/spec/Operation.php index 8becca10..b0525327 100644 --- a/src/spec/Operation.php +++ b/src/spec/Operation.php @@ -46,7 +46,7 @@ protected function attributes(): array 'responses' => Responses::class, 'callbacks' => [Type::STRING, Callback::class], 'deprecated' => Type::BOOLEAN, - 'security' => SecurityRequirements::class, + 'security' => [SecurityRequirement::class], 'servers' => [Server::class], ]; } diff --git a/tests/WriterTest.php b/tests/WriterTest.php index a39e49f4..99f28da5 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -211,9 +211,7 @@ public function testSecurityAtPathOperationLevel() 'paths' => [ '/test' => new PathItem([ 'get' => new Operation([ - 'security' => new SecurityRequirements([ - 'BearerAuth' => new SecurityRequirement([]), - ]), + 'security' => [new SecurityRequirement(['BearerAuth' => []])], 'responses' => new Responses([ 200 => new Response(['description' => 'OK']), ]) From 29f5bbbcde2382dbbd5012151cd1ee94bc1653ba Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 14 Mar 2025 17:07:56 +0530 Subject: [PATCH 11/25] Change design of SecurityScheme --- src/spec/SecurityScheme.php | 9 ++++++++ src/spec/SecuritySchemes.php | 44 ++++++++++++++++++++++++++++++++++++ tests/WriterTest.php | 17 +++++++------- 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 src/spec/SecuritySchemes.php diff --git a/src/spec/SecurityScheme.php b/src/spec/SecurityScheme.php index 2962f743..eb52ad86 100644 --- a/src/spec/SecurityScheme.php +++ b/src/spec/SecurityScheme.php @@ -25,6 +25,8 @@ */ class SecurityScheme extends SpecBaseObject { + public $name; + public $scheme; private $knownTypes = [ "apiKey", "http", @@ -32,6 +34,13 @@ class SecurityScheme extends SpecBaseObject "openIdConnect" ]; + public function __construct(array $data) + { + parent::__construct($data); + $this->name = array_keys($data)[0]; + $this->scheme = array_values($data)[0]; + } + /** * @return array array of attributes available in this object. */ diff --git a/src/spec/SecuritySchemes.php b/src/spec/SecuritySchemes.php new file mode 100644 index 00000000..28c7fbf2 --- /dev/null +++ b/src/spec/SecuritySchemes.php @@ -0,0 +1,44 @@ + and contributors + * @license https://github.com/cebe/php-openapi/blob/master/LICENSE + */ + +namespace cebe\openapi\spec; + +use cebe\openapi\SpecBaseObject; + +class SecuritySchemes extends SpecBaseObject +{ + private $_securitySchemes = []; + + public function __construct(array $data) + { + parent::__construct($data); + $this->_securitySchemes = $data; + } + + protected function attributes(): array + { + return []; + } + + public function performValidation() + { + } + + /** + * @return mixed returns the serializable data of this object for converting it + * to JSON or YAML. + */ + public function getSerializableData() + { + $data = []; + foreach ($this->_securitySchemes as $securityScheme) { + /** @var $securityScheme SecurityScheme */ + $data[$securityScheme->name] = $securityScheme->scheme; + } + return (object) $data; + } +} \ No newline at end of file diff --git a/tests/WriterTest.php b/tests/WriterTest.php index 99f28da5..5e74d882 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -1,14 +1,13 @@ createOpenAPI([ 'components' => new Components([ - 'securitySchemes' => [ - 'BearerAuth' => new SecurityScheme([ - 'type' => 'http', - 'scheme' => 'bearer', - 'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes + 'securitySchemes' => new SecuritySchemes([ + new SecurityScheme([ + 'BearerAuth' => [ + 'type' => 'http', + 'scheme' => 'bearer', + 'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes + ] ]) - ], + ]), ]), 'paths' => [ '/test' => new PathItem([ From b46de764dd652791d82f81af7686d3810c8b0a86 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 14 Mar 2025 18:14:13 +0530 Subject: [PATCH 12/25] Complete the implementation --- src/spec/SecurityRequirements.php | 23 +++++++++++++++++++++++ src/spec/SecuritySchemes.php | 11 ++++++----- tests/WriterTest.php | 17 +++++++++-------- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/spec/SecurityRequirements.php b/src/spec/SecurityRequirements.php index 718f20d0..a17e5227 100644 --- a/src/spec/SecurityRequirements.php +++ b/src/spec/SecurityRequirements.php @@ -17,6 +17,14 @@ */ class SecurityRequirements extends SpecBaseObject { + private $_securityRequirements; + + public function __construct(array $data) + { + parent::__construct($data); + $this->_securityRequirements = $data; + } + /** * @return array array of attributes available in this object. */ @@ -37,4 +45,19 @@ protected function attributes(): array protected function performValidation() { } + + /** + * @return mixed returns the serializable data of this object for converting it + * to JSON or YAML. + * TODO + */ + public function getSerializableData() + { + $data = []; + foreach ($this->_securityRequirements as $name => $securityRequirement) { + /** @var SecurityRequirement $securityRequirement */ + $data[] = [$name => json_decode(json_encode($securityRequirement->getSerializableData()), true)]; + } + return $data; + } } diff --git a/src/spec/SecuritySchemes.php b/src/spec/SecuritySchemes.php index 28c7fbf2..c58dd059 100644 --- a/src/spec/SecuritySchemes.php +++ b/src/spec/SecuritySchemes.php @@ -11,7 +11,7 @@ class SecuritySchemes extends SpecBaseObject { - private $_securitySchemes = []; + private $_securitySchemes; public function __construct(array $data) { @@ -31,14 +31,15 @@ public function performValidation() /** * @return mixed returns the serializable data of this object for converting it * to JSON or YAML. + * TODO */ public function getSerializableData() { $data = []; - foreach ($this->_securitySchemes as $securityScheme) { - /** @var $securityScheme SecurityScheme */ - $data[$securityScheme->name] = $securityScheme->scheme; + foreach ($this->_securitySchemes as $name => $securityScheme) { + /** @var SecurityScheme $securityScheme */ + $data[$name] = $securityScheme->getSerializableData(); } return (object) $data; } -} \ No newline at end of file +} diff --git a/tests/WriterTest.php b/tests/WriterTest.php index 5e74d882..99793881 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -6,6 +6,7 @@ use cebe\openapi\spec\Response; use cebe\openapi\spec\Responses; use cebe\openapi\spec\SecurityRequirement; +use cebe\openapi\spec\SecurityRequirements; use cebe\openapi\spec\SecurityScheme; use cebe\openapi\spec\SecuritySchemes; @@ -200,19 +201,19 @@ public function testSecurityAtPathOperationLevel() $openapi = $this->createOpenAPI([ 'components' => new Components([ 'securitySchemes' => new SecuritySchemes([ - new SecurityScheme([ - 'BearerAuth' => [ - 'type' => 'http', - 'scheme' => 'bearer', - 'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes - ] - ]) + 'BearerAuth' => new SecurityScheme([ + 'type' => 'http', + 'scheme' => 'bearer', + 'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes + ]), ]), ]), 'paths' => [ '/test' => new PathItem([ 'get' => new Operation([ - 'security' => [new SecurityRequirement(['BearerAuth' => []])], + 'security' => new SecurityRequirements([ + 'BearerAuth' => new SecurityRequirement([]), + ]), 'responses' => new Responses([ 200 => new Response(['description' => 'OK']), ]) From 219813f7e2826d782e6b705347dac654719b21ba Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 15 Mar 2025 14:34:06 +0530 Subject: [PATCH 13/25] Fix failing test --- Makefile | 3 --- src/spec/OpenApi.php | 2 +- src/spec/Operation.php | 2 +- src/spec/SecurityRequirement.php | 12 ++++++++++++ src/spec/SecurityRequirements.php | 27 ++++++++++++++++++++------- tests/WriterTest.php | 8 ++++++-- tests/spec/OpenApiTest.php | 2 +- tests/spec/OperationTest.php | 9 +++++---- 8 files changed, 46 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 0d7ca4cf..934a37f1 100644 --- a/Makefile +++ b/Makefile @@ -40,9 +40,6 @@ fix-style: php-cs-fixer.phar cli: docker-compose run --rm php bash -#cli_root: -# docker-compose exec --user="root" php bash - install: $(DOCKER_PHP) composer install --prefer-dist --no-interaction --no-progress --ansi $(DOCKER_NODE) yarn install diff --git a/src/spec/OpenApi.php b/src/spec/OpenApi.php index d22cd579..4a8aa243 100644 --- a/src/spec/OpenApi.php +++ b/src/spec/OpenApi.php @@ -37,7 +37,7 @@ protected function attributes(): array 'servers' => [Server::class], 'paths' => Paths::class, 'components' => Components::class, - 'security' => [SecurityRequirement::class], + 'security' => SecurityRequirements::class, 'tags' => [Tag::class], 'externalDocs' => ExternalDocumentation::class, ]; diff --git a/src/spec/Operation.php b/src/spec/Operation.php index b0525327..8becca10 100644 --- a/src/spec/Operation.php +++ b/src/spec/Operation.php @@ -46,7 +46,7 @@ protected function attributes(): array 'responses' => Responses::class, 'callbacks' => [Type::STRING, Callback::class], 'deprecated' => Type::BOOLEAN, - 'security' => [SecurityRequirement::class], + 'security' => SecurityRequirements::class, 'servers' => [Server::class], ]; } diff --git a/src/spec/SecurityRequirement.php b/src/spec/SecurityRequirement.php index 71f805c8..10099cae 100644 --- a/src/spec/SecurityRequirement.php +++ b/src/spec/SecurityRequirement.php @@ -17,6 +17,13 @@ */ class SecurityRequirement extends SpecBaseObject { + private $_securityRequirement; + public function __construct(array $data) + { + parent::__construct($data); + $this->_securityRequirement = $data; + } + /** * @return array array of attributes available in this object. */ @@ -34,4 +41,9 @@ protected function attributes(): array protected function performValidation() { } + + public function getSerializableData() + { + return $this->_securityRequirement; + } } diff --git a/src/spec/SecurityRequirements.php b/src/spec/SecurityRequirements.php index a17e5227..34cb0b19 100644 --- a/src/spec/SecurityRequirements.php +++ b/src/spec/SecurityRequirements.php @@ -10,7 +10,7 @@ use cebe\openapi\SpecBaseObject; /** - * Lists the required security schemes to execute this operation. + * Lists the required security requirement to execute this operation. * * @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#securityRequirementObject * TODO docs @@ -22,7 +22,18 @@ class SecurityRequirements extends SpecBaseObject public function __construct(array $data) { parent::__construct($data); - $this->_securityRequirements = $data; + + $read = true; + foreach($data as $index => $value) { + if (is_numeric($index)) { // read + $requirements = $value; + $this->_securityRequirements[array_keys($value)[0]] = new SecurityRequirement(array_values($value)[0]); + } else { // write + $read = false; + $requirements = $data; + $this->_securityRequirements[$index] = $value; + } + } } /** @@ -30,11 +41,8 @@ public function __construct(array $data) */ protected function attributes(): array { -// (Type::STRING => Type::ANY)[] - // this object does not have a fixed set of attribute names return []; -// return [Type::STRING, SecurityRequirement::class]; } /** @@ -54,10 +62,15 @@ protected function performValidation() public function getSerializableData() { $data = []; - foreach ($this->_securityRequirements as $name => $securityRequirement) { + foreach ($this->_securityRequirements ?? [] as $name => $securityRequirement) { /** @var SecurityRequirement $securityRequirement */ - $data[] = [$name => json_decode(json_encode($securityRequirement->getSerializableData()), true)]; + $data[] = [$name => $securityRequirement->getSerializableData()]; } return $data; } + + public function getRequirement(string $name) + { + return $this->_securityRequirements[$name] ?? 'nul5l'; + } } diff --git a/tests/WriterTest.php b/tests/WriterTest.php index 99793881..792ae364 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -145,7 +145,9 @@ public function testWriteEmptySecurityYaml() public function testWriteEmptySecurityPartJson() { $openapi = $this->createOpenAPI([ - 'security' => [new SecurityRequirement(['Bearer' => []])], + 'security' => new SecurityRequirements([ + 'Bearer' => new SecurityRequirement([]) + ]), ]); $json = \cebe\openapi\Writer::writeToJson($openapi); @@ -174,7 +176,9 @@ public function testWriteEmptySecurityPartJson() public function testWriteEmptySecurityPartYaml() { $openapi = $this->createOpenAPI([ - 'security' => [new SecurityRequirement(['Bearer' => []])], + 'security' => new SecurityRequirements([ + 'Bearer' => new SecurityRequirement([]) + ]), ]); $yaml = \cebe\openapi\Writer::writeToYaml($openapi); diff --git a/tests/spec/OpenApiTest.php b/tests/spec/OpenApiTest.php index 20b568ed..fce2e040 100644 --- a/tests/spec/OpenApiTest.php +++ b/tests/spec/OpenApiTest.php @@ -72,7 +72,7 @@ public function testReadPetStore() $this->assertInstanceOf(\cebe\openapi\spec\Components::class, $openapi->components); // security - $this->assertAllInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $openapi->security); + $this->assertNull($openapi->security); # since it is not present in spec // tags $this->assertAllInstanceOf(\cebe\openapi\spec\Tag::class, $openapi->tags); diff --git a/tests/spec/OperationTest.php b/tests/spec/OperationTest.php index 6e30566f..b60b95ab 100644 --- a/tests/spec/OperationTest.php +++ b/tests/spec/OperationTest.php @@ -79,10 +79,11 @@ public function testRead() $this->assertInstanceOf(\cebe\openapi\spec\Responses::class, $operation->responses); - $this->assertCount(1, $operation->security); - $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $operation->security[0]); - $this->assertCount(2, $operation->security[0]->petstore_auth); - $this->assertEquals(['write:pets', 'read:pets'], $operation->security[0]->petstore_auth); + $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirements::class, $operation->security); + $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $operation->security->getRequirement('petstore_auth')); + // TODO +// $this->assertCount(2, $operation->security[0]->petstore_auth); +// $this->assertEquals(['write:pets', 'read:pets'], $operation->security[0]->petstore_auth); $this->assertInstanceOf(ExternalDocumentation::class, $operation->externalDocs); $this->assertEquals('Find more info here', $operation->externalDocs->description); From 236a47e76b11ac383dfa441898410af7245a4012 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 15 Mar 2025 15:18:20 +0530 Subject: [PATCH 14/25] Cleanup and fix issue and complete the implementation --- src/spec/SecurityRequirements.php | 12 +++++++-- src/spec/SecurityScheme.php | 9 ------- src/spec/SecuritySchemes.php | 45 ------------------------------- tests/WriterTest.php | 5 ++-- tests/spec/SecuritySchemeTest.php | 6 ++--- 5 files changed, 15 insertions(+), 62 deletions(-) delete mode 100644 src/spec/SecuritySchemes.php diff --git a/src/spec/SecurityRequirements.php b/src/spec/SecurityRequirements.php index 34cb0b19..3cd0dd00 100644 --- a/src/spec/SecurityRequirements.php +++ b/src/spec/SecurityRequirements.php @@ -24,7 +24,7 @@ public function __construct(array $data) parent::__construct($data); $read = true; - foreach($data as $index => $value) { + foreach ($data as $index => $value) { if (is_numeric($index)) { // read $requirements = $value; $this->_securityRequirements[array_keys($value)[0]] = new SecurityRequirement(array_values($value)[0]); @@ -34,6 +34,9 @@ public function __construct(array $data) $this->_securityRequirements[$index] = $value; } } + if ($data === []) { + $this->_securityRequirements = []; + } } /** @@ -71,6 +74,11 @@ public function getSerializableData() public function getRequirement(string $name) { - return $this->_securityRequirements[$name] ?? 'nul5l'; + return $this->_securityRequirements[$name] ?? null; + } + + public function getRequirements() + { + return $this->_securityRequirements; } } diff --git a/src/spec/SecurityScheme.php b/src/spec/SecurityScheme.php index eb52ad86..2962f743 100644 --- a/src/spec/SecurityScheme.php +++ b/src/spec/SecurityScheme.php @@ -25,8 +25,6 @@ */ class SecurityScheme extends SpecBaseObject { - public $name; - public $scheme; private $knownTypes = [ "apiKey", "http", @@ -34,13 +32,6 @@ class SecurityScheme extends SpecBaseObject "openIdConnect" ]; - public function __construct(array $data) - { - parent::__construct($data); - $this->name = array_keys($data)[0]; - $this->scheme = array_values($data)[0]; - } - /** * @return array array of attributes available in this object. */ diff --git a/src/spec/SecuritySchemes.php b/src/spec/SecuritySchemes.php deleted file mode 100644 index c58dd059..00000000 --- a/src/spec/SecuritySchemes.php +++ /dev/null @@ -1,45 +0,0 @@ - and contributors - * @license https://github.com/cebe/php-openapi/blob/master/LICENSE - */ - -namespace cebe\openapi\spec; - -use cebe\openapi\SpecBaseObject; - -class SecuritySchemes extends SpecBaseObject -{ - private $_securitySchemes; - - public function __construct(array $data) - { - parent::__construct($data); - $this->_securitySchemes = $data; - } - - protected function attributes(): array - { - return []; - } - - public function performValidation() - { - } - - /** - * @return mixed returns the serializable data of this object for converting it - * to JSON or YAML. - * TODO - */ - public function getSerializableData() - { - $data = []; - foreach ($this->_securitySchemes as $name => $securityScheme) { - /** @var SecurityScheme $securityScheme */ - $data[$name] = $securityScheme->getSerializableData(); - } - return (object) $data; - } -} diff --git a/tests/WriterTest.php b/tests/WriterTest.php index 792ae364..5450b64f 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -8,7 +8,6 @@ use cebe\openapi\spec\SecurityRequirement; use cebe\openapi\spec\SecurityRequirements; use cebe\openapi\spec\SecurityScheme; -use cebe\openapi\spec\SecuritySchemes; class WriterTest extends \PHPUnit\Framework\TestCase { @@ -204,13 +203,13 @@ public function testSecurityAtPathOperationLevel() { $openapi = $this->createOpenAPI([ 'components' => new Components([ - 'securitySchemes' => new SecuritySchemes([ + 'securitySchemes' => [ 'BearerAuth' => new SecurityScheme([ 'type' => 'http', 'scheme' => 'bearer', 'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes ]), - ]), + ], ]), 'paths' => [ '/test' => new PathItem([ diff --git a/tests/spec/SecuritySchemeTest.php b/tests/spec/SecuritySchemeTest.php index 5e14d5f7..4bbbf4d6 100644 --- a/tests/spec/SecuritySchemeTest.php +++ b/tests/spec/SecuritySchemeTest.php @@ -199,10 +199,10 @@ public function testDefaultSecurity() YAML ); - $this->assertSame([], $openapi->paths->getPath('/path/one')->post->security); + $this->assertSame([], $openapi->paths->getPath('/path/one')->post->security->getRequirements()); $this->assertSame(null, $openapi->paths->getPath('/path/two')->post->security); - $this->assertCount(1, $openapi->security); - $this->assertSame([], $openapi->security[0]->Bearer); + $this->assertCount(1, $openapi->security->getRequirements()); + $this->assertSame([], $openapi->security->getRequirement('Bearer')->getSerializableData()); } } From 55f3e1fb95c1100b73fbf78384107f9cb6a3facf Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 15 Mar 2025 15:24:27 +0530 Subject: [PATCH 15/25] Complete the test --- tests/spec/OperationTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/spec/OperationTest.php b/tests/spec/OperationTest.php index b60b95ab..35fd1fdd 100644 --- a/tests/spec/OperationTest.php +++ b/tests/spec/OperationTest.php @@ -81,9 +81,8 @@ public function testRead() $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirements::class, $operation->security); $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $operation->security->getRequirement('petstore_auth')); - // TODO -// $this->assertCount(2, $operation->security[0]->petstore_auth); -// $this->assertEquals(['write:pets', 'read:pets'], $operation->security[0]->petstore_auth); + $this->assertCount(2, $operation->security->getRequirement('petstore_auth')->getSerializableData()); + $this->assertEquals(['write:pets', 'read:pets'], $operation->security->getRequirement('petstore_auth')->getSerializableData()); $this->assertInstanceOf(ExternalDocumentation::class, $operation->externalDocs); $this->assertEquals('Find more info here', $operation->externalDocs->description); From 23a53774874d396aff1682edf20710392178f23a Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 15 Mar 2025 15:55:19 +0530 Subject: [PATCH 16/25] Covers SecurityRequirements --- tests/spec/SecuritySchemeTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/spec/SecuritySchemeTest.php b/tests/spec/SecuritySchemeTest.php index 4bbbf4d6..4dc6a2a1 100644 --- a/tests/spec/SecuritySchemeTest.php +++ b/tests/spec/SecuritySchemeTest.php @@ -11,6 +11,7 @@ * @covers \cebe\openapi\spec\OAuthFlows * @covers \cebe\openapi\spec\OAuthFlow * @covers \cebe\openapi\spec\SecurityRequirement + * @covers \cebe\openapi\spec\SecurityRequirements */ class SecuritySchemeTest extends \PHPUnit\Framework\TestCase { From 565c1830a2171ea28e7999b85632dd6b94537d47 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 15 Mar 2025 16:01:05 +0530 Subject: [PATCH 17/25] Put back removed Composer Package --- Makefile | 1 - composer.json | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 934a37f1..add949a6 100644 --- a/Makefile +++ b/Makefile @@ -85,4 +85,3 @@ coverage: .php-openapi-covA .php-openapi-covB grep -rhPo '^class \w+' src/spec/ | awk '{print $$2}' |grep -v '^Type$$' | sort > $@ .PHONY: all check-style fix-style install test lint coverage - diff --git a/composer.json b/composer.json index edabaf1e..a3054a52 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,8 @@ "phpunit/phpunit": "^6.5 || ^7.5 || ^8.5 || ^9.4", "oai/openapi-specification": "3.0.3", "mermade/openapi3-examples": "1.0.0", + "apis-guru/openapi-directory": "1.0.0", + "nexmo/api-specification": "1.0.0", "phpstan/phpstan": "^0.12.0" }, "conflict": { From c1db3243e0193359617379f3e93438e38a34fbc5 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 15 Mar 2025 16:07:49 +0530 Subject: [PATCH 18/25] Fix failing tests --- tests/spec/OpenApiTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/spec/OpenApiTest.php b/tests/spec/OpenApiTest.php index fce2e040..ad99e743 100644 --- a/tests/spec/OpenApiTest.php +++ b/tests/spec/OpenApiTest.php @@ -221,7 +221,8 @@ public function testSpecs($openApiFile) } // security - $this->assertAllInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $openapi->security); + $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirements::class, $openapi->security); + $this->assertAllInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $openapi->security->getRequirements()); // tags $this->assertAllInstanceOf(\cebe\openapi\spec\Tag::class, $openapi->tags); From c911987755490395b0238ce256ababe947a8a7ac Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 15 Mar 2025 16:12:47 +0530 Subject: [PATCH 19/25] Fix failing tests 2 --- tests/spec/OpenApiTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/spec/OpenApiTest.php b/tests/spec/OpenApiTest.php index ad99e743..26713344 100644 --- a/tests/spec/OpenApiTest.php +++ b/tests/spec/OpenApiTest.php @@ -221,7 +221,7 @@ public function testSpecs($openApiFile) } // security - $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirements::class, $openapi->security); + $openapi->security !== null && $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirements::class, $openapi->security); $this->assertAllInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $openapi->security->getRequirements()); // tags From 98b3ef37c5aebe306e908bd9a22bd533123519db Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Sat, 15 Mar 2025 16:15:55 +0530 Subject: [PATCH 20/25] Fix failing tests 3 --- tests/spec/OpenApiTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/spec/OpenApiTest.php b/tests/spec/OpenApiTest.php index 26713344..433bd352 100644 --- a/tests/spec/OpenApiTest.php +++ b/tests/spec/OpenApiTest.php @@ -222,7 +222,7 @@ public function testSpecs($openApiFile) // security $openapi->security !== null && $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirements::class, $openapi->security); - $this->assertAllInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $openapi->security->getRequirements()); + $openapi->security !== null && $this->assertAllInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $openapi->security->getRequirements()); // tags $this->assertAllInstanceOf(\cebe\openapi\spec\Tag::class, $openapi->tags); From 15ef5f67e3e6101c879318d791fca1169cd01689 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Mon, 17 Mar 2025 07:18:57 +0530 Subject: [PATCH 21/25] Add more assertions in tests and refactor --- src/spec/SecurityRequirement.php | 1 + src/spec/SecurityRequirements.php | 7 +++---- tests/WriterTest.php | 1 + tests/spec/OperationTest.php | 1 + 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/spec/SecurityRequirement.php b/src/spec/SecurityRequirement.php index 10099cae..a5e0c619 100644 --- a/src/spec/SecurityRequirement.php +++ b/src/spec/SecurityRequirement.php @@ -44,6 +44,7 @@ protected function performValidation() public function getSerializableData() { + parent::getSerializableData(); return $this->_securityRequirement; } } diff --git a/src/spec/SecurityRequirements.php b/src/spec/SecurityRequirements.php index 3cd0dd00..7821ff2a 100644 --- a/src/spec/SecurityRequirements.php +++ b/src/spec/SecurityRequirements.php @@ -23,14 +23,10 @@ public function __construct(array $data) { parent::__construct($data); - $read = true; foreach ($data as $index => $value) { if (is_numeric($index)) { // read - $requirements = $value; $this->_securityRequirements[array_keys($value)[0]] = new SecurityRequirement(array_values($value)[0]); } else { // write - $read = false; - $requirements = $data; $this->_securityRequirements[$index] = $value; } } @@ -55,6 +51,7 @@ protected function attributes(): array */ protected function performValidation() { + // TODO } /** @@ -64,6 +61,8 @@ protected function performValidation() */ public function getSerializableData() { + parent::getSerializableData(); + $data = []; foreach ($this->_securityRequirements ?? [] as $name => $securityRequirement) { /** @var SecurityRequirement $securityRequirement */ diff --git a/tests/WriterTest.php b/tests/WriterTest.php index 5450b64f..57ca5d09 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -255,6 +255,7 @@ public function testSecurityAtPathOperationLevel() ); } + // TODO // public function testSecurityAtGlobalLevel() // { // $openapi = $this->createOpenAPI([ diff --git a/tests/spec/OperationTest.php b/tests/spec/OperationTest.php index 35fd1fdd..734db321 100644 --- a/tests/spec/OperationTest.php +++ b/tests/spec/OperationTest.php @@ -79,6 +79,7 @@ public function testRead() $this->assertInstanceOf(\cebe\openapi\spec\Responses::class, $operation->responses); + $this->assertCount(1, $operation->security->getRequirements()); $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirements::class, $operation->security); $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $operation->security->getRequirement('petstore_auth')); $this->assertCount(2, $operation->security->getRequirement('petstore_auth')->getSerializableData()); From 19e68bfbd4828f4eefbb14c31cda5a5b0ec8e0ea Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Mon, 17 Mar 2025 07:30:34 +0530 Subject: [PATCH 22/25] Add more test --- tests/WriterTest.php | 84 ++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/tests/WriterTest.php b/tests/WriterTest.php index 57ca5d09..355a7449 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -255,46 +255,46 @@ public function testSecurityAtPathOperationLevel() ); } - // TODO -// public function testSecurityAtGlobalLevel() -// { -// $openapi = $this->createOpenAPI([ -// 'components' => new Components([ -// 'securitySchemes' => [ -// 'BearerAuth' => new SecurityScheme([ -// 'type' => 'http', -// 'scheme' => 'bearer', -// 'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes -// ]) -// ], -// ]), -// 'security' => [ -// 'BearerAuth' => new SecurityRequirement([]) -// ], -// 'paths' => [], -// ]); -// -// $yaml = \cebe\openapi\Writer::writeToYaml($openapi); -// -// -// $this->assertEquals(preg_replace('~\R~', "\n", <<createOpenAPI([ + 'components' => new Components([ + 'securitySchemes' => [ + 'BearerAuth' => new SecurityScheme([ + 'type' => 'http', + 'scheme' => 'bearer', + 'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes + ]) + ], + ]), + 'security' => new SecurityRequirements([ + 'BearerAuth' => new SecurityRequirement([]) + ]), + 'paths' => [], + ]); + + $yaml = \cebe\openapi\Writer::writeToYaml($openapi); + + + $this->assertEquals(preg_replace('~\R~', "\n", << Date: Mon, 17 Mar 2025 16:28:28 +0530 Subject: [PATCH 23/25] Resolve TODOs --- src/spec/SecurityRequirement.php | 2 +- src/spec/SecurityRequirements.php | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/spec/SecurityRequirement.php b/src/spec/SecurityRequirement.php index a5e0c619..069c7711 100644 --- a/src/spec/SecurityRequirement.php +++ b/src/spec/SecurityRequirement.php @@ -10,7 +10,7 @@ use cebe\openapi\SpecBaseObject; /** - * Lists the required security schemes to execute this operation. + * A required security scheme to execute this operation. * * @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#securityRequirementObject * diff --git a/src/spec/SecurityRequirements.php b/src/spec/SecurityRequirements.php index 7821ff2a..3b3bbb90 100644 --- a/src/spec/SecurityRequirements.php +++ b/src/spec/SecurityRequirements.php @@ -10,10 +10,10 @@ use cebe\openapi\SpecBaseObject; /** - * Lists the required security requirement to execute this operation. + * Lists the required security schemes to execute this operation. * * @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#securityRequirementObject - * TODO docs + * */ class SecurityRequirements extends SpecBaseObject { @@ -51,13 +51,10 @@ protected function attributes(): array */ protected function performValidation() { - // TODO } /** - * @return mixed returns the serializable data of this object for converting it - * to JSON or YAML. - * TODO + * {@inheritDoc} */ public function getSerializableData() { From 2a660593cbe4b8bf650db6d648f36bdda66b86bd Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 25 Apr 2025 18:22:59 +0530 Subject: [PATCH 24/25] Remove redundant method call --- src/spec/SecurityRequirement.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/spec/SecurityRequirement.php b/src/spec/SecurityRequirement.php index 069c7711..ec3e1d90 100644 --- a/src/spec/SecurityRequirement.php +++ b/src/spec/SecurityRequirement.php @@ -44,7 +44,6 @@ protected function performValidation() public function getSerializableData() { - parent::getSerializableData(); return $this->_securityRequirement; } } From aa73b3d3ae8b745cf794dc8e7a2f6403b11d6b77 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 29 Apr 2025 12:39:34 +0530 Subject: [PATCH 25/25] Remove redundant method call 2 --- src/spec/SecurityRequirements.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/spec/SecurityRequirements.php b/src/spec/SecurityRequirements.php index 3b3bbb90..cbda1ba7 100644 --- a/src/spec/SecurityRequirements.php +++ b/src/spec/SecurityRequirements.php @@ -58,8 +58,6 @@ protected function performValidation() */ public function getSerializableData() { - parent::getSerializableData(); - $data = []; foreach ($this->_securityRequirements ?? [] as $name => $securityRequirement) { /** @var SecurityRequirement $securityRequirement */