Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: How to use Security and SecurityRequirement #225

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/spec/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace cebe\openapi\spec;

use cebe\openapi\exceptions\TypeErrorException;
use cebe\openapi\SpecBaseObject;

/**
Expand Down Expand Up @@ -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,
];
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' => [SecurityRequirement::class],
'security' => [Type::STRING, SecurityRequirement::class],
'servers' => [Server::class],
];
}
Expand Down
32 changes: 32 additions & 0 deletions tests/WriterTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

use cebe\openapi\spec\Components;
use cebe\openapi\spec\OpenApi;
use cebe\openapi\spec\Operation;
use cebe\openapi\spec\PathItem;
use cebe\openapi\spec\SecurityRequirement;
use cebe\openapi\spec\SecurityScheme;

class WriterTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -187,4 +192,31 @@ public function testWriteEmptySecurityPartYaml()
$yaml
);
}

public function testSecurity()
{
$openapi = new OpenApi([
'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' => [
'BearerAuth' => new SecurityRequirement([])
],
])
])
]
]);

$result = json_decode(json_encode($openapi->getSerializableData()), true);
$this->assertTrue($result);
}
}
Loading