Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ 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

install: composer.json package.json
$(DOCKER_PHP) composer install --prefer-dist --no-interaction --no-progress --ansi
Expand Down
50 changes: 27 additions & 23 deletions src/spec/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
*/
class OpenApi extends SpecBaseObject
{
const VERSION_3_0 = '3.0';
const VERSION_3_1 = '3.1';
const VERSION_UNSUPPORTED = 'unsupported';
const VERSION_3_0 = "3.0";
const VERSION_3_1 = "3.1";
const VERSION_UNSUPPORTED = "unsupported";

/**
* Pattern used to validate OpenAPI versions.
Expand All @@ -42,15 +42,15 @@ class OpenApi extends SpecBaseObject
protected function attributes(): array
{
return [
'openapi' => Type::STRING,
'info' => Info::class,
'servers' => [Server::class],
'paths' => Paths::class,
'webhooks' => [PathItem::class],
'components' => Components::class,
'security' => SecurityRequirements::class,
'tags' => [Tag::class],
'externalDocs' => ExternalDocumentation::class,
"openapi" => Type::STRING,
"info" => Info::class,
"servers" => [Server::class],
"paths" => Paths::class,
"webhooks" => [PathItem::class],
"components" => Components::class,
"security" => [SecurityRequirement::class],
"tags" => [Tag::class],
"externalDocs" => ExternalDocumentation::class,
];
}

Expand All @@ -62,9 +62,7 @@ protected function attributeDefaults(): array
return [
// Spec: If the servers property is not provided, or is an empty array,
// the default value would be a Server Object with a url value of /.
'servers' => [
new Server(['url' => '/'])
],
"servers" => [new Server(["url" => "/"])],
];
}

Expand All @@ -73,8 +71,8 @@ public function __get($name)
$ret = parent::__get($name);
// Spec: If the servers property is not provided, or is an empty array,
// the default value would be a Server Object with a url value of /.
if ($name === 'servers' && $ret === []) {
return $this->attributeDefaults()['servers'];
if ($name === "servers" && $ret === []) {
return $this->attributeDefaults()["servers"];
}
return $ret;
}
Expand All @@ -85,13 +83,19 @@ public function __get($name)
public function performValidation()
{
if ($this->getMajorVersion() === static::VERSION_3_0) {
$this->requireProperties(['openapi', 'info', 'paths']);
$this->requireProperties(["openapi", "info", "paths"]);
} else {
$this->requireProperties(['openapi', 'info'], ['paths', 'webhooks', 'components']);
$this->requireProperties(
["openapi", "info"],
["paths", "webhooks", "components"],
);
}

if (!empty($this->openapi) && !preg_match(static::PATTERN_VERSION, $this->openapi)) {
$this->addError('Unsupported openapi version: ' . $this->openapi);
if (
!empty($this->openapi) &&
!preg_match(static::PATTERN_VERSION, $this->openapi)
) {
$this->addError("Unsupported openapi version: " . $this->openapi);
}
}

Expand All @@ -111,9 +115,9 @@ public function getMajorVersion()
}
if (preg_match(static::PATTERN_VERSION, $this->openapi, $matches)) {
switch ($matches[1]) {
case '3.0':
case "3.0":
return static::VERSION_3_0;
case '3.1':
case "3.1":
return static::VERSION_3_1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/spec/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}
Expand Down
14 changes: 1 addition & 13 deletions src/spec/SecurityRequirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,13 @@
use cebe\openapi\SpecBaseObject;

/**
* A required security scheme 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
*
*/
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.
*/
Expand All @@ -41,9 +34,4 @@ protected function attributes(): array
protected function performValidation()
{
}

public function getSerializableData()
{
return $this->_securityRequirement;
}
}
78 changes: 0 additions & 78 deletions src/spec/SecurityRequirements.php

This file was deleted.

114 changes: 2 additions & 112 deletions tests/WriterTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
<?php

use cebe\openapi\spec\Components;
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\SecurityRequirements;
use cebe\openapi\spec\SecurityScheme;

class WriterTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -144,9 +137,7 @@ public function testWriteEmptySecurityYaml()
public function testWriteEmptySecurityPartJson()
{
$openapi = $this->createOpenAPI([
'security' => new SecurityRequirements([
'Bearer' => new SecurityRequirement([])
]),
'security' => [new SecurityRequirement(['Bearer' => []])],
]);

$json = \cebe\openapi\Writer::writeToJson($openapi);
Expand Down Expand Up @@ -175,9 +166,7 @@ public function testWriteEmptySecurityPartJson()
public function testWriteEmptySecurityPartYaml()
{
$openapi = $this->createOpenAPI([
'security' => new SecurityRequirements([
'Bearer' => new SecurityRequirement([])
]),
'security' => [new SecurityRequirement(['Bearer' => []])],
]);

$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
Expand All @@ -193,105 +182,6 @@ public function testWriteEmptySecurityPartYaml()
-
Bearer: []

YAML
),
$yaml
);
}

public function testSecurityAtPathOperationLevel()
{
$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
]),
],
]),
'paths' => [
'/test' => new PathItem([
'get' => new Operation([
'security' => new SecurityRequirements([
'BearerAuth' => new SecurityRequirement([]),
]),
'responses' => new Responses([
200 => new Response(['description' => 'OK']),
])
])
])
]
]);

$yaml = \cebe\openapi\Writer::writeToYaml($openapi);


$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
openapi: 3.0.0
info:
title: 'Test API'
version: 1.0.0
paths:
/test:
get:
responses:
'200':
description: OK
security:
-
BearerAuth: []
components:
securitySchemes:
BearerAuth:
type: http
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' => new SecurityRequirements([
'BearerAuth' => new SecurityRequirement([])
]),
'paths' => [],
]);

$yaml = \cebe\openapi\Writer::writeToYaml($openapi);


$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
openapi: 3.0.0
info:
title: 'Test API'
version: 1.0.0
paths: { }
components:
securitySchemes:
BearerAuth:
type: http
scheme: bearer
bearerFormat: 'AuthToken and JWT Format'
security:
-
BearerAuth: []

YAML
),
$yaml
Expand Down
5 changes: 2 additions & 3 deletions tests/spec/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testReadPetStore()
$this->assertInstanceOf(\cebe\openapi\spec\Components::class, $openapi->components);

// security
$this->assertNull($openapi->security); # since it is not present in spec
$this->assertAllInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $openapi->security);

// tags
$this->assertAllInstanceOf(\cebe\openapi\spec\Tag::class, $openapi->tags);
Expand Down Expand Up @@ -235,8 +235,7 @@ public function testSpecs($openApiFile)
}

// security
$openapi->security !== null && $this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirements::class, $openapi->security);
$openapi->security !== null && $this->assertAllInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $openapi->security->getRequirements());
$this->assertAllInstanceOf(\cebe\openapi\spec\SecurityRequirement::class, $openapi->security);

// tags
$this->assertAllInstanceOf(\cebe\openapi\spec\Tag::class, $openapi->tags);
Expand Down
Loading
Loading