Skip to content

Commit 864fb05

Browse files
committed
Fix issue 2
1 parent 02b980e commit 864fb05

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/spec/SecurityRequirements.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public function __construct(array $data)
2626
foreach ($data as $index => $value) {
2727
if (is_numeric($index)) { // read
2828
if ($value === []) { # empty Security Requirement Object (`{}`) = anonymous access https://github.com/cebe/php-openapi/issues/238
29-
$this->_securityRequirements[$index] = new SecurityRequirement($value);
30-
// break;
29+
$this->_securityRequirements[$index] = $value;
3130
} else {
3231
$this->_securityRequirements[array_keys($value)[0]] = new SecurityRequirement(array_values($value)[0]);
3332
}
@@ -69,7 +68,7 @@ public function getSerializableData()
6968
/** @var SecurityRequirement $securityRequirement */
7069

7170
if (is_numeric($name)) {
72-
$data[$name] = $securityRequirement->getSerializableData();
71+
$data[$name] = (object) $securityRequirement; # case https://github.com/cebe/php-openapi/issues/238
7372
} else {
7473
$data[] = [$name => $securityRequirement->getSerializableData()];
7574
}

tests/issues/238/Issue238Test.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,59 @@
11
<?php
22

33
use cebe\openapi\Reader;
4+
use cebe\openapi\spec\OpenApi;
5+
use cebe\openapi\spec\SecurityRequirements;
6+
use cebe\openapi\Writer;
47

58
// https://github.com/cebe/php-openapi/issues/238
69
class Issue238Test extends \PHPUnit\Framework\TestCase
710
{
8-
public function test238AddSupportForEmptySecurityRequirementObjectInSecurityRequirement()
11+
public function test238AddSupportForEmptySecurityRequirementObjectInSecurityRequirementRead()
912
{
1013
$openapi = Reader::readFromYamlFile(dirname(dirname(__DIR__)).'/data/issue/238/spec.yml');
1114
$this->assertInstanceOf(\cebe\openapi\SpecObjectInterface::class, $openapi);
1215
$this->assertInstanceOf(\cebe\openapi\spec\SecurityRequirements::class, $openapi->paths->getPath('/path-secured')->getOperations()['get']->security);
13-
$this->assertSame($openapi->paths->getPath('/path-secured')->getOperations()['get']->security->getSerializableData(), [[]]);
16+
$this->assertSame(json_decode(json_encode($openapi->paths->getPath('/path-secured')->getOperations()['get']->security->getSerializableData()), true), [[]]);
1417

1518
// return; # TODO
1619
// $openapi = Reader::readFromJsonFile(__DIR__.'/data/issue/238/spec.json');
1720
// $this->assertInstanceOf(\cebe\openapi\SpecObjectInterface::class, $openapi);
1821
}
22+
23+
public function test238AddSupportForEmptySecurityRequirementObjectInSecurityRequirementWrite()
24+
{
25+
$openapi = $this->createOpenAPI([
26+
'security' => new SecurityRequirements([
27+
[]
28+
]),
29+
]);
30+
31+
$yaml = Writer::writeToYaml($openapi);
32+
33+
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
34+
openapi: 3.0.0
35+
info:
36+
title: 'Test API'
37+
version: 1.0.0
38+
paths: { }
39+
security:
40+
- { }
41+
42+
YAML
43+
),
44+
$yaml
45+
);
46+
}
47+
48+
private function createOpenAPI($merge = [])
49+
{
50+
return new OpenApi(array_merge([
51+
'openapi' => '3.0.0',
52+
'info' => [
53+
'title' => 'Test API',
54+
'version' => '1.0.0',
55+
],
56+
'paths' => [],
57+
], $merge));
58+
}
1959
}

0 commit comments

Comments
 (0)