Skip to content

Commit dc0863c

Browse files
committed
Changes
1 parent 2876e4a commit dc0863c

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

src/spec/OpenApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function attributes(): array
3737
'servers' => [Server::class],
3838
'paths' => Paths::class,
3939
'components' => Components::class,
40-
'security' => [Type::STRING, SecurityRequirement::class],
40+
'security' => [SecurityRequirement::class],
4141
'tags' => [Tag::class],
4242
'externalDocs' => ExternalDocumentation::class,
4343
];

src/spec/Operation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function attributes(): array
4646
'responses' => Responses::class,
4747
'callbacks' => [Type::STRING, Callback::class],
4848
'deprecated' => Type::BOOLEAN,
49-
'security' => [Type::STRING, SecurityRequirement::class],
49+
'security' => [SecurityRequirement::class],
5050
'servers' => [Server::class],
5151
];
5252
}

tests/WriterTest.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use cebe\openapi\spec\OpenApi;
55
use cebe\openapi\spec\Operation;
66
use cebe\openapi\spec\PathItem;
7+
use cebe\openapi\spec\Response;
8+
use cebe\openapi\spec\Responses;
79
use cebe\openapi\spec\SecurityRequirement;
810
use cebe\openapi\spec\SecurityScheme;
911

@@ -193,7 +195,7 @@ public function testWriteEmptySecurityPartYaml()
193195
);
194196
}
195197

196-
public function testSecurity()
198+
public function testSecurityAtPathOperationLevel()
197199
{
198200
$openapi = $this->createOpenAPI([
199201
'components' => new Components([
@@ -209,8 +211,11 @@ public function testSecurity()
209211
'/test' => new PathItem([
210212
'get' => new Operation([
211213
'security' => [
212-
'BearerAuth' => new SecurityRequirement([])
214+
new SecurityRequirement(['BearerAuth' => []])
213215
],
216+
'responses' => new Responses([
217+
200 => new Response(['description' => 'OK']),
218+
])
214219
])
215220
])
216221
]
@@ -227,15 +232,61 @@ public function testSecurity()
227232
paths:
228233
/test:
229234
get:
235+
responses:
236+
'200':
237+
description: OK
230238
security:
231-
BearerAuth: { }
239+
-
240+
BearerAuth: []
232241
components:
233242
securitySchemes:
234243
BearerAuth:
235244
type: http
236245
scheme: bearer
237246
bearerFormat: 'AuthToken and JWT Format'
238247
248+
YAML
249+
),
250+
$yaml
251+
);
252+
}
253+
254+
public function testSecurityAtGlobalLevel()
255+
{
256+
$openapi = $this->createOpenAPI([
257+
'components' => new Components([
258+
'securitySchemes' => [
259+
'BearerAuth' => new SecurityScheme([
260+
'type' => 'http',
261+
'scheme' => 'bearer',
262+
'bearerFormat' => 'AuthToken and JWT Format' # optional, arbitrary value for documentation purposes
263+
])
264+
],
265+
]),
266+
'security' => [
267+
'BearerAuth' => new SecurityRequirement([])
268+
],
269+
'paths' => [],
270+
]);
271+
272+
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
273+
274+
275+
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
276+
openapi: 3.0.0
277+
info:
278+
title: 'Test API'
279+
version: 1.0.0
280+
paths: { }
281+
components:
282+
securitySchemes:
283+
BearerAuth:
284+
type: http
285+
scheme: bearer
286+
bearerFormat: 'AuthToken and JWT Format'
287+
security:
288+
BearerAuth: { }
289+
239290
YAML
240291
),
241292
$yaml

0 commit comments

Comments
 (0)