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

Webhooks #1462

Closed
wants to merge 7 commits into from
Closed

Webhooks #1462

wants to merge 7 commits into from

Conversation

qdequippe
Copy link

@qdequippe qdequippe marked this pull request as ready for review August 1, 2023 12:33
@qdequippe
Copy link
Author

qdequippe commented Aug 1, 2023

It seems too easy ☺️ am I missing something to implement? open for feedbacks

@DerManoMann
Copy link
Collaborator

Nice one. It would be great if you could add some tests. Allowing a union of native types (string, etc) and PathItem might break things?

@DerManoMann
Copy link
Collaborator

Also, the serialize code might need to be conditional to suppress the property for 3.0.0

@qdequippe
Copy link
Author

@DerManoMann I added test and changed types also for webhooks (only PathItem finally).

Also, the serialize code might need to be conditional to suppress the property for 3.0.0
tests failed due to this change, any idea on how to handle this properly?

Copy link
Collaborator

@DerManoMann DerManoMann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good so far.

Still some way to go to make all existing tests pass, tho. Also, it would be good to ensure that webhook cannot be used with 3.0.0 - something like https://github.com/zircote/swagger-php/blob/master/src/Annotations/License.php#L77

src/Annotations/OpenApi.php Show resolved Hide resolved
src/Attributes/OpenApi.php Outdated Show resolved Hide resolved
@DerManoMann
Copy link
Collaborator

Sorry for ignoring this - I am quite busy ATM with paid work :/

@qdequippe
Copy link
Author

Sorry for ignoring this - I am quite busy ATM with paid work :/

No problem it's normal ;) I am quite busy too, it's not critical

Copy link
Collaborator

@DerManoMann DerManoMann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking pretty good! Just a few minor things...

/**
* The available webhooks for the API.
*
* @var array<string,PathItem>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might actually be nice to have a new Webhook annotation/attribute for this rather than using raw maps on top level elements.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with that, I got this idea at first but I didn't know if I could add a custom annotation/attribute

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bye the way, how do you think to handle this new annotation/attribute? a new Processor BuildWebhooks?

src/Annotations/OpenApi.php Show resolved Hide resolved
@@ -143,6 +150,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('The OpenAPI document must contain paths field');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd prefer if the error messag ecould be kept the same, even if it means manually reproducing the format.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e. 'Required @OA\Info() not found'

}

if ($this->openapi === self::VERSION_3_1_0 && Generator::isDefault($this->paths) && Generator::isDefault($this->webhooks) && Generator::isDefault($this->components)) {
$this->_context->logger->warning('The OpenAPI document must contain at least one paths field, a components field or a webhooks field');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A message in similar style as above wouold be great.

Perhasps

At least one of 'Required @OA\Info(), @OA\Components() or @OA\Webhook() not found'

src/Attributes/OpenApi.php Outdated Show resolved Hide resolved
Copy link
Collaborator

@DerManoMann DerManoMann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks promising.

A few more things:

  • Webhook needs to be added to the parent/nested arrays similar to other annotations in OpenApi (annotation)
  • The attribute version should extend the annotation version. Saves on re-declaring the properties and also allows to simplify the parent/nested relationships (which are based on annotations only)

There still needs to be code to merge the Webhook data into the right place when processing the annotation tree.
Again, this is related to $_nested. This should probably look something like this:

    public static $_nested = [
        Webhook::class => ['webhooks', 'name'],
    ];

From memory the first element in the array is the property this gets merged in and if there is a second value then that is used as propery lookup in the annotation (Webhook) to find the key to store in under.
Sounds complicated (and it kinda is, at least to understand).

* version="1.0.0",
* title="Webhook Example",
* ),
* webhooks={
Copy link
Collaborator

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?

@@ -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&lt;string,PathItem|string|class-string|object&gt;</span></dt>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs need refresh - ideally done right before merge

@@ -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');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this is surprising.

}

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'");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be @OA\PathItem instead of @OA\Info

@DerManoMann DerManoMann mentioned this pull request Dec 5, 2023
@DerManoMann
Copy link
Collaborator

Closing in favour of #1511.

Thanks @qdequippe for doing most of the hard work.

@qdequippe
Copy link
Author

Thanks @DerManoMann for your feedbacks on this PR, sorry for the lack of time on my side.

@qdequippe qdequippe deleted the webhook branch December 11, 2023 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants