Skip to content

Commit 088af05

Browse files
authored
Merge pull request #115 from WyriHaximus-labs/webhooks
Add support for WebHooks
2 parents 551b05f + 0c10bea commit 088af05

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

src/SpecBaseObject.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,23 @@ protected function hasProperty(string $name): bool
302302
return isset($this->_properties[$name]) || isset($this->attributes()[$name]);
303303
}
304304

305-
protected function requireProperties(array $names)
305+
protected function requireProperties(array $names, array $atLeastOne = [])
306306
{
307307
foreach ($names as $name) {
308308
if (!isset($this->_properties[$name])) {
309309
$this->addError(" is missing required property: $name", get_class($this));
310310
}
311311
}
312+
313+
if (count($atLeastOne) > 0) {
314+
foreach ($atLeastOne as $name) {
315+
if (array_key_exists($name, $this->_properties)) {
316+
return;
317+
}
318+
}
319+
320+
$this->addError(" is missing at least one of the following required properties: " . implode(', ', $atLeastOne), get_class($this));
321+
}
312322
}
313323

314324
protected function validateEmail(string $property)

src/spec/OpenApi.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @property Server[] $servers
2121
* @property Paths|PathItem[] $paths
2222
* @property Components|null $components
23+
* @property PathItem[]|null $webhooks
2324
* @property SecurityRequirement[] $security
2425
* @property Tag[] $tags
2526
* @property ExternalDocumentation|null $externalDocs
@@ -46,6 +47,7 @@ protected function attributes(): array
4647
'info' => Info::class,
4748
'servers' => [Server::class],
4849
'paths' => Paths::class,
50+
'webhooks' => [PathItem::class],
4951
'components' => Components::class,
5052
'security' => [SecurityRequirement::class],
5153
'tags' => [Tag::class],
@@ -83,7 +85,12 @@ public function __get($name)
8385
*/
8486
public function performValidation()
8587
{
86-
$this->requireProperties(['openapi', 'info', 'paths']);
88+
if ($this->getMajorVersion() === static::VERSION_3_0) {
89+
$this->requireProperties(['openapi', 'info', 'paths']);
90+
} else {
91+
$this->requireProperties(['openapi', 'info'], ['paths', 'webhooks', 'components']);
92+
}
93+
8794
if (!empty($this->openapi) && !preg_match(static::PATTERN_VERSION, $this->openapi)) {
8895
$this->addError('Unsupported openapi version: ' . $this->openapi);
8996
}

src/spec/Schema.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @property string[] $required list of required properties
4141
* @property array $enum
4242
*
43-
* @property string $type
43+
* @property string|string[] $type type can only be `string` in OpenAPI 3.0, but can be an array of strings since OpenAPI 3.1
4444
* @property Schema[]|Reference[] $allOf
4545
* @property Schema[]|Reference[] $oneOf
4646
* @property Schema[]|Reference[] $anyOf

src/spec/Type.php

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Type
2121
const BOOLEAN = 'boolean';
2222
const OBJECT = 'object';
2323
const ARRAY = 'array';
24+
const NULL = 'null'; // Since OpenAPI 3.1
2425

2526
/**
2627
* Indicate whether a type is a scalar type, i.e. not an array or object.
@@ -38,6 +39,7 @@ public static function isScalar(string $type): bool
3839
self::NUMBER,
3940
self::STRING,
4041
self::BOOLEAN,
42+
self::NULL,
4143
]);
4244
}
4345
}

tests/spec/OpenApiTest.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testEmpty()
1717
$this->assertEquals([
1818
'OpenApi is missing required property: openapi',
1919
'OpenApi is missing required property: info',
20-
'OpenApi is missing required property: paths',
20+
'OpenApi is missing at least one of the following required properties: paths, webhooks, components',
2121
], $openapi->getErrors());
2222

2323
// check default value of servers
@@ -232,10 +232,15 @@ public function testSpecs($openApiFile)
232232
$this->assertAllInstanceOf(\cebe\openapi\spec\Server::class, $openapi->servers);
233233

234234
// paths
235-
if ($openapi->components !== null) {
235+
if ($openapi->paths !== null) {
236236
$this->assertInstanceOf(\cebe\openapi\spec\Paths::class, $openapi->paths);
237237
}
238238

239+
// webhooks
240+
if ($openapi->webhooks !== null) {
241+
$this->assertAllInstanceOf(\cebe\openapi\spec\PathItem::class, $openapi->webhooks);
242+
}
243+
239244
// components
240245
if ($openapi->components !== null) {
241246
$this->assertInstanceOf(\cebe\openapi\spec\Components::class, $openapi->components);

tests/spec/PathTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testRead()
6666
}
6767
}
6868

69-
public function testCreateionFromObjects()
69+
public function testCreationFromObjects()
7070
{
7171
$paths = new Paths([
7272
'/pets' => new PathItem([

0 commit comments

Comments
 (0)