Skip to content

Commit 01acc44

Browse files
authored
Merge pull request #4 from FandaOG/vyuldashev-master
Vyuldashev master
2 parents 524c89e + 7f04e42 commit 01acc44

26 files changed

+733
-89
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ jobs:
1313
strategy:
1414
fail-fast: true
1515
matrix:
16-
php: [8.0]
17-
stability: [prefer-lowest, prefer-stable]
16+
php: [8.0, 8.1]
1817

19-
name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
18+
name: PHP ${{ matrix.php }}
2019

2120
steps:
2221
- name: Checkout code
@@ -30,7 +29,7 @@ jobs:
3029
coverage: none
3130

3231
- name: Install dependencies
33-
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress
32+
run: composer update --prefer-dist --no-interaction --no-progress
3433

3534
- name: Execute tests
3635
run: vendor/bin/phpunit --verbose

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"require-dev": {
3030
"orchestra/testbench": "^5.3|^6.0|^7.0",
31-
"phpunit/phpunit": "^9.4"
31+
"phpunit/phpunit": "^9.5.13"
3232
},
3333
"autoload": {
3434
"psr-4": {

config/openapi.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
'title' => config('app.name'),
1111
'description' => null,
1212
'version' => '1.0.0',
13+
'contact' => [],
1314
],
1415

1516
'servers' => [
1617
[
1718
'url' => env('APP_URL'),
19+
'description' => null,
20+
'variables' => [],
1821
],
1922
],
2023

@@ -43,6 +46,9 @@
4346
'paths' => [
4447
//
4548
],
49+
'components' => [
50+
//
51+
],
4652
],
4753

4854
],

docs/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626
'/schemas',
2727
'/collections',
2828
'/middlewares',
29+
'/security'
2930
],
3031
displayAllHeaders: true,
3132
sidebarDepth: 2,

docs/middlewares.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
# Middlewares
22

3+
Middlewares are an optional bit of logic to transform the given data at various lifecycle points.
4+
5+
### Path
6+
7+
To add a path middleware create a class that implements `\Vyuldashev\LaravelOpenApi\Contracts\PathMiddleware` then register it by referencing it in the `openapi.collections.default.middlewares.paths` config array like `MyPathMiddleware::class`
8+
9+
Available lifecycle points are:
10+
- `before` - after collecting all `RouteInformation` but before processing them.
11+
- `after` - after the `PathItem` has been built.
12+
13+
### Component
14+
15+
To add a path middleware create a class that implements `\Vyuldashev\LaravelOpenApi\Contracts\ComponentMiddleware` then register it by referencing it in the `openapi.collections.default.middlewares.components` config array like `MyComponentMiddleware::class`
16+
17+
Available lifecycle points are:
18+
- `after` - after the `Components` has been built.

docs/paths/operations.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ The following definition will be generated:
3939
}
4040
```
4141

42+
## Security
43+
44+
See [Security](../security.md#operation-level-example)
45+
4246
## Tags
4347

4448
Tags can be used for logical grouping of operations by resources or any other qualifier.

docs/security.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Security
2+
3+
```bash
4+
php artisan openapi:make-security-scheme BearerToken
5+
```
6+
7+
After you generate a security scheme, it will be declared in the `securitySchemes` section, you can apply it to the whole API or individual operations by adding the security section on the root level or operation level, respectively. When used on the root level, security applies the specified security schemes globally to all API operations, unless overridden on the operation level.
8+
9+
## Root level example
10+
11+
`config/openapi.php`:
12+
13+
```php
14+
15+
'security' => [
16+
GoldSpecDigital\ObjectOrientedOAS\Objects\SecurityRequirement::create()->securityScheme('BearerToken'),
17+
],
18+
19+
```
20+
21+
## Operation level example
22+
23+
```php
24+
use Vyuldashev\LaravelOpenApi\Attributes as OpenApi;
25+
26+
#[OpenApi\PathItem]
27+
class UserController extends Controller
28+
{
29+
/**
30+
* Create new user.
31+
*
32+
* Creates new user or returns already existing user by email.
33+
*/
34+
#[OpenApi\Operation(security: 'BearerToken')]
35+
public function store(Request $request)
36+
{
37+
//
38+
}
39+
}
40+
```

examples/petstore/OpenApi/Schemas/PetSchema.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class PetSchema extends SchemaFactory implements Reusable
1212
{
1313
/**
1414
* @return Schema
15+
*
1516
* @throws InvalidArgumentException
1617
*/
1718
public function build(): SchemaContract

src/Attributes/Operation.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ class Operation
1919
public ?string $method;
2020

2121
/**
22-
* @param string|null $id
23-
* @param array $tags
24-
* @param \Vyuldashev\LaravelOpenApi\Factories\SecuritySchemeFactory|string|null $security
25-
* @param string|null $method
22+
* @param string|null $id
23+
* @param array $tags
24+
* @param \Vyuldashev\LaravelOpenApi\Factories\SecuritySchemeFactory|string|null $security
25+
* @param string|null $method
26+
*
2627
* @throws InvalidArgumentException
2728
*/
2829
public function __construct(string $id = null, array $tags = [], string $security = null, string $method = null)
@@ -31,14 +32,21 @@ public function __construct(string $id = null, array $tags = [], string $securit
3132
$this->tags = $tags;
3233
$this->method = $method;
3334

35+
if ($security === '') {
36+
//user wants to turn off security on this operation
37+
$this->security = $security;
38+
39+
return;
40+
}
41+
3442
if ($security) {
35-
$this->security = class_exists($security) ? $security : app()->getNamespace() . 'OpenApi\\SecuritySchemes\\' . $security . 'SecurityScheme';
43+
$this->security = class_exists($security) ? $security : app()->getNamespace().'OpenApi\\SecuritySchemes\\'.$security;
3644

37-
if (!is_a($this->security, SecuritySchemeFactory::class, true)) {
45+
if (! is_a($this->security, SecuritySchemeFactory::class, true)) {
3846
throw new InvalidArgumentException(
39-
sprintf('Security class is either not declared or is not an instance of [%s]', SecuritySchemeFactory::class)
47+
sprintf('Security class is either not declared or is not an instance of %s', SecuritySchemeFactory::class)
4048
);
4149
}
4250
}
4351
}
44-
}
52+
}

src/Builders/Components/Builder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ public function __construct(array $directories)
2020
protected function getAllClasses(string $collection): Collection
2121
{
2222
return collect($this->directories)
23-
->map(function(string $directory) {
23+
->map(function (string $directory) {
2424
$map = ClassMapGenerator::createMap($directory);
2525

2626
return array_keys($map);
2727
})
2828
->flatten()
29-
->filter(function(string $class) use($collection) {
29+
->filter(function (string $class) use ($collection) {
3030
$reflectionClass = new ReflectionClass($class);
3131
$collectionAttributes = $reflectionClass->getAttributes(CollectionAttribute::class);
3232

33-
if(count($collectionAttributes) === 0 && $collection === Generator::COLLECTION_DEFAULT) {
33+
if (count($collectionAttributes) === 0 && $collection === Generator::COLLECTION_DEFAULT) {
3434
return true;
3535
}
3636

37-
if(count($collectionAttributes) === 0) {
37+
if (count($collectionAttributes) === 0) {
3838
return false;
3939
}
4040

0 commit comments

Comments
 (0)