-
Notifications
You must be signed in to change notification settings - Fork 932
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
Webhooks #1462
Webhooks #1462
Changes from all commits
9b40e03
f913b69
6839599
54e130d
1640feb
860be67
e63c24e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace OpenApi\Examples\WebhookExample; | ||
|
||
use OpenApi\Annotations as OA; | ||
|
||
/** | ||
* @OA\OpenApi( | ||
* @OA\Info( | ||
* version="1.0.0", | ||
* title="Webhook Example", | ||
* ), | ||
* webhooks={ | ||
* "newPet": @OA\PathItem( | ||
* @OA\Post( | ||
* @OA\RequestBody( | ||
* description="Information about a new pet in the system", | ||
* @OA\MediaType( | ||
* mediaType="application/json", | ||
* @OA\Schema(ref="#/components/schemas/Pet") | ||
* ), | ||
* ), | ||
* @OA\Response( | ||
* response=200, | ||
* description="Return a 200 status to indicate that the data was received successfully" | ||
* ) | ||
* ) | ||
* ) | ||
* } | ||
* ) | ||
*/ | ||
class OpenApiSpec | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenApi\Examples\WebhookExample; | ||
|
||
use OpenApi\Annotations as OA; | ||
|
||
/** | ||
* @OA\Schema(required={"id", "name"}) | ||
*/ | ||
final class Pet | ||
{ | ||
/** | ||
* @OA\Property(format="int64") | ||
* | ||
* @var int | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @OA\Property | ||
* | ||
* @var string | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* @OA\Property | ||
* | ||
* @var string | ||
*/ | ||
public $tag; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: 'Webhook Example' | ||
version: 1.0.0 | ||
components: | ||
schemas: | ||
Pet: | ||
required: | ||
- id | ||
- name | ||
properties: | ||
id: | ||
type: integer | ||
format: int64 | ||
name: | ||
type: string | ||
tag: | ||
type: string | ||
type: object | ||
webhooks: | ||
newPet: | ||
post: | ||
requestBody: | ||
description: 'Information about a new pet in the system' | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Pet' | ||
responses: | ||
'200': | ||
description: 'Return a 200 status to indicate that the data was received successfully' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -566,6 +566,8 @@ The list of values includes alternative security requirement objects that can be | |
Only one of the security requirement objects need to be satisfied to authorize a request.<br /> | ||
Individual operations can override this definition.<br /> | ||
To make security optional, an empty security requirement `({})` can be included in the array.</p></dd> | ||
<dt><strong>webhooks</strong> : <span style="font-family: monospace;">array<string,PathItem|string|class-string|object></span></dt> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docs need refresh - ideally done right before merge |
||
<dd><p>The available webhooks for the API.</p></dd> | ||
</dl> | ||
|
||
#### Reference | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,13 @@ class OpenApi extends AbstractAnnotation | |
*/ | ||
public $externalDocs = Generator::UNDEFINED; | ||
|
||
/** | ||
* The available webhooks for the API. | ||
* | ||
* @var Webhook[] | ||
*/ | ||
public $webhooks = Generator::UNDEFINED; | ||
|
||
/** | ||
* @var Analysis | ||
*/ | ||
|
@@ -106,7 +113,7 @@ class OpenApi extends AbstractAnnotation | |
/** | ||
* @inheritdoc | ||
*/ | ||
public static $_required = ['openapi', 'info', 'paths']; | ||
public static $_required = ['openapi', 'info']; | ||
DerManoMann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @inheritdoc | ||
|
@@ -115,6 +122,7 @@ class OpenApi extends AbstractAnnotation | |
Info::class => 'info', | ||
Server::class => ['servers'], | ||
PathItem::class => ['paths', 'path'], | ||
Webhook::class => ['webhooks'], | ||
Components::class => 'components', | ||
Tag::class => ['tags'], | ||
ExternalDocumentation::class => 'externalDocs', | ||
|
@@ -143,6 +151,18 @@ public function validate(array $stack = null, array $skip = null, string $ref = | |
return false; | ||
} | ||
|
||
if ($this->openapi === self::VERSION_3_0_0 && Generator::isDefault($this->paths)) { | ||
$this->_context->logger->warning('Required @OA\PathItem() not found'); | ||
|
||
return false; | ||
} | ||
|
||
if ($this->openapi === self::VERSION_3_1_0 && Generator::isDefault($this->paths) && Generator::isDefault($this->webhooks) && Generator::isDefault($this->components)) { | ||
$this->_context->logger->warning("At least one of 'Required @OA\Info(), @OA\Components() or @OA\Webhook() not found'"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be |
||
|
||
return false; | ||
} | ||
|
||
return parent::validate([], [], '#', new \stdClass()); | ||
} | ||
|
||
|
@@ -230,4 +250,19 @@ private static function resolveRef(string $ref, string $resolved, $container, ar | |
|
||
throw new \Exception('$ref "' . $unresolved . '" not found'); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
#[\ReturnTypeWillChange] | ||
public function jsonSerialize() | ||
{ | ||
$data = parent::jsonSerialize(); | ||
|
||
if (false === $this->_context->isVersion(OpenApi::VERSION_3_1_0)) { | ||
unset($data->webhooks); | ||
} | ||
|
||
return $data; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license Apache 2.0 | ||
*/ | ||
|
||
namespace OpenApi\Annotations; | ||
|
||
use OpenApi\Generator; | ||
|
||
/** | ||
* @Annotation | ||
*/ | ||
class Webhook extends AbstractAnnotation | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $name = Generator::UNDEFINED; | ||
|
||
/** | ||
* @var PathItem | ||
*/ | ||
public $pathItem = Generator::UNDEFINED; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license Apache 2.0 | ||
*/ | ||
|
||
namespace OpenApi\Attributes; | ||
|
||
use OpenApi\Generator; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
class Webhook extends \OpenApi\Annotations\Webhook | ||
{ | ||
/** | ||
* @param string|null $name | ||
* @param PathItem|null $pathItem | ||
* @param array<string,mixed>|null $x | ||
* @param Attachable[]|null $attachables | ||
*/ | ||
public function __construct( | ||
?string $name = null, | ||
?PathItem $pathItem = null, | ||
// annotation | ||
?array $x = null, | ||
?array $attachables = null | ||
) { | ||
parent::__construct([ | ||
'name' => $name ?? Generator::UNDEFINED, | ||
'pathItem' => $pathItem ?? Generator::UNDEFINED, | ||
'x' => $x ?? Generator::UNDEFINED, | ||
'value' => $this->combine($name, $pathItem, $attachables), | ||
]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,6 @@ public function testScan(string $sourceDir, iterable $sources): void | |
public function testScanInvalidSource(): void | ||
{ | ||
$this->assertOpenApiLogEntryContains('Skipping invalid source: /tmp/__swagger_php_does_not_exist__'); | ||
$this->assertOpenApiLogEntryContains('Required @OA\Info() not found'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this is surprising. |
||
$this->assertOpenApiLogEntryContains('Required @OA\PathItem() not found'); | ||
|
||
(new Generator($this->getTrackingLogger())) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still needs updating I think?