26
26
use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
27
27
use ApiPlatform \Metadata \Resource \Factory \ResourceNameCollectionFactoryInterface ;
28
28
use ApiPlatform \Metadata \Resource \ResourceMetadataCollection ;
29
+ use ApiPlatform \OpenApi \Attributes \Webhook ;
29
30
use ApiPlatform \OpenApi \Model ;
30
31
use ApiPlatform \OpenApi \Model \Components ;
31
32
use ApiPlatform \OpenApi \Model \Contact ;
@@ -89,12 +90,13 @@ public function __invoke(array $context = []): OpenApi
89
90
$ servers = '/ ' === $ baseUrl || '' === $ baseUrl ? [new Server ('/ ' )] : [new Server ($ baseUrl )];
90
91
$ paths = new Paths ();
91
92
$ schemas = new \ArrayObject ();
93
+ $ webhooks = new \ArrayObject ();
92
94
93
95
foreach ($ this ->resourceNameCollectionFactory ->create () as $ resourceClass ) {
94
96
$ resourceMetadataCollection = $ this ->resourceMetadataFactory ->create ($ resourceClass );
95
97
96
98
foreach ($ resourceMetadataCollection as $ resourceMetadata ) {
97
- $ this ->collectPaths ($ resourceMetadata , $ resourceMetadataCollection , $ paths , $ schemas );
99
+ $ this ->collectPaths ($ resourceMetadata , $ resourceMetadataCollection , $ paths , $ schemas, $ webhooks );
98
100
}
99
101
}
100
102
@@ -118,11 +120,15 @@ public function __invoke(array $context = []): OpenApi
118
120
new \ArrayObject (),
119
121
new \ArrayObject ($ securitySchemes )
120
122
),
121
- $ securityRequirements
123
+ $ securityRequirements ,
124
+ [],
125
+ null ,
126
+ null ,
127
+ $ webhooks
122
128
);
123
129
}
124
130
125
- private function collectPaths (ApiResource $ resource , ResourceMetadataCollection $ resourceMetadataCollection , Paths $ paths , \ArrayObject $ schemas ): void
131
+ private function collectPaths (ApiResource $ resource , ResourceMetadataCollection $ resourceMetadataCollection , Paths $ paths , \ArrayObject $ schemas, \ ArrayObject $ webhooks ): void
126
132
{
127
133
if (0 === $ resource ->getOperations ()->count ()) {
128
134
return ;
@@ -135,10 +141,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
135
141
continue ;
136
142
}
137
143
138
- $ openapiOperation = $ operation ->getOpenapi ();
144
+ $ openapiAttribute = $ operation ->getOpenapi ();
139
145
140
146
// Operation ignored from OpenApi
141
- if ($ operation instanceof HttpOperation && false === $ openapiOperation ) {
147
+ if ($ operation instanceof HttpOperation && false === $ openapiAttribute ) {
142
148
continue ;
143
149
}
144
150
@@ -162,8 +168,15 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
162
168
continue ;
163
169
}
164
170
165
- if (!\is_object ($ openapiOperation )) {
171
+ $ pathItem = null ;
172
+
173
+ if ($ openapiAttribute instanceof Webhook) {
174
+ $ pathItem = $ openapiAttribute ->getPathItem () ?: new PathItem ();
175
+ $ openapiOperation = $ pathItem ->{'get ' .ucfirst (strtolower ($ method ))}() ?: new Model \Operation ();
176
+ } elseif (!\is_object ($ openapiAttribute )) {
166
177
$ openapiOperation = new Model \Operation ();
178
+ } else {
179
+ $ openapiOperation = $ openapiAttribute ;
167
180
}
168
181
169
182
// Complete with defaults
@@ -227,9 +240,9 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
227
240
$ openapiOperation = $ openapiOperation ->withDeprecated ((bool ) $ operation ->getOpenapiContext ()['deprecated ' ]);
228
241
}
229
242
230
- if ($ path ) {
243
+ if (! $ pathItem && $ path ) {
231
244
$ pathItem = $ paths ->getPath ($ path ) ?: new PathItem ();
232
- } else {
245
+ } elseif (! $ pathItem ) {
233
246
$ pathItem = new PathItem ();
234
247
}
235
248
@@ -384,7 +397,14 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
384
397
}
385
398
}
386
399
387
- $ paths ->addPath ($ path , $ pathItem ->{'with ' .ucfirst ($ method )}($ openapiOperation ));
400
+ if ($ openapiAttribute instanceof Webhook) {
401
+ if (!isset ($ webhooks [$ openapiAttribute ->getName ()])) {
402
+ $ webhooks [$ openapiAttribute ->getName ()] = new \ArrayObject ();
403
+ }
404
+ $ webhooks [$ openapiAttribute ->getName ()]->append ($ pathItem ->{'with ' .ucfirst ($ method )}($ openapiOperation ));
405
+ } else {
406
+ $ paths ->addPath ($ path , $ pathItem ->{'with ' .ucfirst ($ method )}($ openapiOperation ));
407
+ }
388
408
}
389
409
}
390
410
@@ -510,7 +530,7 @@ private function getLinks(ResourceMetadataCollection $resourceMetadataCollection
510
530
}
511
531
512
532
// Operation ignored from OpenApi
513
- if ($ operation instanceof HttpOperation && false === $ operation ->getOpenapi ()) {
533
+ if ($ operation instanceof HttpOperation && ( false === $ operation ->getOpenapi () || $ operation -> getOpenapi () instanceof Webhook )) {
514
534
continue ;
515
535
}
516
536
0 commit comments